Skip to content

Commit 0b7788e

Browse files
committed
refactor(pool): support creating connections in destroying state
We have some edge cases in our testing where `endSessions` is sent during `destroy`, but the pool might not have enough open connections in that case.
1 parent aae9997 commit 0b7788e

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

lib/core/connection/pool.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -560,18 +560,19 @@ Pool.prototype.connect = function(callback) {
560560
}
561561

562562
stateTransition(this, CONNECTED);
563-
if (typeof callback === 'function') {
564-
callback(null, conn);
565-
} else {
566-
this.emit('connect', this, conn);
567-
}
568563

569564
// create min connections
570565
if (this.minSize) {
571566
for (let i = 0; i < this.minSize; i++) {
572567
createConnection(this);
573568
}
574569
}
570+
571+
if (typeof callback === 'function') {
572+
callback(null, conn);
573+
} else {
574+
this.emit('connect', this, conn);
575+
}
575576
});
576577
};
577578

@@ -687,7 +688,6 @@ Pool.prototype.destroy = function(force, callback) {
687688
}
688689

689690
destroy(self, connections, { force: false }, callback);
690-
// } else if (self.queue.length > 0 && !this.reconnectId) {
691691
} else {
692692
// Ensure we empty the queue
693693
_execute(self)();
@@ -942,7 +942,7 @@ function removeConnection(self, connection) {
942942
}
943943

944944
function createConnection(pool, callback) {
945-
if (pool.state === DESTROYED || pool.state === DESTROYING) {
945+
if (pool.state === DESTROYED) {
946946
if (typeof callback === 'function') {
947947
callback(new MongoError('Cannot create connection when pool is destroyed'));
948948
}
@@ -983,7 +983,7 @@ function createConnection(pool, callback) {
983983
}
984984

985985
// the pool might have been closed since we started creating the connection
986-
if (pool.state === DESTROYED || pool.state === DESTROYING) {
986+
if (pool.state === DESTROYED) {
987987
if (typeof callback === 'function') {
988988
callback(new MongoError('Pool was destroyed after connection creation'));
989989
}
@@ -1050,6 +1050,12 @@ function _execute(self) {
10501050
if (self.availableConnections.length === 0) {
10511051
// Flush any monitoring operations
10521052
flushMonitoringOperations(self.queue);
1053+
1054+
// attempt to grow the pool
1055+
if (totalConnections < self.options.size) {
1056+
createConnection(self);
1057+
}
1058+
10531059
break;
10541060
}
10551061

@@ -1114,10 +1120,7 @@ function _execute(self) {
11141120
}
11151121

11161122
// Re-execute the operation
1117-
setTimeout(function() {
1118-
_execute(self)();
1119-
}, 10);
1120-
1123+
setTimeout(() => _execute(self)(), 10);
11211124
break;
11221125
}
11231126
}

0 commit comments

Comments
 (0)