diff --git a/lib/client/client.js b/lib/client/client.js index 9dda196..8cd2d34 100644 --- a/lib/client/client.js +++ b/lib/client/client.js @@ -163,6 +163,11 @@ function setupSocket(socket, opts) { socket.on('error', function onError(err) { if (log.trace()) log.trace({err: err}, 'error event: %s', new Error().stack); + + if (opts.connectTimer) { + clearTimeout(opts.connectTimer); + opts.connectTimer = false; + } if (opts.listeners('error').length) opts.emit('error', err); @@ -706,11 +711,11 @@ Client.prototype._connect = function _connect() { var proto = this.secure ? tls : net; var self = this; var socket = null; - var timer = false; + this.connectTimer = false; function onConnect() { - if (timer) - clearTimeout(timer); + if (self.connectTimer) + clearTimeout(self.connectTimer); socket.removeListener('connect', onConnect); socket.removeListener('secureConnect', onConnect); @@ -732,7 +737,7 @@ Client.prototype._connect = function _connect() { setupSocket(socket, this); if (this.connectTimeout) { - timer = setTimeout(function onConnectTimeout() { + this.connectTimer = setTimeout(function onConnectTimeout() { if (!socket || !socket.readable || !socket.writeable) { socket.destroy(); diff --git a/lib/client/pool.js b/lib/client/pool.js index aa2c601..4e7900e 100644 --- a/lib/client/pool.js +++ b/lib/client/pool.js @@ -31,7 +31,7 @@ var RETURN_VAL_OPS = [ ///--- Internal Functions -function createPool(options) { +function createPool(options, self) { assert.ok(options); return pooling.createPool({ @@ -46,9 +46,16 @@ function createPool(options) { client.on('error', function (err) { client.removeAllListeners('connect'); + client.removeAllListeners('connectTimeout'); + self.emit('error', err); return callback(err); }); + client.on('connectTimeout', function () { + client.removeAllListeners('connect'); + self.emit('connectTimeout'); + }); + client.once('connect', function onConnect() { client.removeAllListeners('error'); @@ -118,7 +125,7 @@ function ClientPool(options) { url: options.url, tlsOptions: options.tlsOptions }; - this.pool = createPool(options); + this.pool = createPool(options, this); } util.inherits(ClientPool, EventEmitter); module.exports = ClientPool;