Merge client-pool-events from pfmooney/node-ldapjs

Fix mcavage/node-ldapjs#87
This commit is contained in:
Patrick Mooney 2013-11-28 14:09:28 -06:00
commit dfbb6031c6
2 changed files with 19 additions and 7 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);
@ -734,7 +739,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, clientpool) {
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');
clientpool.emit('error', err);
return callback(err); return callback(err);
}); });
client.on('connectTimeout', function () {
client.removeAllListeners('connect');
clientpool.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;
@ -241,7 +248,7 @@ ClientPool.prototype.bind = function bind(dn, creds, controls, callback) {
self.options.bindControls = null; self.options.bindControls = null;
return this.pool.shutdown(function () { return this.pool.shutdown(function () {
self.pool = createPool(self.options); self.pool = createPool(self.options, self);
return self.pool.acquire(function onAcquire(err, client) { return self.pool.acquire(function onAcquire(err, client) {
if (err) if (err)