Skip to content
Open
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
2 changes: 2 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ you spot any mistakes.
* Add new error codes up to MySQL 5.7.29
* Fix early detection of bad callback to `connection.query`
* Support Node.js 12.x #2211
* Add `idleConnectionTimeout` to pool options
* Add `connectionMinLimit` to pool options
* Support Node.js 13.x
* Support non-enumerable properties in object argument to `connection.query` #2253
* Update `bignumber.js` to 9.0.0
Expand Down
5 changes: 5 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,11 @@ 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`)
* `connectionMinLimit`: The minimum number of connections in the pool.
checked when removing a connection. (Default: `5`)

## 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
8 changes: 8 additions & 0 deletions lib/PoolConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,17 @@ function PoolConfig(options) {
this.connectionLimit = (options.connectionLimit === undefined)
? 10
: Number(options.connectionLimit);
//SERVER-5357 BEGIN
this.connectionMinLimit = (options.connectionMinLimit === undefined)
? 5
: Number(options.connectionMinLimit);
//SERVER-5357 END
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
20 changes: 20 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,17 @@ PoolConnection.prototype._removeFromPool = function _removeFromPool() {
return;
}

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

//SERVER-5357 BEGIN
if (this._allConnections.length <= this.config.connectionMinLimit) {
return;
}
//SERVER-5357 END

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

Expand Down