From e46ef6eee495f606e98816f7369d1156d2fe0912 Mon Sep 17 00:00:00 2001 From: Ankur Deep Jaiswal Date: Tue, 6 Jun 2017 18:34:29 +0530 Subject: [PATCH 1/5] Remove Stale connections that have not been used for idletimeout period of time. #962 --- lib/Pool.js | 42 ++++++++++++++++++++++++++++++++++++++++++ lib/PoolConfig.js | 1 + 2 files changed, 43 insertions(+) diff --git a/lib/Pool.js b/lib/Pool.js index 87a40114a..ac002d208 100644 --- a/lib/Pool.js +++ b/lib/Pool.js @@ -17,6 +17,39 @@ function Pool(options) { this._freeConnections = []; this._connectionQueue = []; this._closed = false; + this._recycleTimer = null; +} + +function recycleStaleConnections() { + var now = Date.now(); + var min = null; + for(var i=0 ;i= this.config.idleTimeout) { + this._freeConnections[i].destroy(); + i--; + } else { + if(min == null) { + min = now - lastused + } else { + if((now - lastused) < min) { + min = now - lastused; + } + } + } + } + } + if(min != null) { + if(min > 0) { + clearInterval(this._recycleTimer); + this._recycleTimer = setInterval(recycleStaleConnections.bind(this),this.config.idleTimeout); + } + } else { + clearInterval(this._recycleTimer); + this._recycleTimer = null; + } } Pool.prototype.getConnection = function (cb) { @@ -138,6 +171,11 @@ Pool.prototype.releaseConnection = function releaseConnection(connection) { throw new Error('Connection already released'); } else { // add connection to end of free queue + if(this.config.idleTimeout > 0 && this._recycleTimer == null) { + this._recycleTimer = setInterval(recycleStaleConnections.bind(this),this.config.idleTimeout); + } + + connection._usedTimestamp = Date.now(); this._freeConnections.push(connection); this.emit('release', connection); } @@ -160,6 +198,10 @@ Pool.prototype.releaseConnection = function releaseConnection(connection) { Pool.prototype.end = function (cb) { this._closed = true; + if(this._recycleTimer) { + clearInterval(this._recycleTimer); + this._recycleTimer = null; + } if (typeof cb !== 'function') { cb = function (err) { diff --git a/lib/PoolConfig.js b/lib/PoolConfig.js index 8c5017a27..ce80e3477 100644 --- a/lib/PoolConfig.js +++ b/lib/PoolConfig.js @@ -20,6 +20,7 @@ function PoolConfig(options) { this.queueLimit = (options.queueLimit === undefined) ? 0 : Number(options.queueLimit); + this.idleTimeout = (options.idleTimeout === undefined)? 0 : Number(options.idleTimeout); } PoolConfig.prototype.newConnectionConfig = function newConnectionConfig() { From 105cee09fc7394439001e4f8ec5c8c5eff78419d Mon Sep 17 00:00:00 2001 From: Ankur Deep Jaiswal Date: Wed, 7 Jun 2017 11:43:25 +0530 Subject: [PATCH 2/5] fix Lint errors --- lib/Pool.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/Pool.js b/lib/Pool.js index ac002d208..0f9a34727 100644 --- a/lib/Pool.js +++ b/lib/Pool.js @@ -23,16 +23,16 @@ function Pool(options) { function recycleStaleConnections() { var now = Date.now(); var min = null; - for(var i=0 ;i= this.config.idleTimeout) { this._freeConnections[i].destroy(); i--; } else { if(min == null) { - min = now - lastused + min = now - lastused; } else { if((now - lastused) < min) { min = now - lastused; @@ -199,8 +199,8 @@ Pool.prototype.releaseConnection = function releaseConnection(connection) { Pool.prototype.end = function (cb) { this._closed = true; if(this._recycleTimer) { - clearInterval(this._recycleTimer); - this._recycleTimer = null; + clearInterval(this._recycleTimer); + this._recycleTimer = null; } if (typeof cb !== 'function') { From 8896669884a71dbbd56432748f0b9b9fcf794227 Mon Sep 17 00:00:00 2001 From: Ankur Deep Jaiswal Date: Wed, 7 Jun 2017 12:20:34 +0530 Subject: [PATCH 3/5] fix space space-infix-ops lint error --- lib/Pool.js | 2 +- lib/PoolConfig.js | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/Pool.js b/lib/Pool.js index 0f9a34727..8ce8156ba 100644 --- a/lib/Pool.js +++ b/lib/Pool.js @@ -23,7 +23,7 @@ function Pool(options) { function recycleStaleConnections() { var now = Date.now(); var min = null; - for(var i=0; i Date: Wed, 30 Aug 2017 12:11:04 +0530 Subject: [PATCH 4/5] Fix eslint errors --- lib/Pool.js | 16 ++++++++-------- lib/PoolConnection.js | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/Pool.js b/lib/Pool.js index 8ce8156ba..8ac540496 100644 --- a/lib/Pool.js +++ b/lib/Pool.js @@ -23,26 +23,26 @@ function Pool(options) { function recycleStaleConnections() { var now = Date.now(); var min = null; - for(var i = 0; i < this._freeConnections.length; i++ ) { + for (var i = 0; i < this._freeConnections.length; i++ ) { var lastused = this._freeConnections[i]._usedTimestamp; - if(lastused !== undefined) { - if((now - lastused) >= this.config.idleTimeout) { + if (lastused !== undefined) { + if ((now - lastused) >= this.config.idleTimeout) { this._freeConnections[i].destroy(); i--; } else { if(min == null) { min = now - lastused; } else { - if((now - lastused) < min) { + if ((now - lastused) < min) { min = now - lastused; } } } } } - if(min != null) { - if(min > 0) { + if (min != null) { + if (min > 0) { clearInterval(this._recycleTimer); this._recycleTimer = setInterval(recycleStaleConnections.bind(this),this.config.idleTimeout); } @@ -171,7 +171,7 @@ Pool.prototype.releaseConnection = function releaseConnection(connection) { throw new Error('Connection already released'); } else { // add connection to end of free queue - if(this.config.idleTimeout > 0 && this._recycleTimer == null) { + if (this.config.idleTimeout > 0 && this._recycleTimer == null) { this._recycleTimer = setInterval(recycleStaleConnections.bind(this),this.config.idleTimeout); } @@ -198,7 +198,7 @@ Pool.prototype.releaseConnection = function releaseConnection(connection) { Pool.prototype.end = function (cb) { this._closed = true; - if(this._recycleTimer) { + if (this._recycleTimer) { clearInterval(this._recycleTimer); this._recycleTimer = null; } diff --git a/lib/PoolConnection.js b/lib/PoolConnection.js index d29581804..f05fe0ec5 100644 --- a/lib/PoolConnection.js +++ b/lib/PoolConnection.js @@ -43,7 +43,7 @@ PoolConnection.prototype.end = function () { + 'deprecated. In next version calling conn.end() will be ' + 'restored to default conn.end() behavior. Use ' + 'conn.release() instead.' - ); + ); this.release(); }; From ead7dd4298c5f3ea9fd8f43b6b8646de8fe1e024 Mon Sep 17 00:00:00 2001 From: Ankur Deep Jaiswal Date: Wed, 30 Aug 2017 12:16:29 +0530 Subject: [PATCH 5/5] fix lint errors --- lib/Pool.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Pool.js b/lib/Pool.js index 8ac540496..0641342f8 100644 --- a/lib/Pool.js +++ b/lib/Pool.js @@ -31,7 +31,7 @@ function recycleStaleConnections() { this._freeConnections[i].destroy(); i--; } else { - if(min == null) { + if (min == null) { min = now - lastused; } else { if ((now - lastused) < min) {