Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add idleConnectionTimeout to pool options #2218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 1 Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ you spot any mistakes.
## HEAD

* Support Node.js 12.x #2211
* Add `idleConnectionTimeout` to pool options

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

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

## Pool events

Expand Down
5 changes: 5 additions & 0 deletions 5 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
3 changes: 3 additions & 0 deletions 3 lib/PoolConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ function PoolConfig(options) {
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 14 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 38 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();
});
});
Morty Proxy This is a proxified and sanitized view of the page, visit original site.