Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
feat(pool): introduce the concept of a minimum pool size
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Dec 1, 2017
1 parent e7c2242 commit b01b1f8
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion lib/connection/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}
};
}

Expand Down Expand Up @@ -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);
});
Expand Down

0 comments on commit b01b1f8

Please sign in to comment.