From 6e2725d3e946483c7df1135e74724359442ada20 Mon Sep 17 00:00:00 2001 From: Pontus Melke Date: Wed, 7 Sep 2016 10:54:40 +0200 Subject: [PATCH] Backport fix for #136 to 1.0 --- src/v1/internal/ch-node.js | 9 +++++++++ src/v1/internal/pool.js | 13 +++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) 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) {