Skip to content

Commit d1547ce

Browse files
committed
feat(NODE-3607): update tcp keepalive options
1 parent 30d2461 commit d1547ce

File tree

5 files changed

+15
-9
lines changed

5 files changed

+15
-9
lines changed

src/cmap/connect.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,14 +329,13 @@ const SOCKET_ERROR_EVENTS = new Set(SOCKET_ERROR_EVENT_LIST);
329329
function makeConnection(options: MakeConnectionOptions, _callback: Callback<Stream>) {
330330
const useTLS = options.tls ?? false;
331331
const keepAlive = options.keepAlive ?? true;
332-
const socketTimeoutMS = options.socketTimeoutMS ?? Reflect.get(options, 'socketTimeout') ?? 0;
333332
const noDelay = options.noDelay ?? true;
334333
const connectTimeoutMS = options.connectTimeoutMS ?? 30000;
335334
const rejectUnauthorized = options.rejectUnauthorized ?? true;
336-
const keepAliveInitialDelay =
337-
((options.keepAliveInitialDelay ?? 120000) > socketTimeoutMS
338-
? Math.round(socketTimeoutMS / 2)
339-
: options.keepAliveInitialDelay) ?? 120000;
335+
// Default to delay to 300 seconds. Node automatically then sets TCP_KEEPINTVL to 1 second
336+
// which is acceptable to the recommendation of 10 seconds and also cannot be configured.
337+
// TCP_KEEPCNT is also set to 10 in Node and cannot be configured. (Recommendation is 9)
338+
const keepAliveInitialDelay = options.keepAliveInitialDelay || 300000;
340339
const existingSocket = options.existingSocket;
341340

342341
let socket: Stream;

src/cmap/connection.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ export interface ConnectionOptions
122122
credentials?: MongoCredentials;
123123
connectTimeoutMS?: number;
124124
tls: boolean;
125+
/** @deprecated - Will not be able to turn off in the future. */
125126
keepAlive?: boolean;
127+
/** @deprecated - Will not be configurable in the future. */
126128
keepAliveInitialDelay?: number;
127129
noDelay?: boolean;
128130
socketTimeoutMS?: number;

src/connection_string.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,12 +864,14 @@ export const OPTIONS = {
864864
return wc;
865865
}
866866
},
867+
/** @deprecated - Will not be able to turn off in the future. */
867868
keepAlive: {
868869
default: true,
869870
type: 'boolean'
870871
},
872+
/** @deprecated - Will not be configurable in the future. */
871873
keepAliveInitialDelay: {
872-
default: 120000,
874+
default: 300000,
873875
type: 'uint'
874876
},
875877
loadBalanced: {

src/mongo_client.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,12 @@ export interface MongoClientOptions extends BSONSerializeOptions, SupportedNodeC
211211
sslCRL?: string;
212212
/** TCP Connection no delay */
213213
noDelay?: boolean;
214-
/** TCP Connection keep alive enabled */
214+
/** @deprecated TCP Connection keep alive enabled. Will not be able to turn off in the future. */
215215
keepAlive?: boolean;
216-
/** The number of milliseconds to wait before initiating keepAlive on the TCP socket */
216+
/**
217+
* @deprecated The number of milliseconds to wait before initiating keepAlive on the TCP socket.
218+
* Will not be configurable in the future.
219+
*/
217220
keepAliveInitialDelay?: number;
218221
/** Force server to assign `_id` values instead of driver */
219222
forceServerObjectId?: boolean;

test/unit/mongo_client.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ describe('MongoOptions', function () {
610610
['forceserverobjectid', false],
611611
['heartbeatfrequencyms', 10000],
612612
['keepalive', true],
613-
['keepaliveinitialdelay', 120000],
613+
['keepaliveinitialdelay', 300000],
614614
['localthresholdms', 15],
615615
['maxidletimems', 0],
616616
['maxpoolsize', 100],

0 commit comments

Comments
 (0)