idleListener no longer grabs references to things it doesn't need (#83)

This commit is contained in:
Bryan Clement 2019-01-02 10:10:42 -08:00 committed by Brian C
parent 35a285c9a7
commit f91769538d

View File

@ -65,6 +65,20 @@ function promisify (Promise, callback) {
return { callback: cb, result: result }
}
function makeIdleListener (pool, client) {
return function idleListener (err) {
err.client = client
client.removeListener('error', idleListener)
client.on('error', () => {
pool.log('additional client error after disconnection due to error', err)
})
pool._remove(client)
// TODO - document that once the pool emits an error
// the client has already been closed & purged and is unusable
pool.emit('error', err, client)
}
}
class Pool extends EventEmitter {
constructor (options, Client) {
super()
@ -197,17 +211,7 @@ class Pool extends EventEmitter {
newClient (pendingItem) {
const client = new this.Client(this.options)
this._clients.push(client)
const idleListener = (err) => {
err.client = client
client.removeListener('error', idleListener)
client.on('error', () => {
this.log('additional client error after disconnection due to error', err)
})
this._remove(client)
// TODO - document that once the pool emits an error
// the client has already been closed & purged and is unusable
this.emit('error', err, client)
}
const idleListener = makeIdleListener(this, client)
this.log('checking client timeout')