Skip to content

Commit aa19f19

Browse files
authored
Merge pull request #138 from pontusmelke/1.0-backport-terminated-connection-fix
Backport fix for #136 to 1.0
2 parents 1bb8d23 + 6e2725d commit aa19f19

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/v1/internal/ch-node.js

+9
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ class NodeChannel {
276276
});
277277

278278
self._conn.on('error', self._handleConnectionError);
279+
self._conn.on('end', self._handleConnectionTerminated);
279280

280281
// Drain all pending messages
281282
let pending = self._pending;
@@ -293,6 +294,13 @@ class NodeChannel {
293294
}
294295
}
295296

297+
_handleConnectionTerminated() {
298+
this._error = new Error('Connection was closed by server');
299+
if( this.onerror ) {
300+
this.onerror(this._error);
301+
}
302+
}
303+
296304
/**
297305
* Write the passed in buffer to connection
298306
* @param {NodeBuffer} buffer - Buffer to write
@@ -318,6 +326,7 @@ class NodeChannel {
318326
this._open = false;
319327
if( this._conn ) {
320328
this._conn.end();
329+
this._conn.removeListener('end', this._handleConnectionTerminated);
321330
this._conn.on('end', cb);
322331
} else {
323332
cb();

src/v1/internal/pool.js

+9-4
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)