@@ -42,7 +42,8 @@ function hasSessionSupport(topology) {
42
42
* @class
43
43
* @param {string } options.host The server host
44
44
* @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
46
47
* @param {boolean } [options.reconnect=true] Server will attempt to reconnect on loss of connection
47
48
* @param {number } [options.reconnectTries=30] Server attempt to reconnect #times
48
49
* @param {number } [options.reconnectInterval=1000] Server will wait # milliseconds between retries
@@ -86,6 +87,8 @@ var Pool = function(topology, options) {
86
87
port : 27017 ,
87
88
// Pool default max size
88
89
size : 5 ,
90
+ // Pool default min size
91
+ minSize : 0 ,
89
92
// socket settings
90
93
connectionTimeout : 30000 ,
91
94
socketTimeout : 360000 ,
@@ -175,6 +178,13 @@ Object.defineProperty(Pool.prototype, 'size', {
175
178
}
176
179
} ) ;
177
180
181
+ Object . defineProperty ( Pool . prototype , 'minSize' , {
182
+ enumerable : true ,
183
+ get : function ( ) {
184
+ return this . options . minSize ;
185
+ }
186
+ } ) ;
187
+
178
188
Object . defineProperty ( Pool . prototype , 'connectionTimeout' , {
179
189
enumerable : true ,
180
190
get : function ( ) {
@@ -321,6 +331,16 @@ function connectionFailureHandler(self, event) {
321
331
if ( ! self . reconnectId && self . options . reconnect ) {
322
332
self . reconnectId = setTimeout ( attemptReconnect ( self ) , self . options . reconnectInterval ) ;
323
333
}
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
+ }
324
344
} ;
325
345
}
326
346
@@ -734,6 +754,11 @@ Pool.prototype.connect = function() {
734
754
// Move the active connection
735
755
moveConnectionBetween ( connection , self . connectingConnections , self . availableConnections ) ;
736
756
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
+
737
762
// Emit the connect event
738
763
self . emit ( 'connect' , self ) ;
739
764
} ) ;
0 commit comments