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 connectionLifetime to pool options #2195

Closed
wants to merge 1 commit 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 Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ you spot any mistakes.
* Remove special case for handshake in determine packet code
* Small performance improvement starting command sequence
* Support Node.js 11.x
* Add `connectionLifetime` to pool options

## v2.16.0 (2018-07-17)

Expand Down
3 changes: 3 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,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`)
* `connectionLifetime`: The maximum number of milliseconds a connection can be
alive 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
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.connectionLifetime = (options.connectionLifetime === undefined)
? 0
: Number(options.connectionLifetime);
}

PoolConfig.prototype.newConnectionConfig = function newConnectionConfig() {
Expand Down
11 changes: 11 additions & 0 deletions lib/PoolConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ function PoolConnection(pool, options) {
Connection.call(this, options);
this._pool = pool;

if (this._pool.config.connectionLifetime) {
this._lifetimeout = setTimeout(
this.destroy.bind(this),
this._pool.config.connectionLifetime
);
}

// Bind connection to pool domain
if (Events.usingDomains) {
this.domain = pool.domain;
Expand Down Expand Up @@ -54,6 +61,10 @@ PoolConnection.prototype.destroy = function () {
};

PoolConnection.prototype._removeFromPool = function _removeFromPool() {
if (this._lifetimeout) {
clearTimeout(this._lifetimeout);
}

if (!this._pool || this._pool._closed) {
return;
}
Expand Down
26 changes: 26 additions & 0 deletions test/unit/pool/test-connection-lifetime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var assert = require('assert');
var common = require('../../common');
var pool = common.createPool({
port : common.fakeServerPort,
connectionLifetime : 100
});

var server = common.createFakeServer();

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

pool.getConnection(function (err, connection) {
assert.ifError(err);
assert.ok(connection);

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

});
});