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); }); };