Skip to content

Commit

Permalink
Restore lazy-socket's ability to reconnect on terminated connections
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathansamines committed Jul 8, 2022
1 parent a3c9d47 commit 7700021
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/LazySocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function LazySocket(properties) {
this.host = properties.host;

this._socket = null;
this._closed = false;
this._callbacks = [];
}

Expand Down Expand Up @@ -57,15 +58,28 @@ LazySocket.prototype._lazyConnect = function() {
});
}

function onend() {
// "end" is called when the socket connection is terminated, regardless of the termination being unexpected or not
// to distinguish between unexpected terminations (e.g need reconnection)
// from expected terminations (e.g calling LazySocket's .end() or .destroy()), the later are flagged as "closed"

if (!self._closed) {
self._socket = null;
}
}

this._socket = net
.createConnection(this.port, this.host)
.once('error', onerror);
.once('error', onerror)
.once('end', onend);
};

LazySocket.prototype.end = function() {
this._closed = true;
if (this._socket) this._socket.end();
};

LazySocket.prototype.destroy = function() {
this._closed = true;
if (this._socket) this._socket.destroy();
};

0 comments on commit 7700021

Please sign in to comment.