diff --git a/lib/connection/pool.js b/lib/connection/pool.js index 5bc4b52b7..f6e215484 100644 --- a/lib/connection/pool.js +++ b/lib/connection/pool.js @@ -42,7 +42,8 @@ function hasSessionSupport(topology) { * @class * @param {string} options.host The server host * @param {number} options.port The server port - * @param {number} [options.size=1] Max server connection pool size + * @param {number} [options.size=5] Max server connection pool size + * @param {number} [options.minSize=0] Minimum server connection pool size * @param {boolean} [options.reconnect=true] Server will attempt to reconnect on loss of connection * @param {number} [options.reconnectTries=30] Server attempt to reconnect #times * @param {number} [options.reconnectInterval=1000] Server will wait # milliseconds between retries @@ -86,6 +87,8 @@ var Pool = function(topology, options) { port: 27017, // Pool default max size size: 5, + // Pool default min size + minSize: 0, // socket settings connectionTimeout: 30000, socketTimeout: 360000, @@ -175,6 +178,13 @@ Object.defineProperty(Pool.prototype, 'size', { } }); +Object.defineProperty(Pool.prototype, 'minSize', { + enumerable: true, + get: function() { + return this.options.minSize; + } +}); + Object.defineProperty(Pool.prototype, 'connectionTimeout', { enumerable: true, get: function() { @@ -321,6 +331,16 @@ function connectionFailureHandler(self, event) { if (!self.reconnectId && self.options.reconnect) { self.reconnectId = setTimeout(attemptReconnect(self), self.options.reconnectInterval); } + + // Do we need to do anything to maintain the minimum pool size + const totalConnections = + self.availableConnections.length + + self.connectingConnections.length + + self.inUseConnections.length; + + if (totalConnections < self.minSize) { + _createConnection(self); + } }; } @@ -734,6 +754,11 @@ Pool.prototype.connect = function() { // Move the active connection moveConnectionBetween(connection, self.connectingConnections, self.availableConnections); + // if we have a minPoolSize, create a connection + if (self.minSize) { + for (let i = 0; i < self.minSize; i++) _createConnection(self); + } + // Emit the connect event self.emit('connect', self); });