Skip to content

Commit dd92556

Browse files
committed
fix: 🔥 Change socket timeout default to 0
Socket timeout by default is infinity. NODE-2835
1 parent 4955a52 commit dd92556

File tree

11 files changed

+20
-18
lines changed

11 files changed

+20
-18
lines changed

lib/cmap/connection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Connection extends EventEmitter {
3232
this.id = options.id;
3333
this.address = streamIdentifier(stream);
3434
this.bson = options.bson;
35-
this.socketTimeout = typeof options.socketTimeout === 'number' ? options.socketTimeout : 360000;
35+
this.socketTimeout = typeof options.socketTimeout === 'number' ? options.socketTimeout : 0;
3636
this.host = options.host || 'localhost';
3737
this.port = options.port || 27017;
3838
this.monitorCommands =

lib/core/connection/connect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ function makeConnection(family, options, cancellationToken, _callback) {
263263
: typeof options.connectTimeoutMS === 'number'
264264
? options.connectTimeoutMS
265265
: 30000;
266-
const socketTimeout = typeof options.socketTimeout === 'number' ? options.socketTimeout : 360000;
266+
const socketTimeout = typeof options.socketTimeout === 'number' ? options.socketTimeout : 0;
267267
const rejectUnauthorized =
268268
typeof options.rejectUnauthorized === 'boolean' ? options.rejectUnauthorized : true;
269269

lib/core/connection/connection.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class Connection extends EventEmitter {
6969
* @param {boolean} [options.keepAlive=true] TCP Connection keep alive enabled
7070
* @param {number} [options.keepAliveInitialDelay=120000] Initial delay before TCP keep alive enabled
7171
* @param {number} [options.connectionTimeout=30000] TCP Connection timeout setting
72-
* @param {number} [options.socketTimeout=360000] TCP Socket timeout setting
72+
* @param {number} [options.socketTimeout=0] TCP Socket timeout setting
7373
* @param {boolean} [options.promoteLongs] Convert Long values from the db into Numbers if they fit into 53 bits
7474
* @param {boolean} [options.promoteValues] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
7575
* @param {boolean} [options.promoteBuffers] Promotes Binary BSON values to native Node Buffers.
@@ -92,7 +92,7 @@ class Connection extends EventEmitter {
9292

9393
this.port = options.port || 27017;
9494
this.host = options.host || 'localhost';
95-
this.socketTimeout = typeof options.socketTimeout === 'number' ? options.socketTimeout : 360000;
95+
this.socketTimeout = typeof options.socketTimeout === 'number' ? options.socketTimeout : 0;
9696

9797
// These values are inspected directly in tests, but maybe not necessary to keep around
9898
this.keepAlive = typeof options.keepAlive === 'boolean' ? options.keepAlive : true;
@@ -316,8 +316,7 @@ class Connection extends EventEmitter {
316316
if (typeof options === 'function') (callback = options), (options = {});
317317

318318
const conn = this;
319-
const socketTimeout =
320-
typeof options.socketTimeout === 'number' ? options.socketTimeout : 360000;
319+
const socketTimeout = typeof options.socketTimeout === 'number' ? options.socketTimeout : 0;
321320
const bson = conn.options.bson;
322321
const query = new Query(bson, ns, command, {
323322
numberToSkip: 0,

lib/core/connection/pool.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ var _id = 0;
6363
* @param {number} [options.keepAliveInitialDelay=120000] Initial delay before TCP keep alive enabled
6464
* @param {boolean} [options.noDelay=true] TCP Connection no delay
6565
* @param {number} [options.connectionTimeout=30000] TCP Connection timeout setting
66-
* @param {number} [options.socketTimeout=360000] TCP Socket timeout setting
67-
* @param {number} [options.monitoringSocketTimeout=30000] TCP Socket timeout setting for replicaset monitoring socket
66+
* @param {number} [options.socketTimeout=0] TCP Socket timeout setting
67+
* @param {number} [options.monitoringSocketTimeout=0] TCP Socket timeout setting for replicaset monitoring socket
6868
* @param {boolean} [options.ssl=false] Use SSL for connection
6969
* @param {boolean|function} [options.checkServerIdentity=true] Ensure we check server identify during SSL, set to false to disable checking. Only works for Node 0.12.x or higher. You can pass in a boolean or your own checkServerIdentity override function.
7070
* @param {Buffer} [options.ca] SSL Certificate store binary buffer
@@ -111,7 +111,7 @@ var Pool = function(topology, options) {
111111
minSize: 0,
112112
// socket settings
113113
connectionTimeout: 30000,
114-
socketTimeout: 360000,
114+
socketTimeout: 0,
115115
keepAlive: true,
116116
keepAliveInitialDelay: 120000,
117117
noDelay: true,

lib/core/topologies/replset.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ ReplSet.prototype.connect = function(options) {
919919
);
920920
});
921921

922-
// Error out as high availbility interval must be < than socketTimeout
922+
// Error out as high availability interval must be < than socketTimeout
923923
if (
924924
this.s.options.socketTimeout > 0 &&
925925
this.s.options.socketTimeout <= this.s.options.haInterval

lib/core/topologies/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function topologyId(server) {
7575
* @param {number} [options.keepAliveInitialDelay=120000] Initial delay before TCP keep alive enabled
7676
* @param {boolean} [options.noDelay=true] TCP Connection no delay
7777
* @param {number} [options.connectionTimeout=30000] TCP Connection timeout setting
78-
* @param {number} [options.socketTimeout=360000] TCP Socket timeout setting
78+
* @param {number} [options.socketTimeout=0] TCP Socket timeout setting
7979
* @param {boolean} [options.ssl=false] Use SSL for connection
8080
* @param {boolean|function} [options.checkServerIdentity=true] Ensure we check server identify during SSL, set to false to disable checking. Only works for Node 0.12.x or higher. You can pass in a boolean or your own checkServerIdentity override function.
8181
* @param {Buffer} [options.ca] SSL Certificate store binary buffer

lib/mongo_client.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ const validOptions = require('./operations/connect').validOptions;
9797
* @param {boolean} [options.keepAlive=true] TCP Connection keep alive enabled
9898
* @param {number} [options.keepAliveInitialDelay=120000] The number of milliseconds to wait before initiating keepAlive on the TCP socket
9999
* @param {number} [options.connectTimeoutMS=10000] How long to wait for a connection to be established before timing out
100-
* @param {number} [options.socketTimeoutMS=360000] How long a send or receive on a socket can take before timing out
100+
* @param {number} [options.socketTimeoutMS=0] How long a send or receive on a socket can take before timing out
101101
* @param {number} [options.family] Version of IP stack. Can be 4, 6 or null (default).
102102
* If null, will attempt to connect with IPv6, and will fall back to IPv4 on failure
103103
* @param {number} [options.reconnectTries=30] Server attempt to reconnect #times
@@ -371,7 +371,7 @@ MongoClient.prototype.isConnected = function(options) {
371371
* @param {boolean} [options.keepAlive=true] TCP Connection keep alive enabled
372372
* @param {number} [options.keepAliveInitialDelay=120000] The number of milliseconds to wait before initiating keepAlive on the TCP socket
373373
* @param {number} [options.connectTimeoutMS=10000] How long to wait for a connection to be established before timing out
374-
* @param {number} [options.socketTimeoutMS=360000] How long a send or receive on a socket can take before timing out
374+
* @param {number} [options.socketTimeoutMS=0] How long a send or receive on a socket can take before timing out
375375
* @param {number} [options.family] Version of IP stack. Can be 4, 6 or null (default).
376376
* If null, will attempt to connect with IPv6, and will fall back to IPv4 on failure
377377
* @param {number} [options.reconnectTries=30] Server attempt to reconnect #times

lib/operations/connect.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ function connect(mongoClient, url, options, callback) {
290290
const _finalOptions = createUnifiedOptions(object, options);
291291

292292
// Check if we have connection and socket timeout set
293-
if (_finalOptions.socketTimeoutMS == null) _finalOptions.socketTimeoutMS = 360000;
293+
if (_finalOptions.socketTimeoutMS == null) _finalOptions.socketTimeoutMS = 0;
294294
if (_finalOptions.connectTimeoutMS == null) _finalOptions.connectTimeoutMS = 10000;
295295
if (_finalOptions.retryWrites == null) _finalOptions.retryWrites = true;
296296
if (_finalOptions.useRecoveryToken == null) _finalOptions.useRecoveryToken = true;
@@ -788,7 +788,7 @@ function translateOptions(options, translationOptions) {
788788
}
789789

790790
// Set the socket and connection timeouts
791-
if (options.socketTimeoutMS == null) options.socketTimeoutMS = 360000;
791+
if (options.socketTimeoutMS == null) options.socketTimeoutMS = 0;
792792
if (options.connectTimeoutMS == null) options.connectTimeoutMS = 10000;
793793

794794
if (!translationOptions.createServers) {

lib/topologies/mongos.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ var legalOptionNames = [
8484
* @param {boolean} [options.socketOptions.keepAlive=true] TCP Connection keep alive enabled
8585
* @param {number} [options.socketOptions.keepAliveInitialDelay=120000] The number of milliseconds to wait before initiating keepAlive on the TCP socket
8686
* @param {number} [options.socketOptions.connectTimeoutMS=10000] How long to wait for a connection to be established before timing out
87-
* @param {number} [options.socketOptions.socketTimeoutMS=360000] How long a send or receive on a socket can take before timing out
87+
* @param {number} [options.socketOptions.socketTimeoutMS=0] How long a send or receive on a socket can take before timing out
8888
* @param {boolean} [options.domainsEnabled=false] Enable the wrapping of the callback in the current domain, disabled by default to avoid perf hit.
8989
* @param {boolean} [options.monitorCommands=false] Enable command monitoring for this topology
9090
* @fires Mongos#connect

lib/topologies/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ var legalOptionNames = [
8686
* @param {boolean} [options.socketOptions.keepAlive=true] TCP Connection keep alive enabled
8787
* @param {number} [options.socketOptions.keepAliveInitialDelay=120000] The number of milliseconds to wait before initiating keepAlive on the TCP socket
8888
* @param {number} [options.socketOptions.connectTimeoutMS=10000] How long to wait for a connection to be established before timing out
89-
* @param {number} [options.socketOptions.socketTimeoutMS=360000] How long a send or receive on a socket can take before timing out
89+
* @param {number} [options.socketOptions.socketTimeoutMS=0] How long a send or receive on a socket can take before timing out
9090
* @param {number} [options.reconnectTries=30] Server attempt to reconnect #times
9191
* @param {number} [options.reconnectInterval=1000] Server will wait # milliseconds between retries
9292
* @param {boolean} [options.monitoring=true] Triggers the server instance to call ismaster

test/functional/mongo_client.test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,10 @@ describe('MongoClient', function() {
517517
{},
518518
{
519519
keepAlive: true,
520-
keepAliveInitialDelay: 100
520+
keepAliveInitialDelay: 100,
521+
// keepAliveInitialDelay is clamped to half the size of socketTimeout
522+
// if socketTimeout is less than keepAliveInitialDelay
523+
socketTimeout: 101
521524
}
522525
);
523526

0 commit comments

Comments
 (0)