Skip to content

Commit f608f44

Browse files
authored
feat(keepAlive): make keepAlive options consistent (#1612)
keepAlive options are now consistent with socket / node api. keepAlive is a boolean that enables / disables keepAlive, and keepAliveInitialDelay is the number of milliseconds to wait before initiating keepAlive. BREAKING CHANGE: option `keepAlive` is now split into boolean `keepAlive` and number `keepAliveInitialDelay` Fixes NODE-998
1 parent e006725 commit f608f44

File tree

7 files changed

+26
-21
lines changed

7 files changed

+26
-21
lines changed

CHANGES_3.0.0.md

+4
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ testCollection.updateOne({_id: 'test'}, {});
198198
// An error is returned: The update operation document must contain at least one atomic operator.
199199
```
200200

201+
### `keepAlive`
202+
203+
Wherever it occurs, the option `keepAlive` has been changed. `keepAlive` is now a boolean that enables/disables `keepAlive`, while `keepAliveInitialDelay` specifies how long to wait before initiating keepAlive. This brings the API in line with [NodeJS's socket api](https://nodejs.org/dist/latest-v9.x/docs/api/all.html#net_socket_setkeepalive_enable_initialdelay)
204+
201205
### Tests
202206

203207
We have updated all of the tests to use [Mocha](https://mochajs.org) and a new test runner, [`mongodb-test-runner`](https://github.com./mongodb-js/mongodb-test-runner), which

lib/topologies/mongos.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ var legalOptionNames = [
5858
'autoReconnect',
5959
'emitError',
6060
'keepAlive',
61+
'keepAliveInitialDelay',
6162
'noDelay',
6263
'connectTimeoutMS',
6364
'socketTimeoutMS',
@@ -96,7 +97,8 @@ var legalOptionNames = [
9697
* @param {string} [options.servername=null] String containing the server name requested via TLS SNI.
9798
* @param {object} [options.socketOptions=null] Socket options
9899
* @param {boolean} [options.socketOptions.noDelay=true] TCP Socket NoDelay option.
99-
* @param {number} [options.socketOptions.keepAlive=0] TCP KeepAlive on the socket with a X ms delay before start.
100+
* @param {boolean} [options.socketOptions.keepAlive=true] TCP Connection keep alive enabled
101+
* @param {number} [options.socketOptions.keepAliveInitialDelay=30000] The number of milliseconds to wait before initiating keepAlive on the TCP socket
100102
* @param {number} [options.socketOptions.connectTimeoutMS=0] TCP Connection timeout setting
101103
* @param {number} [options.socketOptions.socketTimeoutMS=0] TCP Socket timeout setting
102104
* @param {boolean} [options.domainsEnabled=false] Enable the wrapping of the callback in the current domain, disabled by default to avoid perf hit.
@@ -175,10 +177,6 @@ class Mongos extends TopologyBase {
175177

176178
// Translate all the options to the mongodb-core ones
177179
clonedOptions = translateOptions(clonedOptions, socketOptions);
178-
if (typeof clonedOptions.keepAlive === 'number') {
179-
clonedOptions.keepAliveInitialDelay = clonedOptions.keepAlive;
180-
clonedOptions.keepAlive = clonedOptions.keepAlive > 0;
181-
}
182180

183181
// Build default client information
184182
clonedOptions.clientInfo = this.clientInfo;

lib/topologies/replset.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ var legalOptionNames = [
6161
'autoReconnect',
6262
'emitError',
6363
'keepAlive',
64+
'keepAliveInitialDelay',
6465
'noDelay',
6566
'connectTimeoutMS',
6667
'socketTimeoutMS',
@@ -106,7 +107,8 @@ var legalOptionNames = [
106107
* @param {string} [options.servername=null] String containing the server name requested via TLS SNI.
107108
* @param {object} [options.socketOptions=null] Socket options
108109
* @param {boolean} [options.socketOptions.noDelay=true] TCP Socket NoDelay option.
109-
* @param {number} [options.socketOptions.keepAlive=0] TCP KeepAlive on the socket with a X ms delay before start.
110+
* @param {boolean} [options.socketOptions.keepAlive=true] TCP Connection keep alive enabled
111+
* @param {number} [options.socketOptions.keepAliveInitialDelay=30000] The number of milliseconds to wait before initiating keepAlive on the TCP socket
110112
* @param {number} [options.socketOptions.connectTimeoutMS=10000] TCP Connection timeout setting
111113
* @param {number} [options.socketOptions.socketTimeoutMS=0] TCP Socket timeout setting
112114
* @param {boolean} [options.domainsEnabled=false] Enable the wrapping of the callback in the current domain, disabled by default to avoid perf hit.
@@ -182,10 +184,6 @@ class ReplSet extends TopologyBase {
182184

183185
// Translate all the options to the mongodb-core ones
184186
clonedOptions = translateOptions(clonedOptions, socketOptions);
185-
if (typeof clonedOptions.keepAlive === 'number') {
186-
clonedOptions.keepAliveInitialDelay = clonedOptions.keepAlive;
187-
clonedOptions.keepAlive = clonedOptions.keepAlive > 0;
188-
}
189187

190188
// Build default client information
191189
clonedOptions.clientInfo = this.clientInfo;

lib/topologies/server.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ var legalOptionNames = [
5555
'autoReconnect',
5656
'emitError',
5757
'keepAlive',
58+
'keepAliveInitialDelay',
5859
'noDelay',
5960
'connectTimeoutMS',
6061
'socketTimeoutMS',
@@ -96,7 +97,8 @@ var legalOptionNames = [
9697
* @param {object} [options.socketOptions=null] Socket options
9798
* @param {boolean} [options.socketOptions.autoReconnect=true] Reconnect on error.
9899
* @param {boolean} [options.socketOptions.noDelay=true] TCP Socket NoDelay option.
99-
* @param {number} [options.socketOptions.keepAlive=0] TCP KeepAlive on the socket with a X ms delay before start.
100+
* @param {boolean} [options.socketOptions.keepAlive=true] TCP Connection keep alive enabled
101+
* @param {number} [options.socketOptions.keepAliveInitialDelay=30000] The number of milliseconds to wait before initiating keepAlive on the TCP socket
100102
* @param {number} [options.socketOptions.connectTimeoutMS=0] TCP Connection timeout setting
101103
* @param {number} [options.socketOptions.socketTimeoutMS=0] TCP Socket timeout setting
102104
* @param {number} [options.reconnectTries=30] Server attempt to reconnect #times
@@ -173,10 +175,6 @@ class Server extends TopologyBase {
173175

174176
// Translate all the options to the mongodb-core ones
175177
clonedOptions = translateOptions(clonedOptions, socketOptions);
176-
if (typeof clonedOptions.keepAlive === 'number') {
177-
clonedOptions.keepAliveInitialDelay = clonedOptions.keepAlive;
178-
clonedOptions.keepAlive = clonedOptions.keepAlive > 0;
179-
}
180178

181179
// Build default client information
182180
clonedOptions.clientInfo = this.clientInfo;

test/functional/mongo_client_tests.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ describe('MongoClient', function() {
181181
poolSize: 10,
182182
autoReconnect: false,
183183
noDelay: false,
184-
keepAlive: 100,
184+
keepAlive: true,
185+
keepAliveInitialDelay: 100,
185186
connectTimeoutMS: 444444,
186187
socketTimeoutMS: 555555
187188
},
@@ -226,7 +227,8 @@ describe('MongoClient', function() {
226227
poolSize: 1,
227228
socketOptions: {
228229
noDelay: false,
229-
keepAlive: 100,
230+
keepAlive: true,
231+
keepAliveInitialDelay: 100,
230232
connectTimeoutMS: 444444,
231233
socketTimeoutMS: 555555
232234
}
@@ -275,7 +277,8 @@ describe('MongoClient', function() {
275277
poolSize: 1,
276278
socketOptions: {
277279
noDelay: false,
278-
keepAlive: 100,
280+
keepAlive: true,
281+
keepAliveInitialDelay: 100,
279282
connectTimeoutMS: 444444,
280283
socketTimeoutMS: 555555
281284
}
@@ -500,7 +503,8 @@ describe('MongoClient', function() {
500503
MongoClient.connect(
501504
configuration.url(),
502505
{
503-
keepAlive: 100
506+
keepAlive: true,
507+
keepAliveInitialDelay: 100
504508
},
505509
function(err, client) {
506510
test.equal(null, err);

test/functional/reconnect_tests.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe('Reconnect', function() {
6161
db: { native_parser: true, bufferMaxEntries: -1 },
6262
server: {
6363
poolSize: 20,
64-
socketOptions: { autoReconnect: true, keepAlive: 50 },
64+
socketOptions: { autoReconnect: true, keepAlive: true, keepAliveInitialDelay: 50 },
6565
reconnectTries: 1000,
6666
reconnectInterval: 1000
6767
}

test/functional/replset_connection_tests.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,10 @@ describe('ReplSet (Connection)', function() {
201201
new Server(configuration.host, configuration.port + 1),
202202
new Server(configuration.host, configuration.port + 2)
203203
],
204-
{ socketOptions: { keepAlive: 100 }, rs_name: configuration.replicasetName }
204+
{
205+
socketOptions: { keepAlive: true, keepAliveInitialDelay: 100 },
206+
rs_name: configuration.replicasetName
207+
}
205208
);
206209

207210
var client = new MongoClient(replSet, { w: 0 });

0 commit comments

Comments
 (0)