From 40f5126b6ef16d80c03a3001b0802824dbbe2af5 Mon Sep 17 00:00:00 2001 From: Brian C Date: Fri, 14 Jul 2017 14:07:07 -0500 Subject: [PATCH] Fix idle client teardown on error (#68) - Re-add default idleTimeoutMillis = 10000 by default - Fix idle timeout clearing on shutdown - Ensure idleTimeoutMillis is used in timeout --- index.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index c317e531..49d797be 100644 --- a/index.js +++ b/index.js @@ -16,7 +16,7 @@ function throwOnRelease () { function release (client, err) { client.release = throwOnRelease - if (err) { + if (err || this.ending) { this._remove(client) this._pulseQueue() return @@ -28,14 +28,10 @@ function release (client, err) { tid = setTimeout(() => { this.log('remove idle client') this._remove(client) - }, this.idleTimeoutMillis) + }, this.options.idleTimeoutMillis) } - if (this.ending) { - this._remove(client) - } else { - this._idle.push(new IdleItem(client, tid)) - } + this._idle.push(new IdleItem(client, tid)) this._pulseQueue() } @@ -64,6 +60,10 @@ class Pool extends EventEmitter { this.Client = this.options.Client || Client || require('pg').Client this.Promise = this.options.Promise || global.Promise + if (typeof this.options.idleTimeoutMillis === 'undefined') { + this.options.idleTimeoutMillis = 10000 + } + this._clients = [] this._idle = [] this._pendingQueue = [] @@ -114,7 +114,10 @@ class Pool extends EventEmitter { } _remove (client) { - this._idle = this._idle.filter(item => item.client !== client) + this._idle = this._idle.filter(item => { + clearTimeout(item.timeoutId) + return item.client !== client + }) this._clients = this._clients.filter(c => c !== client) client.end() this.emit('remove', client)