Skip to content

Commit d13b153

Browse files
committed
fix(cmap): error wait queue members on failed connection creation
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
1 parent 5a12683 commit d13b153

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

lib/cmap/connection_pool.js

+24-2
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,6 @@ function createConnection(pool, callback) {
424424

425425
// if a callback has been provided, check out the connection immediately
426426
if (typeof callback === 'function') {
427-
pool.emit('connectionCheckedOut', new ConnectionCheckedOutEvent(pool, connection));
428427
callback(undefined, connection);
429428
return;
430429
}
@@ -474,7 +473,30 @@ function processWaitQueue(pool) {
474473

475474
const maxPoolSize = pool.options.maxPoolSize;
476475
if (pool[kWaitQueue].length && (maxPoolSize <= 0 || pool.totalConnectionCount < maxPoolSize)) {
477-
createConnection(pool);
476+
createConnection(pool, (err, connection) => {
477+
const waitQueueMember = pool[kWaitQueue].shift();
478+
if (waitQueueMember == null) {
479+
if (err == null) {
480+
pool[kConnections].push(connection);
481+
}
482+
483+
return;
484+
}
485+
486+
if (waitQueueMember[kCancelled]) {
487+
return;
488+
}
489+
490+
if (err) {
491+
pool.emit('connectionCheckOutFailed', new ConnectionCheckOutFailedEvent(pool, err));
492+
} else {
493+
pool.emit('connectionCheckedOut', new ConnectionCheckedOutEvent(pool, connection));
494+
}
495+
496+
clearTimeout(waitQueueMember.timer);
497+
waitQueueMember.callback(err, connection);
498+
});
499+
478500
return;
479501
}
480502
}

test/unit/cmap/connection_pool.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,13 @@ describe('Connection Pool', function() {
158158

159159
const callback = err => {
160160
expect(err).to.exist;
161-
expect(err).to.match(/Timed out/);
161+
expect(err).to.match(/closed/);
162162
pool.close(done);
163163
};
164164

165165
pool.withConnection((err, conn, cb) => {
166166
expect(err).to.exist;
167-
expect(err).to.match(/Timed out/);
167+
expect(err).to.match(/closed/);
168168
cb(err);
169169
}, callback);
170170
});

0 commit comments

Comments
 (0)