Skip to content

Commit

Permalink
fix(cmap): error wait queue members on failed connection creation
Browse files Browse the repository at this point in the history
When a pool is empty and an operation enters the wait queue, the
pool will attempt to immediately create a new connection for the
requested operation. If that immediate creation fails, the error
should be returned to that member, rather than letting the member
sit in the queue indefinitely

NODE-2424
NODE-2372
  • Loading branch information
mbroadst committed Jan 17, 2020
1 parent 5a12683 commit d13b153
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
26 changes: 24 additions & 2 deletions lib/cmap/connection_pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,6 @@ function createConnection(pool, callback) {

// if a callback has been provided, check out the connection immediately
if (typeof callback === 'function') {
pool.emit('connectionCheckedOut', new ConnectionCheckedOutEvent(pool, connection));
callback(undefined, connection);
return;
}
Expand Down Expand Up @@ -474,7 +473,30 @@ function processWaitQueue(pool) {

const maxPoolSize = pool.options.maxPoolSize;
if (pool[kWaitQueue].length && (maxPoolSize <= 0 || pool.totalConnectionCount < maxPoolSize)) {
createConnection(pool);
createConnection(pool, (err, connection) => {
const waitQueueMember = pool[kWaitQueue].shift();
if (waitQueueMember == null) {
if (err == null) {
pool[kConnections].push(connection);
}

return;
}

if (waitQueueMember[kCancelled]) {
return;
}

if (err) {
pool.emit('connectionCheckOutFailed', new ConnectionCheckOutFailedEvent(pool, err));
} else {
pool.emit('connectionCheckedOut', new ConnectionCheckedOutEvent(pool, connection));
}

clearTimeout(waitQueueMember.timer);
waitQueueMember.callback(err, connection);
});

return;
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/unit/cmap/connection_pool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,13 @@ describe('Connection Pool', function() {

const callback = err => {
expect(err).to.exist;
expect(err).to.match(/Timed out/);
expect(err).to.match(/closed/);
pool.close(done);
};

pool.withConnection((err, conn, cb) => {
expect(err).to.exist;
expect(err).to.match(/Timed out/);
expect(err).to.match(/closed/);
cb(err);
}, callback);
});
Expand Down

0 comments on commit d13b153

Please sign in to comment.