From d7ece5aa2de2583878395dbcbc05736433774abb Mon Sep 17 00:00:00 2001 From: Alex Varju Date: Fri, 18 Feb 2022 16:58:00 -0800 Subject: [PATCH 1/2] Listen for more socket failure events --- lib/memjs/server.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/memjs/server.js b/lib/memjs/server.js index ea04af9..cc00966 100644 --- a/lib/memjs/server.js +++ b/lib/memjs/server.js @@ -149,7 +149,7 @@ Server.prototype.sock = function(sasl, go) { }); // setup error handler - self._socket.on('error', function(error) { + var socketErrorListener = function(error) { self.connected = false; if (self.timeoutSet) { self._socket.setTimeout(0); @@ -157,7 +157,10 @@ Server.prototype.sock = function(sasl, go) { } self._socket = undefined; self.error(error); - }); + }; + self._socket.on('error', socketErrorListener); + self._socket.on('close', socketErrorListener); + self._socket.on('disconnect', socketErrorListener); // setup connection timeout handler self.timeoutSet = true; From f40126a210a1882064aa3c7f2c246c8aaa902b64 Mon Sep 17 00:00:00 2001 From: Amit Levy Date: Wed, 2 Mar 2022 10:34:46 -0500 Subject: [PATCH 2/2] Only call socket error callback on `close` event The `close` event is always called after an `error` event (https://nodejs.org/api/net.html#event-error_1), and `disconnect` is not an event emitted from a `net.Socket`. So we should only wait for a `close` event (which captures both `error` cases and non-`error` cases such as the server cleaning closing the connection with a FIN). This will avoid calling error callbacks twice (I believe this isn't technically a problem at the moment, but only calling `Server#error` once for a close should be more maintainable). --- lib/memjs/server.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/memjs/server.js b/lib/memjs/server.js index cc00966..a9e7f9c 100644 --- a/lib/memjs/server.js +++ b/lib/memjs/server.js @@ -149,7 +149,7 @@ Server.prototype.sock = function(sasl, go) { }); // setup error handler - var socketErrorListener = function(error) { + self._socket.on('close', function(error) { self.connected = false; if (self.timeoutSet) { self._socket.setTimeout(0); @@ -157,10 +157,7 @@ Server.prototype.sock = function(sasl, go) { } self._socket = undefined; self.error(error); - }; - self._socket.on('error', socketErrorListener); - self._socket.on('close', socketErrorListener); - self._socket.on('disconnect', socketErrorListener); + }); // setup connection timeout handler self.timeoutSet = true;