Skip to content

Commit

Permalink
Add enqueue event to pool
Browse files Browse the repository at this point in the history
closes #716
  • Loading branch information
dougwilson committed Sep 7, 2014
1 parent 5f09c1c commit 6413790
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ you spot any mistakes.
## HEAD

* Add code `POOL_ENQUEUELIMIT` to error reaching `queueLimit`
* Add `enqueue` event to pool #716
* Add `enqueue` event to protocol and connection #381
* Blacklist unsupported connection flags #881
* Make only column names enumerable in `RowDataPacket` #549 #895
Expand Down
13 changes: 13 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,19 @@ addition to those options pools accept a few extras:
before returning an error from `getConnection`. If set to `0`, there is no
limit to the number of queued connection requests. (Default: `0`)

## Pool events

### enqueue

The pool will emit an `enqueue` event when a callback has been queued to wait for
an available connection.

```js
pool.on('enqueue', function () {
console.log('Waiting for available connection slot');
});
```

## PoolCluster

PoolCluster provides multiple hosts connection. (group & retry & selector)
Expand Down
1 change: 1 addition & 0 deletions lib/Pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ Pool.prototype._enqueueCallback = function _enqueueCallback(callback) {
: callback;

this._connectionQueue.push(cb);
this.emit('enqueue');
};

Pool.prototype._purgeConnection = function _purgeConnection(connection) {
Expand Down
47 changes: 47 additions & 0 deletions test/unit/pool/test-enqueue-event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var assert = require('assert');
var common = require('../../common');
var pool = common.createPool({
connectionLimit : 1,
port : common.fakeServerPort
});

var index = 0;
var server = common.createFakeServer();

server.listen(common.fakeServerPort, function (err) {
assert.ifError(err);

var count = 0;
pool.on('enqueue', function () {
count++;
});

pool.getConnection(function (err, connection) {
assert.ifError(err);
assert.ok(connection);
assert.equal(++index, 1);
assert.equal(count, 0);

pool.getConnection(function (err) {
assert.ifError(err);
assert.equal(++index, 2);
assert.equal(count, 2);

connection.release();
});

pool.getConnection(function (err) {
assert.ifError(err);
assert.equal(++index, 3);
assert.equal(count, 2);

connection.destroy();
server.destroy();
});

process.nextTick(function () {
assert.equal(count, 2);
connection.release();
});
});
});

0 comments on commit 6413790

Please sign in to comment.