Skip to content
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

Add idleConnectionTimeout to pool options #2218

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 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 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 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 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
12 changes: 12 additions & 0 deletions lib/PoolConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ PoolConnection.prototype.release = function release() {
return undefined;
}

if (this._pool.config.idleConnectionTimeout) {
this._idleTimeout = setTimeout(
this.destroy.bind(this),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably end the connection gracefully, running the QUIT packet to the server instead of just dropping the TCP connection. Various database setups will block a client that keeps dropping TCP connections without properly closing them.

this._pool.config.idleConnectionTimeout
);
}

return pool.releaseConnection(this);
};

Expand All @@ -58,6 +65,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();
});
});