From 15f03a4cb4b8f66f1bae912ce09a1765e9686392 Mon Sep 17 00:00:00 2001 From: Tony Mobily Date: Fri, 3 Oct 2014 16:27:19 +0800 Subject: [PATCH 1/2] Added socketTimeout option, which ensure that OS-level timeout is handled for all sockets handled by the library --- lib/Connection.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/Connection.js b/lib/Connection.js index c4c43cf..d51b74d 100644 --- a/lib/Connection.js +++ b/lib/Connection.js @@ -56,6 +56,7 @@ function Connection(config) { this._config = { socket: config.socket, + socketTimeout: config.socketTimeout, host: config.host || 'localhost', port: config.port || 143, tls: config.tls, @@ -98,7 +99,6 @@ Connection.prototype.connect = function() { socket = config.socket || new Socket(); socket.setKeepAlive(true); - socket.setTimeout(0); this._sock = undefined; this._tagcount = 0; this._tmrConn = undefined; @@ -150,6 +150,21 @@ Connection.prototype.connect = function() { }; this._sock.on('error', this._onError); + this._onSocketTimeout = function(){ + clearTimeout(self._tmrConn); + clearTimeout(self._tmrAuth); + clearTimeout(self._tmrKeepalive); + self.state = 'disconnected'; + self.debug && self.debug('[connection] Socket timeout'); + + var err = new Error('Socket timed out while talking to server'); + err.source = 'timeout'; + self.emit('error', err); + socket.destroy(); + }; + this._sock.on('timeout', this._onSocketTimeout); + socket.setTimeout(config.socketTimeout); + socket.once('close', function(had_err) { clearTimeout(self._tmrConn); clearTimeout(self._tmrAuth); @@ -1675,6 +1690,9 @@ Connection.prototype._starttls = function() { }); self._sock.on('error', self._onError); + self._sock.on('timeout', this._onSocketTimeout); + self._sock.setTimeout(config.socketTimeout); + self._parser.setStream(self._sock); }); }; From b98b9e39975ab2bd5f11b119aefb7f55d595a8a2 Mon Sep 17 00:00:00 2001 From: Tony Mobily Date: Fri, 3 Oct 2014 21:26:54 +0800 Subject: [PATCH 2/2] Applied a default to config.socketTimeout, renamed error name to something more specific, added space for style --- lib/Connection.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Connection.js b/lib/Connection.js index d51b74d..eee5cbb 100644 --- a/lib/Connection.js +++ b/lib/Connection.js @@ -56,7 +56,7 @@ function Connection(config) { this._config = { socket: config.socket, - socketTimeout: config.socketTimeout, + socketTimeout: config.socketTimeout || 0, host: config.host || 'localhost', port: config.port || 143, tls: config.tls, @@ -150,7 +150,7 @@ Connection.prototype.connect = function() { }; this._sock.on('error', this._onError); - this._onSocketTimeout = function(){ + this._onSocketTimeout = function() { clearTimeout(self._tmrConn); clearTimeout(self._tmrAuth); clearTimeout(self._tmrKeepalive); @@ -158,7 +158,7 @@ Connection.prototype.connect = function() { self.debug && self.debug('[connection] Socket timeout'); var err = new Error('Socket timed out while talking to server'); - err.source = 'timeout'; + err.source = 'socket-timeout'; self.emit('error', err); socket.destroy(); };