Skip to content

Commit

Permalink
mysqljs#2218 Add idleConnectionTimeout to pool options
Browse files Browse the repository at this point in the history
  • Loading branch information
thellmic89 committed Feb 13, 2020
1 parent 32a0293 commit 86774b4
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
5 changes: 5 additions & 0 deletions lib/Pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ Pool.prototype.acquireConnection = function acquireConnection(connection, cb) {
pool.emit('connection', connection);
}

if (connection._idleTimeout) {
clearTimeout(connection._idleTimeout);
connection._idleTimeout = null;
}

pool.emit('acquire', connection);
cb(null, connection);
}
Expand Down
13 changes: 8 additions & 5 deletions lib/PoolConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ function PoolConfig(options) {
options = ConnectionConfig.parseUrl(options);
}

this.acquireTimeout = (options.acquireTimeout === undefined)
this.acquireTimeout = (options.acquireTimeout === undefined)
? 10 * 1000
: Number(options.acquireTimeout);
this.connectionConfig = new ConnectionConfig(options);
this.waitForConnections = (options.waitForConnections === undefined)
this.connectionConfig = new ConnectionConfig(options);
this.waitForConnections = (options.waitForConnections === undefined)
? true
: Boolean(options.waitForConnections);
this.connectionLimit = (options.connectionLimit === undefined)
this.connectionLimit = (options.connectionLimit === undefined)
? 10
: Number(options.connectionLimit);
this.queueLimit = (options.queueLimit === undefined)
this.queueLimit = (options.queueLimit === undefined)
? 0
: Number(options.queueLimit);
this.idleConnectionTimeout = (options.idleConnectionTimeout === undefined)
? 0
: Number(options.idleConnectionTimeout);
}

PoolConfig.prototype.newConnectionConfig = function newConnectionConfig() {
Expand Down
14 changes: 14 additions & 0 deletions lib/PoolConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ var Events = require('events');
module.exports = PoolConnection;
inherits(PoolConnection, Connection);

function _noop() {}

function PoolConnection(pool, options) {
Connection.call(this, options);
this._pool = pool;
Expand Down Expand Up @@ -32,6 +34,13 @@ PoolConnection.prototype.release = function release() {
return undefined;
}

if (this._pool.config.idleConnectionTimeout) {
this._idleTimeout = setTimeout(
this._realEnd.bind(this, _noop),
this._pool.config.idleConnectionTimeout
);
}

return pool.releaseConnection(this);
};

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

if (this._idleTimeout) {
clearTimeout(this._idleTimeout);
this._idleTimeout = null;
}

var pool = this._pool;
this._pool = null;

Expand Down
38 changes: 38 additions & 0 deletions test/unit/pool/test-idle-connection-timeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var assert = require('assert');
var common = require('../../common');
var pool = common.createPool({
port : common.fakeServerPort,
idleConnectionTimeout : 100
});

var server = common.createFakeServer();

server.listen(common.fakeServerPort, function(err){
assert.ifError(err);

pool.once('release', function(connection) {
assert.ok(connection);
setTimeout(function() {
assert.equal(connection.state, 'disconnected');
pool.end(function (err) {
assert.ifError(err);
server.destroy();
});
}, 200);
});

pool.getConnection(function (err, firstConnection) {
assert.ifError(err);
assert.ok(firstConnection);
setTimeout(function() {
pool.getConnection(function (err, connection) {
assert.ifError(err);
assert.equal(connection.state, 'authenticated');
assert.equal(connection._idleTimeout, null);
assert.equal(firstConnection, connection);
connection.release();
});
}, 75);
firstConnection.release();
});
});

0 comments on commit 86774b4

Please sign in to comment.