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

Commit b01b1f8

Browse files
committed
feat(pool): introduce the concept of a minimum pool size
1 parent e7c2242 commit b01b1f8

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

lib/connection/pool.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ function hasSessionSupport(topology) {
4242
* @class
4343
* @param {string} options.host The server host
4444
* @param {number} options.port The server port
45-
* @param {number} [options.size=1] Max server connection pool size
45+
* @param {number} [options.size=5] Max server connection pool size
46+
* @param {number} [options.minSize=0] Minimum server connection pool size
4647
* @param {boolean} [options.reconnect=true] Server will attempt to reconnect on loss of connection
4748
* @param {number} [options.reconnectTries=30] Server attempt to reconnect #times
4849
* @param {number} [options.reconnectInterval=1000] Server will wait # milliseconds between retries
@@ -86,6 +87,8 @@ var Pool = function(topology, options) {
8687
port: 27017,
8788
// Pool default max size
8889
size: 5,
90+
// Pool default min size
91+
minSize: 0,
8992
// socket settings
9093
connectionTimeout: 30000,
9194
socketTimeout: 360000,
@@ -175,6 +178,13 @@ Object.defineProperty(Pool.prototype, 'size', {
175178
}
176179
});
177180

181+
Object.defineProperty(Pool.prototype, 'minSize', {
182+
enumerable: true,
183+
get: function() {
184+
return this.options.minSize;
185+
}
186+
});
187+
178188
Object.defineProperty(Pool.prototype, 'connectionTimeout', {
179189
enumerable: true,
180190
get: function() {
@@ -321,6 +331,16 @@ function connectionFailureHandler(self, event) {
321331
if (!self.reconnectId && self.options.reconnect) {
322332
self.reconnectId = setTimeout(attemptReconnect(self), self.options.reconnectInterval);
323333
}
334+
335+
// Do we need to do anything to maintain the minimum pool size
336+
const totalConnections =
337+
self.availableConnections.length +
338+
self.connectingConnections.length +
339+
self.inUseConnections.length;
340+
341+
if (totalConnections < self.minSize) {
342+
_createConnection(self);
343+
}
324344
};
325345
}
326346

@@ -734,6 +754,11 @@ Pool.prototype.connect = function() {
734754
// Move the active connection
735755
moveConnectionBetween(connection, self.connectingConnections, self.availableConnections);
736756

757+
// if we have a minPoolSize, create a connection
758+
if (self.minSize) {
759+
for (let i = 0; i < self.minSize; i++) _createConnection(self);
760+
}
761+
737762
// Emit the connect event
738763
self.emit('connect', self);
739764
});

0 commit comments

Comments
 (0)