Skip to content

Commit 945b92e

Browse files
committed
Add acquireTimeout pool option
closes #821 closes #854
1 parent 5a517ad commit 945b92e

File tree

6 files changed

+100
-1
lines changed

6 files changed

+100
-1
lines changed

Changes.md

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

99
* Add code `POOL_NOEXIST` in PoolCluster error #846
10+
* Add `acquireTimeout` pool option to specify a timeout for acquiring a connection #821 #854
1011
* Add `timeout` option to all sequences #855 #863
1112
* Default `connectTimeout` to 10 seconds
1213
* Fix domain binding with `conn.connect`

Readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,9 @@ Pools accept all the same options as a connection. When creating a new
328328
connection, the options are simply passed to the connection constructor. In
329329
addition to those options pools accept a few extras:
330330

331+
* `acquireTimeout`: The milliseconds before a timeout occurs during the connection
332+
acquisition. This is slightly different from `connectTimeout`, because acquiring
333+
a pool connection does not always involve making a connection. (Default: 10 seconds)
331334
* `waitForConnections`: Determines the pool's action when no connections are
332335
available and the limit has been reached. If `true`, the pool will queue the
333336
connection request and call it when one becomes available. If `false`, the

lib/Pool.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Pool.prototype.acquireConnection = function acquireConnection(connection, cb) {
7575
var pool = this;
7676

7777
connection._pool = null;
78-
connection.ping(function(err){
78+
connection.ping({timeout: this.config.acquireTimeout}, function(err) {
7979
if (!err) {
8080
connection._pool = pool;
8181
cb(null, connection);

lib/PoolConfig.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ function PoolConfig(options) {
77
options = ConnectionConfig.parseUrl(options);
88
}
99

10+
this.acquireTimeout = (options.acquireTimeout === undefined)
11+
? 10 * 1000
12+
: Number(options.acquireTimeout);
1013
this.connectionConfig = new ConnectionConfig(options);
1114
this.waitForConnections = (options.waitForConnections === undefined)
1215
? true
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
var assert = require('assert');
2+
var common = require('../../common');
3+
var pool = common.createPool({
4+
acquireTimeout : 100,
5+
connectionLimit : 1,
6+
port : common.fakeServerPort
7+
});
8+
var server = common.createFakeServer();
9+
var Packets = require(common.lib + '/protocol/packets');
10+
11+
var fail = false;
12+
var seq = 0;
13+
var serverConn = null;
14+
var tid = 0;
15+
16+
server.listen(common.fakeServerPort, function (err) {
17+
assert.ifError(err);
18+
19+
pool.getConnection(function (err, conn) {
20+
assert.ifError(err);
21+
assert.equal(conn.threadId, 1);
22+
conn.release();
23+
24+
// lag the next ping
25+
fail = true;
26+
27+
pool.getConnection(function(err, conn){
28+
assert.ifError(err);
29+
assert.equal(++seq, 1);
30+
assert.equal(conn.threadId, 2);
31+
assert.equal(fail, false);
32+
conn.ping(function(err){
33+
assert.ifError(err);
34+
conn.release();
35+
});
36+
});
37+
38+
pool.getConnection(function(err, conn){
39+
assert.ifError(err);
40+
assert.equal(++seq, 2);
41+
assert.equal(conn.threadId, 2);
42+
server.destroy();
43+
});
44+
});
45+
});
46+
47+
server.on('connection', function(incomingConnection) {
48+
serverConn = incomingConnection;
49+
incomingConnection.handshake({
50+
threadId: ++tid
51+
});
52+
incomingConnection.on('ping', function() {
53+
if (!fail) {
54+
this._sendPacket(new Packets.OkPacket());
55+
this._parser.resetPacketNumber();
56+
}
57+
58+
fail = false;
59+
});
60+
});

test/unit/test-PoolConfig.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,35 @@ test('PoolConfig#Constructor', {
4141
assert.equal(config.connectionLimit, 2);
4242
},
4343
});
44+
45+
test('PoolConfig#Constructor.acquireTimeout', {
46+
'defaults to 10 seconds': function() {
47+
var config = new PoolConfig({});
48+
49+
assert.equal(config.acquireTimeout, (10 * 1000));
50+
},
51+
52+
'undefined uses default': function() {
53+
var config = new PoolConfig({
54+
acquireTimeout: undefined
55+
});
56+
57+
assert.equal(config.acquireTimeout, (10 * 1000));
58+
},
59+
60+
'can set to 0': function() {
61+
var config = new PoolConfig({
62+
acquireTimeout: 0
63+
});
64+
65+
assert.equal(config.acquireTimeout, 0);
66+
},
67+
68+
'can set to custom value': function() {
69+
var config = new PoolConfig({
70+
acquireTimeout: 10000
71+
});
72+
73+
assert.equal(config.acquireTimeout, 10000);
74+
},
75+
});

0 commit comments

Comments
 (0)