Skip to content

Commit 37a07b3

Browse files
dougwilsonseangarner
authored andcommitted
Add enqueue event to pool
closes mysqljs#716
1 parent 47fd30c commit 37a07b3

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

Changes.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ you spot any mistakes.
77
## HEAD
88

99
* Add code `POOL_ENQUEUELIMIT` to error reaching `queueLimit`
10+
* Add `enqueue` event to pool #716
1011
* Add `enqueue` event to protocol and connection #381
1112
* Blacklist unsupported connection flags #881
1213
* Make only column names enumerable in `RowDataPacket` #549 #895

Readme.md

+13
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,19 @@ addition to those options pools accept a few extras:
348348
before returning an error from `getConnection`. If set to `0`, there is no
349349
limit to the number of queued connection requests. (Default: `0`)
350350

351+
## Pool events
352+
353+
### enqueue
354+
355+
The pool will emit an `enqueue` event when a callback has been queued to wait for
356+
an available connection.
357+
358+
```js
359+
pool.on('enqueue', function () {
360+
console.log('Waiting for available connection slot');
361+
});
362+
```
363+
351364
## PoolCluster
352365

353366
PoolCluster provides multiple hosts connection. (group & retry & selector)

lib/Pool.js

+1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ Pool.prototype._enqueueCallback = function _enqueueCallback(callback) {
201201
: callback;
202202

203203
this._connectionQueue.push(cb);
204+
this.emit('enqueue');
204205
};
205206

206207
Pool.prototype._purgeConnection = function _purgeConnection(connection) {

test/unit/pool/test-enqueue-event.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
var assert = require('assert');
2+
var common = require('../../common');
3+
var pool = common.createPool({
4+
connectionLimit : 1,
5+
port : common.fakeServerPort
6+
});
7+
8+
var index = 0;
9+
var server = common.createFakeServer();
10+
11+
server.listen(common.fakeServerPort, function (err) {
12+
assert.ifError(err);
13+
14+
var count = 0;
15+
pool.on('enqueue', function () {
16+
count++;
17+
});
18+
19+
pool.getConnection(function (err, connection) {
20+
assert.ifError(err);
21+
assert.ok(connection);
22+
assert.equal(++index, 1);
23+
assert.equal(count, 0);
24+
25+
pool.getConnection(function (err) {
26+
assert.ifError(err);
27+
assert.equal(++index, 2);
28+
assert.equal(count, 2);
29+
30+
connection.release();
31+
});
32+
33+
pool.getConnection(function (err) {
34+
assert.ifError(err);
35+
assert.equal(++index, 3);
36+
assert.equal(count, 2);
37+
38+
connection.destroy();
39+
server.destroy();
40+
});
41+
42+
process.nextTick(function () {
43+
assert.equal(count, 2);
44+
connection.release();
45+
});
46+
});
47+
});

0 commit comments

Comments
 (0)