diff --git a/src/v1/internal/ch-node.js b/src/v1/internal/ch-node.js index d4629652f..c48b2184a 100644 --- a/src/v1/internal/ch-node.js +++ b/src/v1/internal/ch-node.js @@ -280,6 +280,7 @@ class NodeChannel { }); self._conn.on('error', self._handleConnectionError); + self._conn.on('end', self._handleConnectionTerminated); // Drain all pending messages let pending = self._pending; @@ -297,6 +298,13 @@ class NodeChannel { } } + _handleConnectionTerminated() { + this._error = new Error('Connection was closed by server'); + if( this.onerror ) { + this.onerror(this._error); + } + } + isEncrypted() { return this._encrypted; } @@ -326,6 +334,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..ed53bdf05 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) {