GH-30 Handle unbinds more gracefully
This commit is contained in:
parent
fdd3184420
commit
35f6b8a6c7
|
@ -31,6 +31,7 @@ var ModifyRequest = messages.ModifyRequest;
|
||||||
var ModifyDNRequest = messages.ModifyDNRequest;
|
var ModifyDNRequest = messages.ModifyDNRequest;
|
||||||
var SearchRequest = messages.SearchRequest;
|
var SearchRequest = messages.SearchRequest;
|
||||||
var UnbindRequest = messages.UnbindRequest;
|
var UnbindRequest = messages.UnbindRequest;
|
||||||
|
var UnbindResponse = messages.UnbindResponse;
|
||||||
|
|
||||||
var LDAPResult = messages.LDAPResult;
|
var LDAPResult = messages.LDAPResult;
|
||||||
var SearchEntry = messages.SearchEntry;
|
var SearchEntry = messages.SearchEntry;
|
||||||
|
@ -138,14 +139,31 @@ function Client(options) {
|
||||||
return self._log;
|
return self._log;
|
||||||
});
|
});
|
||||||
|
|
||||||
this.connection = this._newConnection();
|
|
||||||
this.reconnect = (typeof(options.reconnect) === 'number' ?
|
this.reconnect = (typeof(options.reconnect) === 'number' ?
|
||||||
options.reconnect : 1000);
|
options.reconnect : 1000);
|
||||||
|
|
||||||
|
this.connect();
|
||||||
}
|
}
|
||||||
util.inherits(Client, EventEmitter);
|
util.inherits(Client, EventEmitter);
|
||||||
module.exports = Client;
|
module.exports = Client;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connects this client, either at construct time, or after an unbind has
|
||||||
|
* been called. Under normal circumstances you don't need to call this method.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
Client.prototype.connect = function() {
|
||||||
|
if (this.connection)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var self = this;
|
||||||
|
this.connection = this._newConnection();
|
||||||
|
this.connection.on('end', function() {
|
||||||
|
delete self.connection;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs a simple authentication against the server.
|
* Performs a simple authentication against the server.
|
||||||
*
|
*
|
||||||
|
@ -675,6 +693,7 @@ Client.prototype._send = function(message, expect, callback, connection) {
|
||||||
};
|
};
|
||||||
} else if (expect === 'unbind') {
|
} else if (expect === 'unbind') {
|
||||||
_writeCb = function() {
|
_writeCb = function() {
|
||||||
|
conn.unbindMessageID = message.id;
|
||||||
conn.end();
|
conn.end();
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
|
@ -746,21 +765,35 @@ Client.prototype._newConnection = function() {
|
||||||
log.trace('%s close event had_err=%s', c.ldap.id, had_err ? 'yes' : 'no');
|
log.trace('%s close event had_err=%s', c.ldap.id, had_err ? 'yes' : 'no');
|
||||||
|
|
||||||
Object.keys(c.ldap.messages).forEach(function(msgid) {
|
Object.keys(c.ldap.messages).forEach(function(msgid) {
|
||||||
if (typeof(c.ldap.messages[msgid]) === 'function') {
|
var err;
|
||||||
var _cb = c.ldap.messages[msgid];
|
if (c.unbindMessageID !== parseInt(msgid, 10)) {
|
||||||
delete c.ldap.messages[msgid];
|
err = new ConnectionError(c.ldap.id + ' closed');
|
||||||
return _cb(new ConnectionError(c.ldap.id + ' closed'));
|
} else {
|
||||||
} else if (c.ldap.messages[msgid]) {
|
err = new UnbindResponse({
|
||||||
c.ldap.messages[msgid].emit('error', new ConnectionError(c.ldap.id +
|
messageID: msgid,
|
||||||
' closed'));
|
|
||||||
}
|
|
||||||
delete c.ldap.messages[msgid];
|
|
||||||
});
|
});
|
||||||
|
err.status = 'unbind';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof(c.ldap.messages[msgid]) === 'function') {
|
||||||
|
var callback = c.ldap.messages[msgid];
|
||||||
|
delete c.ldap.messages[msgid];
|
||||||
|
return callback(err);
|
||||||
|
} else if (c.ldap.messages[msgid]) {
|
||||||
|
if (err instanceof Error)
|
||||||
|
c.ldap.messages[msgid].emit('error', err);
|
||||||
|
delete c.ldap.messages[msgid];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
delete c.ldap;
|
delete c.ldap;
|
||||||
if (self.reconnect) {
|
if (self.reconnect) {
|
||||||
self.connection = null;
|
self.connection = null;
|
||||||
|
|
||||||
setTimeout(function() { self._newConnection() }, self.reconnect);
|
setTimeout(function() {
|
||||||
|
delete c.unbindMessageID;
|
||||||
|
self._newConnection();
|
||||||
|
}, self.reconnect);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -549,8 +549,19 @@ test('abandon (GH-27)', function(t) {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test('unbind and reconnect (GH-30)', function(t) {
|
||||||
|
client.unbind(function(err) {
|
||||||
|
t.ifError(err);
|
||||||
|
client.once('connect', function() {
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
client.connect();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
test('shutdown', function(t) {
|
test('shutdown', function(t) {
|
||||||
client.unbind(function() {
|
client.unbind(function(err) {
|
||||||
|
t.ifError(err);
|
||||||
server.on('close', function() {
|
server.on('close', function() {
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue