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
This commit is contained in:
Brian C 2017-07-14 14:07:07 -05:00 committed by GitHub
parent 2421a769cb
commit 40f5126b6e

View File

@ -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)