Client support multiple servers
This commit is contained in:
parent
2a73ee749f
commit
7d52f867a0
|
@ -14,7 +14,7 @@ The code to create a new client looks like:
|
||||||
|
|
||||||
var ldap = require('ldapjs');
|
var ldap = require('ldapjs');
|
||||||
var client = ldap.createClient({
|
var client = ldap.createClient({
|
||||||
url: 'ldap://127.0.0.1:1389'
|
url: 'ldap://127.0.0.1:1389,127.0.0.2:1389'
|
||||||
});
|
});
|
||||||
|
|
||||||
You can use `ldap://` or `ldaps://`; the latter would connect over SSL (note
|
You can use `ldap://` or `ldaps://`; the latter would connect over SSL (note
|
||||||
|
@ -24,7 +24,7 @@ client is:
|
||||||
|
|
||||||
|Attribute |Description |
|
|Attribute |Description |
|
||||||
|---------------|-----------------------------------------------------------|
|
|---------------|-----------------------------------------------------------|
|
||||||
|url |A valid LDAP URL (proto/host/port only) |
|
|url |A valid LDAP URL(s) (proto/host(s)/port(s) |
|
||||||
|socketPath |Socket path if using AF\_UNIX sockets |
|
|socketPath |Socket path if using AF\_UNIX sockets |
|
||||||
|log |A compatible logger instance (Default: no-op logger) |
|
|log |A compatible logger instance (Default: no-op logger) |
|
||||||
|timeout |Milliseconds client should let operations live for before timing out (Default: Infinity)|
|
|timeout |Milliseconds client should let operations live for before timing out (Default: Infinity)|
|
||||||
|
|
|
@ -110,12 +110,17 @@ function Client (options) {
|
||||||
EventEmitter.call(this, options)
|
EventEmitter.call(this, options)
|
||||||
|
|
||||||
var self = this
|
var self = this
|
||||||
var _url
|
this.servers = []
|
||||||
if (options.url) { _url = url.parse(options.url) }
|
if (options.url) {
|
||||||
this.host = _url ? _url.hostname : undefined
|
var [, protocol, hosts, path] = options.url.match(/^([a-zA-Z]+:\/\/)([^/]*)(.*)/)
|
||||||
this.port = _url ? _url.port : false
|
|
||||||
this.secure = _url ? _url.secure : false
|
hosts.split(',').forEach((host) => {
|
||||||
this.url = _url
|
this.servers.push(url.parse(`${protocol}${host}${path}`))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// select random server at the start
|
||||||
|
this.nextServer = Math.floor(Math.random() * this.servers.length)
|
||||||
this.tlsOptions = options.tlsOptions
|
this.tlsOptions = options.tlsOptions
|
||||||
this.socketPath = options.socketPath || false
|
this.socketPath = options.socketPath || false
|
||||||
|
|
||||||
|
@ -792,6 +797,8 @@ Client.prototype.connect = function connect () {
|
||||||
|
|
||||||
// Establish basic socket connection
|
// Establish basic socket connection
|
||||||
function connectSocket (cb) {
|
function connectSocket (cb) {
|
||||||
|
self.nextServer = (self.nextServer + 1) % self.servers.length
|
||||||
|
self.url = self.servers[self.nextServer]
|
||||||
cb = once(cb)
|
cb = once(cb)
|
||||||
|
|
||||||
function onResult (err, res) {
|
function onResult (err, res) {
|
||||||
|
@ -820,12 +827,13 @@ Client.prototype.connect = function connect () {
|
||||||
setupClient(cb)
|
setupClient(cb)
|
||||||
}
|
}
|
||||||
|
|
||||||
var port = (self.port || self.socketPath)
|
var port = (self.servers.length && self.servers[self.nextServer].port) || self.socketPath
|
||||||
|
var hostname = (self.servers.length && self.servers[self.nextServer].hostname) || undefined
|
||||||
if (self.secure) {
|
if (self.secure) {
|
||||||
socket = tls.connect(port, self.host, self.tlsOptions)
|
socket = tls.connect(port, hostname, self.tlsOptions)
|
||||||
socket.once('secureConnect', onConnect)
|
socket.once('secureConnect', onConnect)
|
||||||
} else {
|
} else {
|
||||||
socket = net.connect(port, self.host)
|
socket = net.connect(port, hostname)
|
||||||
socket.once('connect', onConnect)
|
socket.once('connect', onConnect)
|
||||||
}
|
}
|
||||||
socket.once('error', onResult)
|
socket.once('error', onResult)
|
||||||
|
@ -975,6 +983,9 @@ Client.prototype.connect = function connect () {
|
||||||
maxDelay: this.reconnect.maxDelay
|
maxDelay: this.reconnect.maxDelay
|
||||||
})
|
})
|
||||||
failAfter = this.reconnect.failAfter
|
failAfter = this.reconnect.failAfter
|
||||||
|
if (this.servers.length > 1 && failAfter) {
|
||||||
|
failAfter *= this.servers.length
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
retry = backoff.exponential({
|
retry = backoff.exponential({
|
||||||
initialDelay: 1,
|
initialDelay: 1,
|
||||||
|
|
Loading…
Reference in New Issue