diff --git a/src/v1/internal/ch-node.js b/src/v1/internal/ch-node.js index 62930b0a4..f043cb607 100644 --- a/src/v1/internal/ch-node.js +++ b/src/v1/internal/ch-node.js @@ -276,6 +276,7 @@ class NodeChannel { }); self._conn.on('error', self._handleConnectionError); + self._conn.on('end', self._handleConnectionTerminated); // Drain all pending messages let pending = self._pending; @@ -293,6 +294,13 @@ class NodeChannel { } } + _handleConnectionTerminated() { + this._error = new Error('Connection was closed by server'); + if( this.onerror ) { + this.onerror(this._error); + } + } + /** * Write the passed in buffer to connection * @param {NodeBuffer} buffer - Buffer to write @@ -318,6 +326,7 @@ class NodeChannel { this._open = false; if( this._conn ) { this._conn.end(); + this._conn.removeListener('end', this._handleConnectionTerminated); this._conn.on('end', cb); } else { cb(); diff --git a/src/v1/internal/pool.js b/src/v1/internal/pool.js index 494ce239a..0f9bcbe41 100644 --- a/src/v1/internal/pool.js +++ b/src/v1/internal/pool.js @@ -40,11 +40,16 @@ class Pool { } acquire() { - if( this._pool.length > 0 ) { - return this._pool.pop(); - } else { - return this._create( this._release ); + let resource; + while (this._pool.length) { + resource = this._pool.pop(); + + if (this._validate(resource)) { + return resource; + } } + + return this._create(this._release); } _release(resource) {