From d1547ce95c596012a4139f34c97479846e986c6f Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Tue, 4 Apr 2023 12:24:06 +0200 Subject: [PATCH 1/3] feat(NODE-3607): update tcp keepalive options --- src/cmap/connect.ts | 9 ++++----- src/cmap/connection.ts | 2 ++ src/connection_string.ts | 4 +++- src/mongo_client.ts | 7 +++++-- test/unit/mongo_client.test.js | 2 +- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/cmap/connect.ts b/src/cmap/connect.ts index 7b2866adf2..f4e6a6b0f6 100644 --- a/src/cmap/connect.ts +++ b/src/cmap/connect.ts @@ -329,14 +329,13 @@ const SOCKET_ERROR_EVENTS = new Set(SOCKET_ERROR_EVENT_LIST); function makeConnection(options: MakeConnectionOptions, _callback: Callback) { const useTLS = options.tls ?? false; const keepAlive = options.keepAlive ?? true; - const socketTimeoutMS = options.socketTimeoutMS ?? Reflect.get(options, 'socketTimeout') ?? 0; const noDelay = options.noDelay ?? true; const connectTimeoutMS = options.connectTimeoutMS ?? 30000; const rejectUnauthorized = options.rejectUnauthorized ?? true; - const keepAliveInitialDelay = - ((options.keepAliveInitialDelay ?? 120000) > socketTimeoutMS - ? Math.round(socketTimeoutMS / 2) - : options.keepAliveInitialDelay) ?? 120000; + // Default to delay to 300 seconds. Node automatically then sets TCP_KEEPINTVL to 1 second + // which is acceptable to the recommendation of 10 seconds and also cannot be configured. + // TCP_KEEPCNT is also set to 10 in Node and cannot be configured. (Recommendation is 9) + const keepAliveInitialDelay = options.keepAliveInitialDelay || 300000; const existingSocket = options.existingSocket; let socket: Stream; diff --git a/src/cmap/connection.ts b/src/cmap/connection.ts index 464f86d9b3..ad49356620 100644 --- a/src/cmap/connection.ts +++ b/src/cmap/connection.ts @@ -122,7 +122,9 @@ export interface ConnectionOptions credentials?: MongoCredentials; connectTimeoutMS?: number; tls: boolean; + /** @deprecated - Will not be able to turn off in the future. */ keepAlive?: boolean; + /** @deprecated - Will not be configurable in the future. */ keepAliveInitialDelay?: number; noDelay?: boolean; socketTimeoutMS?: number; diff --git a/src/connection_string.ts b/src/connection_string.ts index 9b55437273..6f7c69b2e6 100644 --- a/src/connection_string.ts +++ b/src/connection_string.ts @@ -864,12 +864,14 @@ export const OPTIONS = { return wc; } }, + /** @deprecated - Will not be able to turn off in the future. */ keepAlive: { default: true, type: 'boolean' }, + /** @deprecated - Will not be configurable in the future. */ keepAliveInitialDelay: { - default: 120000, + default: 300000, type: 'uint' }, loadBalanced: { diff --git a/src/mongo_client.ts b/src/mongo_client.ts index 885c980fbf..562a41c36f 100644 --- a/src/mongo_client.ts +++ b/src/mongo_client.ts @@ -211,9 +211,12 @@ export interface MongoClientOptions extends BSONSerializeOptions, SupportedNodeC sslCRL?: string; /** TCP Connection no delay */ noDelay?: boolean; - /** TCP Connection keep alive enabled */ + /** @deprecated TCP Connection keep alive enabled. Will not be able to turn off in the future. */ keepAlive?: boolean; - /** The number of milliseconds to wait before initiating keepAlive on the TCP socket */ + /** + * @deprecated The number of milliseconds to wait before initiating keepAlive on the TCP socket. + * Will not be configurable in the future. + */ keepAliveInitialDelay?: number; /** Force server to assign `_id` values instead of driver */ forceServerObjectId?: boolean; diff --git a/test/unit/mongo_client.test.js b/test/unit/mongo_client.test.js index 1ff539aaf3..7e553cd462 100644 --- a/test/unit/mongo_client.test.js +++ b/test/unit/mongo_client.test.js @@ -610,7 +610,7 @@ describe('MongoOptions', function () { ['forceserverobjectid', false], ['heartbeatfrequencyms', 10000], ['keepalive', true], - ['keepaliveinitialdelay', 120000], + ['keepaliveinitialdelay', 300000], ['localthresholdms', 15], ['maxidletimems', 0], ['maxpoolsize', 100], From 47c81d824f1ab595ea41bc1dfcf528ce24e5f616 Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Wed, 12 Apr 2023 12:46:29 +0200 Subject: [PATCH 2/3] chore: updating per comments --- src/cmap/connect.ts | 2 +- src/connection_string.ts | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/cmap/connect.ts b/src/cmap/connect.ts index f4e6a6b0f6..4593d096a0 100644 --- a/src/cmap/connect.ts +++ b/src/cmap/connect.ts @@ -335,7 +335,7 @@ function makeConnection(options: MakeConnectionOptions, _callback: Callback Date: Thu, 13 Apr 2023 14:17:46 +0200 Subject: [PATCH 3/3] fix: remove changes outside deprecations --- src/cmap/connect.ts | 9 +++++---- src/connection_string.ts | 2 +- test/unit/mongo_client.test.js | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/cmap/connect.ts b/src/cmap/connect.ts index 4593d096a0..7b2866adf2 100644 --- a/src/cmap/connect.ts +++ b/src/cmap/connect.ts @@ -329,13 +329,14 @@ const SOCKET_ERROR_EVENTS = new Set(SOCKET_ERROR_EVENT_LIST); function makeConnection(options: MakeConnectionOptions, _callback: Callback) { const useTLS = options.tls ?? false; const keepAlive = options.keepAlive ?? true; + const socketTimeoutMS = options.socketTimeoutMS ?? Reflect.get(options, 'socketTimeout') ?? 0; const noDelay = options.noDelay ?? true; const connectTimeoutMS = options.connectTimeoutMS ?? 30000; const rejectUnauthorized = options.rejectUnauthorized ?? true; - // Default to delay to 300 seconds. Node automatically then sets TCP_KEEPINTVL to 1 second - // which is acceptable to the recommendation of 10 seconds and also cannot be configured. - // TCP_KEEPCNT is also set to 10 in Node and cannot be configured. (Recommendation is 9) - const keepAliveInitialDelay = options.keepAliveInitialDelay; + const keepAliveInitialDelay = + ((options.keepAliveInitialDelay ?? 120000) > socketTimeoutMS + ? Math.round(socketTimeoutMS / 2) + : options.keepAliveInitialDelay) ?? 120000; const existingSocket = options.existingSocket; let socket: Stream; diff --git a/src/connection_string.ts b/src/connection_string.ts index def9ec637c..852120014e 100644 --- a/src/connection_string.ts +++ b/src/connection_string.ts @@ -870,7 +870,7 @@ export const OPTIONS = { deprecated: 'Will not be able to turn off in the future.' }, keepAliveInitialDelay: { - default: 300000, + default: 120000, type: 'uint', deprecated: 'Will not be configurable in the future.' }, diff --git a/test/unit/mongo_client.test.js b/test/unit/mongo_client.test.js index 7e553cd462..1ff539aaf3 100644 --- a/test/unit/mongo_client.test.js +++ b/test/unit/mongo_client.test.js @@ -610,7 +610,7 @@ describe('MongoOptions', function () { ['forceserverobjectid', false], ['heartbeatfrequencyms', 10000], ['keepalive', true], - ['keepaliveinitialdelay', 300000], + ['keepaliveinitialdelay', 120000], ['localthresholdms', 15], ['maxidletimems', 0], ['maxpoolsize', 100],