diff --git a/lib/pool.js b/lib/pool.js index ec3f4bd0a4..6809eed871 100644 --- a/lib/pool.js +++ b/lib/pool.js @@ -29,9 +29,14 @@ Pool.prototype.getConnection = function (cb) { var connection; - if (this._freeConnections.length > 0) { + while (this._freeConnections.length > 0) { connection = this._freeConnections.shift(); + if (connection._expires && connection._expires < Date.now()) { + connection.destroy(); + continue; + } + return process.nextTick(function () { return cb(null, connection); }); @@ -40,6 +45,10 @@ Pool.prototype.getConnection = function (cb) { if (this.config.connectionLimit === 0 || this._allConnections.length < this.config.connectionLimit) { connection = new PoolConnection(this, {config: this.config.connectionConfig}); + if (this.config.connectionExpiry) { + connection._expires = Date.now() + this.config.connectionExpiry; + } + this._allConnections.push(connection); return connection.connect(function (err) { @@ -79,6 +88,8 @@ Pool.prototype.releaseConnection = function (connection) { process.nextTick(this.getConnection.bind(this, cb)); } + } else if (connection._expires && connection._expires < Date.now()) { + connection.destroy(); } else if (this._connectionQueue.length) { cb = this._connectionQueue.shift(); diff --git a/lib/pool_connection.js b/lib/pool_connection.js index 4357388dcb..eefa88ff80 100644 --- a/lib/pool_connection.js +++ b/lib/pool_connection.js @@ -9,6 +9,7 @@ inherits(PoolConnection, Connection); function PoolConnection (pool, options) { Connection.call(this, options); this._pool = pool; + this._expires = false; // When a fatal error occurs the connection's protocol ends, which will cause // the connection to end as well, thus we only need to watch for the end event