Skip to content

Commit ef55076

Browse files
committed
Add POOL_CLOSED code to "Pool is closed." error
1 parent 1cebe23 commit ef55076

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

Changes.md

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

77
## HEAD
88

9+
* Add `POOL_CLOSED` code to "Pool is closed." error
910
* Add `POOL_CONNLIMIT` code to "No connections available." error #1332
1011
* Fix Query stream to emit close after ending #1349 #1350
1112
* Fix type cast for BIGINT columns when number is negative #1376

lib/Pool.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ function Pool(options) {
2222
Pool.prototype.getConnection = function (cb) {
2323

2424
if (this._closed) {
25+
var err = new Error('Pool is closed.');
26+
err.code = 'POOL_CLOSED';
2527
process.nextTick(function () {
26-
cb(new Error('Pool is closed.'));
28+
cb(err);
2729
});
2830
return;
2931
}
@@ -48,6 +50,7 @@ Pool.prototype.getConnection = function (cb) {
4850

4951
if (pool._closed) {
5052
err = new Error('Pool is closed.');
53+
err.code = 'POOL_CLOSED';
5154
}
5255

5356
if (err) {
@@ -89,6 +92,7 @@ Pool.prototype.acquireConnection = function acquireConnection(connection, cb) {
8992

9093
if (pool._closed) {
9194
err = new Error('Pool is closed.');
95+
err.code = 'POOL_CLOSED';
9296
}
9397

9498
if (err) {
@@ -115,7 +119,6 @@ Pool.prototype.acquireConnection = function acquireConnection(connection, cb) {
115119
};
116120

117121
Pool.prototype.releaseConnection = function releaseConnection(connection) {
118-
var cb;
119122
var pool = this;
120123

121124
if (this._acquiringConnections.indexOf(connection) !== -1) {
@@ -138,17 +141,18 @@ Pool.prototype.releaseConnection = function releaseConnection(connection) {
138141
}
139142
}
140143

141-
while (this._closed && this._connectionQueue.length) {
144+
if (this._closed) {
142145
// empty the connection queue
143-
cb = this._connectionQueue.shift();
144-
145-
process.nextTick(cb.bind(null, new Error('Pool is closed.')));
146-
}
147-
148-
if (this._connectionQueue.length) {
149-
cb = this._connectionQueue.shift();
150-
151-
this.getConnection(cb);
146+
this._connectionQueue.splice(0).forEach(function (cb) {
147+
var err = new Error('Pool is closed.');
148+
err.code = 'POOL_CLOSED';
149+
process.nextTick(function () {
150+
cb(err);
151+
});
152+
});
153+
} else if (this._connectionQueue.length) {
154+
// get connection with next waiting callback
155+
this.getConnection(this._connectionQueue.shift());
152156
}
153157
};
154158

test/unit/pool/test-end-ping.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ server.listen(common.fakeServerPort, function (err) {
2222
pool.getConnection(function (err, conn) {
2323
assert.ok(err);
2424
assert.equal(err.message, 'Pool is closed.');
25+
assert.equal(err.code, 'POOL_CLOSED');
2526
});
2627

2728
pool.end(function (err) {

test/unit/pool/test-end-queued.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,7 @@ process.on('exit', function() {
4141
assert.ok(conn1Err);
4242
assert.ok(conn2Err);
4343
assert.equal(conn1Err.message, 'Pool is closed.');
44-
assert.equal(conn2Err.message, 'Pool is closed.');
44+
assert.equal(conn2Err.code, 'POOL_CLOSED');
45+
assert.equal(conn1Err.message, 'Pool is closed.');
46+
assert.equal(conn2Err.code, 'POOL_CLOSED');
4547
});

0 commit comments

Comments
 (0)