Skip to content

Commit 09b308f

Browse files
arnaudchenyensudougwilson
authored andcommitted
Add acquire and release events to Pool for tracking
closes #1366 closes #1449 closes #1528 closes #1625
1 parent eeca132 commit 09b308f

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed

Changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ you spot any mistakes.
88

99
* Accept regular expression as pool cluster pattern #1572
1010
* Accept wildcard anywhere in pool cluster pattern #1570
11+
* Add `acquire` and `release` events to `Pool` for tracking #1366 #1449 #1528 #1625
1112
* Add new error codes up to MySQL 5.7.17
1213
* Fix edge cases when determing Query result packets #1547
1314
* Fix memory leak when using long-running domains #1619 #1620

Readme.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,18 @@ constructor. In addition to those options pools accept a few extras:
394394

395395
## Pool events
396396

397+
### acquire
398+
399+
The pool will emit an `acquire` event when a connection is acquired from the pool.
400+
This is called after all acquiring activity has been performed on the connection,
401+
right before the connection is handed to the callback of the acquiring code.
402+
403+
```js
404+
pool.on('acquire', function (connection) {
405+
console.log('Connection %d acquired', connection.threadId);
406+
});
407+
```
408+
397409
### connection
398410

399411
The pool will emit a `connection` event when a new connection is made within the pool.
@@ -417,6 +429,18 @@ pool.on('enqueue', function () {
417429
});
418430
```
419431

432+
### release
433+
434+
The pool will emit a `release` event when a connection is released back to the
435+
pool. This is called after all release activity has been performed on the connection,
436+
so the connection will be listed as free at the time of the event.
437+
438+
```js
439+
pool.on('release', function (connection) {
440+
console.log('Connection %d released', connection.threadId);
441+
});
442+
```
443+
420444
## Closing all the connections in a pool
421445

422446
When you are done using the pool, you have to end all the connections or the

lib/Pool.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Pool.prototype.getConnection = function (cb) {
6060
}
6161

6262
pool.emit('connection', connection);
63+
pool.emit('acquire', connection);
6364
cb(null, connection);
6465
});
6566
return;
@@ -105,6 +106,7 @@ Pool.prototype.acquireConnection = function acquireConnection(connection, cb) {
105106
pool.emit('connection', connection);
106107
}
107108

109+
pool.emit('acquire', connection);
108110
cb(null, connection);
109111
}
110112

@@ -137,6 +139,7 @@ Pool.prototype.releaseConnection = function releaseConnection(connection) {
137139
} else {
138140
// add connection to end of free queue
139141
this._freeConnections.push(connection);
142+
this.emit('release', connection);
140143
}
141144
}
142145

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 server = common.createFakeServer();
9+
10+
server.listen(common.fakeServerPort, function (err) {
11+
assert.ifError(err);
12+
13+
var count = 0;
14+
var threadId = -1;
15+
pool.on('acquire', function (connection) {
16+
count++;
17+
threadId = connection.threadId;
18+
});
19+
20+
pool.getConnection(function (err, connection) {
21+
assert.ifError(err);
22+
assert.ok(connection);
23+
assert.equal(count, 1);
24+
assert.equal(threadId, connection.threadId);
25+
connection.release();
26+
});
27+
28+
pool.getConnection(function (err, connection) {
29+
assert.ifError(err);
30+
assert.ok(connection);
31+
assert.equal(count, 2);
32+
assert.equal(threadId, connection.threadId);
33+
connection.release();
34+
pool.end(function (err) {
35+
assert.ifError(err);
36+
server.destroy();
37+
});
38+
});
39+
});

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 server = common.createFakeServer();
9+
10+
server.listen(common.fakeServerPort, function (err) {
11+
assert.ifError(err);
12+
13+
var count = 0;
14+
var threadId = -1;
15+
pool.on('release', function (connection) {
16+
count++;
17+
threadId = connection.threadId;
18+
});
19+
20+
pool.getConnection(function (err, connection) {
21+
assert.ifError(err);
22+
assert.ok(connection);
23+
assert.equal(count, 0);
24+
connection.release();
25+
assert.equal(count, 1);
26+
assert.equal(threadId, connection.threadId);
27+
});
28+
29+
pool.getConnection(function (err, connection) {
30+
assert.ifError(err);
31+
assert.ok(connection);
32+
assert.equal(count, 1);
33+
connection.release();
34+
assert.equal(count, 2);
35+
assert.equal(threadId, connection.threadId);
36+
pool.end(function (err) {
37+
assert.ifError(err);
38+
server.destroy();
39+
});
40+
});
41+
});

0 commit comments

Comments
 (0)