Skip to content

Commit 7b1d9bd

Browse files
committed
fix: allow event loop to process during wait queue processing
Running `processWaitQueue` on the next tick allows the event loop to process while the connection pool is processing large numbers of wait queue members. NODE-2803
1 parent 446877d commit 7b1d9bd

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

lib/cmap/connection_pool.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ class ConnectionPool extends EventEmitter {
218218
return;
219219
}
220220

221-
// add this request to the wait queue
222221
const waitQueueMember = { callback };
223222

224223
const pool = this;
@@ -233,11 +232,8 @@ class ConnectionPool extends EventEmitter {
233232
}, waitQueueTimeoutMS);
234233
}
235234

236-
// place the member at the end of the wait queue
237235
this[kWaitQueue].push(waitQueueMember);
238-
239-
// process the wait queue
240-
processWaitQueue(this);
236+
process.nextTick(() => processWaitQueue(this));
241237
}
242238

243239
/**
@@ -250,10 +246,8 @@ class ConnectionPool extends EventEmitter {
250246
const stale = connectionIsStale(this, connection);
251247
const willDestroy = !!(poolClosed || stale || connection.closed);
252248

253-
// Properly adjust state of connection
254249
if (!willDestroy) {
255250
connection.markAvailable();
256-
257251
this[kConnections].push(connection);
258252
}
259253

@@ -264,7 +258,7 @@ class ConnectionPool extends EventEmitter {
264258
destroyConnection(this, connection, reason);
265259
}
266260

267-
processWaitQueue(this);
261+
process.nextTick(() => processWaitQueue(this));
268262
}
269263

270264
/**
@@ -434,7 +428,7 @@ function createConnection(pool, callback) {
434428

435429
// otherwise add it to the pool for later acquisition, and try to process the wait queue
436430
pool[kConnections].push(connection);
437-
processWaitQueue(pool);
431+
process.nextTick(() => processWaitQueue(pool));
438432
});
439433
}
440434

test/unit/cmap/connection_pool.test.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,13 @@ describe('Connection Pool', function() {
145145
sinon.stub(pool, 'availableConnectionCount').get(() => 0);
146146
pool.checkIn(conn);
147147

148-
expect(pool)
149-
.property('waitQueueSize')
150-
.to.equal(0);
148+
process.nextTick(() => {
149+
expect(pool)
150+
.property('waitQueueSize')
151+
.to.equal(0);
151152

152-
done();
153+
done();
154+
});
153155
});
154156
});
155157
});

0 commit comments

Comments
 (0)