Skip to content

Commit 86774b4

Browse files
committed
mysqljs#2218 Add idleConnectionTimeout to pool options
1 parent 32a0293 commit 86774b4

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

lib/Pool.js

+5
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

+8-5
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@ function PoolConfig(options) {
77
options = ConnectionConfig.parseUrl(options);
88
}
99

10-
this.acquireTimeout = (options.acquireTimeout === undefined)
10+
this.acquireTimeout = (options.acquireTimeout === undefined)
1111
? 10 * 1000
1212
: Number(options.acquireTimeout);
13-
this.connectionConfig = new ConnectionConfig(options);
14-
this.waitForConnections = (options.waitForConnections === undefined)
13+
this.connectionConfig = new ConnectionConfig(options);
14+
this.waitForConnections = (options.waitForConnections === undefined)
1515
? true
1616
: Boolean(options.waitForConnections);
17-
this.connectionLimit = (options.connectionLimit === undefined)
17+
this.connectionLimit = (options.connectionLimit === undefined)
1818
? 10
1919
: Number(options.connectionLimit);
20-
this.queueLimit = (options.queueLimit === undefined)
20+
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

+14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ var Events = require('events');
55
module.exports = PoolConnection;
66
inherits(PoolConnection, Connection);
77

8+
function _noop() {}
9+
810
function PoolConnection(pool, options) {
911
Connection.call(this, options);
1012
this._pool = pool;
@@ -32,6 +34,13 @@ PoolConnection.prototype.release = function release() {
3234
return undefined;
3335
}
3436

37+
if (this._pool.config.idleConnectionTimeout) {
38+
this._idleTimeout = setTimeout(
39+
this._realEnd.bind(this, _noop),
40+
this._pool.config.idleConnectionTimeout
41+
);
42+
}
43+
3544
return pool.releaseConnection(this);
3645
};
3746

@@ -58,6 +67,11 @@ PoolConnection.prototype._removeFromPool = function _removeFromPool() {
5867
return;
5968
}
6069

70+
if (this._idleTimeout) {
71+
clearTimeout(this._idleTimeout);
72+
this._idleTimeout = null;
73+
}
74+
6175
var pool = this._pool;
6276
this._pool = null;
6377

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)