Skip to content

Commit 009ef77

Browse files
authored
Merge pull request #137 from Gertt/fix/terminated-connections
remove terminated connections from pool
2 parents 257bf12 + c18854e commit 009ef77

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/v1/internal/ch-node.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ class NodeChannel {
280280
});
281281

282282
self._conn.on('error', self._handleConnectionError);
283+
self._conn.on('end', self._handleConnectionTerminated);
283284

284285
// Drain all pending messages
285286
let pending = self._pending;
@@ -297,6 +298,13 @@ class NodeChannel {
297298
}
298299
}
299300

301+
_handleConnectionTerminated() {
302+
this._error = new Error('Connection was closed by server');
303+
if( this.onerror ) {
304+
this.onerror(this._error);
305+
}
306+
}
307+
300308
isEncrypted() {
301309
return this._encrypted;
302310
}
@@ -326,6 +334,7 @@ class NodeChannel {
326334
this._open = false;
327335
if( this._conn ) {
328336
this._conn.end();
337+
this._conn.removeListener('end', this._handleConnectionTerminated);
329338
this._conn.on('end', cb);
330339
} else {
331340
cb();

src/v1/internal/pool.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,16 @@ class Pool {
4040
}
4141

4242
acquire() {
43-
if( this._pool.length > 0 ) {
44-
return this._pool.pop();
45-
} else {
46-
return this._create( this._release );
43+
let resource;
44+
while( this._pool.length ) {
45+
resource = this._pool.pop();
46+
47+
if( this._validate(resource) ) {
48+
return resource;
49+
}
4750
}
51+
52+
return this._create(this._release);
4853
}
4954

5055
_release(resource) {

0 commit comments

Comments
 (0)