Skip to content

Commit fc44254

Browse files
committed
Add idleConnectionTimeout to pool options
1 parent 99d6f78 commit fc44254

File tree

6 files changed

+62
-0
lines changed

6 files changed

+62
-0
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
* Support Node.js 12.x #2211
10+
* Add `idleConnectionTimeout` to pool options
1011

1112
## v2.17.1 (2019-04-18)
1213

Readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,9 @@ constructor. In addition to those options pools accept a few extras:
393393
* `queueLimit`: The maximum number of connection requests the pool will queue
394394
before returning an error from `getConnection`. If set to `0`, there is no
395395
limit to the number of queued connection requests. (Default: `0`)
396+
* `idleConnectionTimeout`: The maximum number of milliseconds a connection can be
397+
idle in the pool. If set to `0` connection will live until it is manually
398+
destroyed or the pool is closed. (Default: `0`)
396399

397400
## Pool events
398401

lib/Pool.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ Pool.prototype.acquireConnection = function acquireConnection(connection, cb) {
106106
pool.emit('connection', connection);
107107
}
108108

109+
if (connection._idleTimeout) {
110+
clearTimeout(connection._idleTimeout);
111+
connection._idleTimeout = null;
112+
}
113+
109114
pool.emit('acquire', connection);
110115
cb(null, connection);
111116
}

lib/PoolConfig.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ function PoolConfig(options) {
2020
this.queueLimit = (options.queueLimit === undefined)
2121
? 0
2222
: Number(options.queueLimit);
23+
this.idleConnectionTimeout = (options.idleConnectionTimeout === undefined)
24+
? 0
25+
: Number(options.idleConnectionTimeout);
2326
}
2427

2528
PoolConfig.prototype.newConnectionConfig = function newConnectionConfig() {

lib/PoolConnection.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ PoolConnection.prototype.release = function release() {
3232
return undefined;
3333
}
3434

35+
if (this._pool.config.idleConnectionTimeout) {
36+
this._idleTimeout = setTimeout(
37+
this.destroy.bind(this),
38+
this._pool.config.idleConnectionTimeout
39+
);
40+
}
41+
3542
return pool.releaseConnection(this);
3643
};
3744

@@ -58,6 +65,11 @@ PoolConnection.prototype._removeFromPool = function _removeFromPool() {
5865
return;
5966
}
6067

68+
if (this._idleTimeout) {
69+
clearTimeout(this._idleTimeout);
70+
this._idleTimeout = null;
71+
}
72+
6173
var pool = this._pool;
6274
this._pool = null;
6375

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
var assert = require('assert');
2+
var common = require('../../common');
3+
var pool = common.createPool({
4+
port : common.fakeServerPort,
5+
idleConnectionTimeout : 100
6+
});
7+
8+
var server = common.createFakeServer();
9+
10+
server.listen(common.fakeServerPort, function(err){
11+
assert.ifError(err);
12+
13+
pool.once('release', function(connection) {
14+
assert.ok(connection);
15+
setTimeout(function() {
16+
assert.equal(connection.state, 'disconnected');
17+
pool.end(function (err) {
18+
assert.ifError(err);
19+
server.destroy();
20+
});
21+
}, 200);
22+
});
23+
24+
pool.getConnection(function (err, firstConnection) {
25+
assert.ifError(err);
26+
assert.ok(firstConnection);
27+
setTimeout(function() {
28+
pool.getConnection(function (err, connection) {
29+
assert.ifError(err);
30+
assert.equal(connection.state, 'authenticated');
31+
assert.equal(connection._idleTimeout, null);
32+
assert.equal(firstConnection, connection);
33+
connection.release();
34+
});
35+
}, 75);
36+
firstConnection.release();
37+
});
38+
});

0 commit comments

Comments
 (0)