Skip to content

Commit

Permalink
refactor(pool): support creating connections in destroying state
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mbroadst committed Nov 5, 2019
1 parent aae9997 commit 0b7788e
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions lib/core/connection/pool.js
Expand Up @@ -560,18 +560,19 @@ Pool.prototype.connect = function(callback) {
}

stateTransition(this, CONNECTED);
if (typeof callback === 'function') {
callback(null, conn);
} else {
this.emit('connect', this, conn);
}

// create min connections
if (this.minSize) {
for (let i = 0; i < this.minSize; i++) {
createConnection(this);
}
}

if (typeof callback === 'function') {
callback(null, conn);
} else {
this.emit('connect', this, conn);
}
});
};

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

destroy(self, connections, { force: false }, callback);
// } else if (self.queue.length > 0 && !this.reconnectId) {
} else {
// Ensure we empty the queue
_execute(self)();
Expand Down Expand Up @@ -942,7 +942,7 @@ function removeConnection(self, connection) {
}

function createConnection(pool, callback) {
if (pool.state === DESTROYED || pool.state === DESTROYING) {
if (pool.state === DESTROYED) {
if (typeof callback === 'function') {
callback(new MongoError('Cannot create connection when pool is destroyed'));
}
Expand Down Expand Up @@ -983,7 +983,7 @@ function createConnection(pool, callback) {
}

// the pool might have been closed since we started creating the connection
if (pool.state === DESTROYED || pool.state === DESTROYING) {
if (pool.state === DESTROYED) {
if (typeof callback === 'function') {
callback(new MongoError('Pool was destroyed after connection creation'));
}
Expand Down Expand Up @@ -1050,6 +1050,12 @@ function _execute(self) {
if (self.availableConnections.length === 0) {
// Flush any monitoring operations
flushMonitoringOperations(self.queue);

// attempt to grow the pool
if (totalConnections < self.options.size) {
createConnection(self);
}

break;
}

Expand Down Expand Up @@ -1114,10 +1120,7 @@ function _execute(self) {
}

// Re-execute the operation
setTimeout(function() {
_execute(self)();
}, 10);

setTimeout(() => _execute(self)(), 10);
break;
}
}
Expand Down

0 comments on commit 0b7788e

Please sign in to comment.