Skip to content

Commit 194e1bf

Browse files
authored
chore(web): align cluster connection options with the design COMPASS-8794 (#6603)
chore(web): align cluster connection options with the design
1 parent 27e0f3e commit 194e1bf

File tree

4 files changed

+16
-32
lines changed

4 files changed

+16
-32
lines changed

packages/compass-web/polyfills/net/index.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@ import { Duplex } from 'stream';
88
* implementation
99
*/
1010
class Socket extends Duplex {
11-
private _tls = false;
1211
private _ws: WebSocket | null = null;
13-
private _setOptions: {
14-
setKeepAlive?: { enabled?: boolean; initialDelay?: number };
15-
setTimeout?: { timeout?: number };
16-
setNoDelay?: { noDelay?: boolean };
17-
} = {};
1812
constructor() {
1913
super();
2014
}
@@ -27,7 +21,6 @@ class Socket extends Duplex {
2721
lookup?: ConnectionOptions['lookup'];
2822
tls?: boolean;
2923
}) {
30-
this._tls = !!options.tls;
3124
const { wsURL, ...atlasOptions } =
3225
lookup?.() ?? ({} as { wsURL?: string; clusterName?: string });
3326
this._ws = new WebSocket(wsURL ?? '/ws-proxy');
@@ -62,7 +55,7 @@ class Socket extends Duplex {
6255
const res = JSON.parse(data) as { preMessageOk: 1 };
6356
if (res.preMessageOk) {
6457
setTimeout(() => {
65-
this.emit(this._tls ? 'secureConnect' : 'connect');
58+
this.emit(options.tls ? 'secureConnect' : 'connect');
6659
});
6760
}
6861
} catch (err) {
@@ -108,19 +101,13 @@ class Socket extends Duplex {
108101
this._ws?.close();
109102
return this;
110103
}
111-
setKeepAlive(enabled = false, initialDelay = 0) {
112-
this._setOptions.setKeepAlive = { enabled, initialDelay };
104+
setKeepAlive() {
113105
return this;
114106
}
115-
setTimeout(timeout: number, cb?: () => void) {
116-
this._setOptions.setTimeout = { timeout };
117-
if (cb) {
118-
this.once('timeout', cb);
119-
}
107+
setTimeout() {
120108
return this;
121109
}
122-
setNoDelay(noDelay = true) {
123-
this._setOptions.setNoDelay = { noDelay };
110+
setNoDelay() {
124111
return this;
125112
}
126113
}

packages/compass-web/scripts/ws-proxy.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,6 @@ const net = require('net');
33
const tls = require('tls');
44
const { WebSocketServer } = require('ws');
55

6-
function serializeError(err) {
7-
if (err) {
8-
return {
9-
name: err.name,
10-
message: err.message,
11-
};
12-
}
13-
}
14-
156
/**
167
* Creates a simple passthrough proxy that accepts a websocket connection and
178
* establishes a corresponding socket connection to a server. The connection
@@ -60,7 +51,7 @@ function createWebSocketProxy(port = 1337, logger = console) {
6051
SOCKET_ERROR_EVENT_LIST.forEach((evt) => {
6152
socket.on(evt, (err) => {
6253
logger.log('server socket error event (%s)', evt, err);
63-
ws.send(JSON.stringify({ evt, error: serializeError(err) }));
54+
ws.close(evt === 'close' ? 1001 : 1011);
6455
});
6556
});
6657
socket.on(connectEvent, () => {

packages/compass-web/src/connection-storage.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe('buildConnectionInfoFromClusterDescription', function () {
5252
},
5353
],
5454
},
55-
'mongodb+srv://replicaSet.mongodb.com/?tls=true&authMechanism=MONGODB-X509&authSource=%24external&maxPoolSize=3',
55+
'mongodb+srv://replicaSet.mongodb.com/?tls=true&authMechanism=MONGODB-X509&authSource=%24external&serverMonitoringMode=poll&maxIdleTimeMS=30000&minPoolSize=0&maxPoolSize=5',
5656
],
5757
[
5858
'sharded',
@@ -90,7 +90,7 @@ describe('buildConnectionInfoFromClusterDescription', function () {
9090
},
9191
],
9292
},
93-
'mongodb+srv://sharded.mongodb.com/?tls=true&authMechanism=MONGODB-X509&authSource=%24external&maxPoolSize=3&srvMaxHosts=3',
93+
'mongodb+srv://sharded.mongodb.com/?tls=true&authMechanism=MONGODB-X509&authSource=%24external&serverMonitoringMode=poll&maxIdleTimeMS=30000&minPoolSize=0&maxPoolSize=5&srvMaxHosts=1',
9494
],
9595
[
9696
'serverless',
@@ -119,7 +119,7 @@ describe('buildConnectionInfoFromClusterDescription', function () {
119119
},
120120
],
121121
},
122-
'mongodb+srv://serverless.mongodb.com/?tls=true&authMechanism=MONGODB-X509&authSource=%24external&maxPoolSize=3',
122+
'mongodb+srv://serverless.mongodb.com/?tls=true&authMechanism=MONGODB-X509&authSource=%24external&serverMonitoringMode=poll&maxIdleTimeMS=30000&minPoolSize=0&maxPoolSize=5',
123123
],
124124
];
125125

packages/compass-web/src/connection-storage.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,16 @@ export function buildConnectionInfoFromClusterDescription(
168168
connectionString.searchParams.set('authMechanism', 'MONGODB-X509');
169169
connectionString.searchParams.set('authSource', '$external');
170170

171+
// Make sure server monitoring is done without streaming
172+
connectionString.searchParams.set('serverMonitoringMode', 'poll');
173+
// Allow driver to clean up idle connections from the pool
174+
connectionString.searchParams.set('maxIdleTimeMS', '30000');
175+
171176
// Limit connection pool for replicas and sharded
172-
connectionString.searchParams.set('maxPoolSize', '3');
177+
connectionString.searchParams.set('minPoolSize', '0');
178+
connectionString.searchParams.set('maxPoolSize', '5');
173179
if (isSharded(description)) {
174-
connectionString.searchParams.set('srvMaxHosts', '3');
180+
connectionString.searchParams.set('srvMaxHosts', '1');
175181
}
176182

177183
for (const [key, value] of Object.entries(extraConnectionOptions ?? {})) {

0 commit comments

Comments
 (0)