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

@ -164,6 +164,11 @@ function setupSocket(socket, opts) {
if (log.trace()) if (log.trace())
log.trace({err: err}, 'error event: %s', new Error().stack); log.trace({err: err}, 'error event: %s', new Error().stack);
if (opts.connectTimer) {
clearTimeout(opts.connectTimer);
opts.connectTimer = false;
}
if (opts.listeners('error').length) if (opts.listeners('error').length)
opts.emit('error', err); opts.emit('error', err);
@ -706,11 +711,11 @@ Client.prototype._connect = function _connect() {
var proto = this.secure ? tls : net; var proto = this.secure ? tls : net;
var self = this; var self = this;
var socket = null; var socket = null;
var timer = false; this.connectTimer = false;
function onConnect() { function onConnect() {
if (timer) if (self.connectTimer)
clearTimeout(timer); clearTimeout(self.connectTimer);
socket.removeListener('connect', onConnect); socket.removeListener('connect', onConnect);
socket.removeListener('secureConnect', onConnect); socket.removeListener('secureConnect', onConnect);
@ -732,7 +737,7 @@ Client.prototype._connect = function _connect() {
setupSocket(socket, this); setupSocket(socket, this);
if (this.connectTimeout) { if (this.connectTimeout) {
timer = setTimeout(function onConnectTimeout() { this.connectTimer = setTimeout(function onConnectTimeout() {
if (!socket || !socket.readable || !socket.writeable) { if (!socket || !socket.readable || !socket.writeable) {
socket.destroy(); socket.destroy();

View File

@ -31,7 +31,7 @@ var RETURN_VAL_OPS = [
///--- Internal Functions ///--- Internal Functions
function createPool(options) { function createPool(options, self) {
assert.ok(options); assert.ok(options);
return pooling.createPool({ return pooling.createPool({
@ -46,9 +46,16 @@ function createPool(options) {
client.on('error', function (err) { client.on('error', function (err) {
client.removeAllListeners('connect'); client.removeAllListeners('connect');
client.removeAllListeners('connectTimeout');
self.emit('error', err);
return callback(err); return callback(err);
}); });
client.on('connectTimeout', function () {
client.removeAllListeners('connect');
self.emit('connectTimeout');
});
client.once('connect', function onConnect() { client.once('connect', function onConnect() {
client.removeAllListeners('error'); client.removeAllListeners('error');
@ -118,7 +125,7 @@ function ClientPool(options) {
url: options.url, url: options.url,
tlsOptions: options.tlsOptions tlsOptions: options.tlsOptions
}; };
this.pool = createPool(options); this.pool = createPool(options, this);
} }
util.inherits(ClientPool, EventEmitter); util.inherits(ClientPool, EventEmitter);
module.exports = ClientPool; module.exports = ClientPool;