Fix some ClientPool event handling

Emit connectTimeout and error events from ClientPool as Client would.
This commit is contained in:
Patrick Mooney 2013-10-24 21:04:50 -05:00
parent 0d12eef3ff
commit e03d40957e
2 changed files with 18 additions and 6 deletions

View File

@ -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();

View File

@ -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;