Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Commit 7d52f86

Browse files
author
Martin Cizek
committed
Client support multiple servers
1 parent 2a73ee7 commit 7d52f86

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

docs/client.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The code to create a new client looks like:
1414

1515
var ldap = require('ldapjs');
1616
var client = ldap.createClient({
17-
url: 'ldap://127.0.0.1:1389'
17+
url: 'ldap://127.0.0.1:1389,127.0.0.2:1389'
1818
});
1919

2020
You can use `ldap://` or `ldaps://`; the latter would connect over SSL (note
@@ -24,7 +24,7 @@ client is:
2424

2525
|Attribute |Description |
2626
|---------------|-----------------------------------------------------------|
27-
|url |A valid LDAP URL (proto/host/port only) |
27+
|url |A valid LDAP URL(s) (proto/host(s)/port(s) |
2828
|socketPath |Socket path if using AF\_UNIX sockets |
2929
|log |A compatible logger instance (Default: no-op logger) |
3030
|timeout |Milliseconds client should let operations live for before timing out (Default: Infinity)|

lib/client/client.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,17 @@ function Client (options) {
110110
EventEmitter.call(this, options)
111111

112112
var self = this
113-
var _url
114-
if (options.url) { _url = url.parse(options.url) }
115-
this.host = _url ? _url.hostname : undefined
116-
this.port = _url ? _url.port : false
117-
this.secure = _url ? _url.secure : false
118-
this.url = _url
113+
this.servers = []
114+
if (options.url) {
115+
var [, protocol, hosts, path] = options.url.match(/^([a-zA-Z]+:\/\/)([^/]*)(.*)/)
116+
117+
hosts.split(',').forEach((host) => {
118+
this.servers.push(url.parse(`${protocol}${host}${path}`))
119+
})
120+
}
121+
122+
// select random server at the start
123+
this.nextServer = Math.floor(Math.random() * this.servers.length)
119124
this.tlsOptions = options.tlsOptions
120125
this.socketPath = options.socketPath || false
121126

@@ -792,6 +797,8 @@ Client.prototype.connect = function connect () {
792797

793798
// Establish basic socket connection
794799
function connectSocket (cb) {
800+
self.nextServer = (self.nextServer + 1) % self.servers.length
801+
self.url = self.servers[self.nextServer]
795802
cb = once(cb)
796803

797804
function onResult (err, res) {
@@ -820,12 +827,13 @@ Client.prototype.connect = function connect () {
820827
setupClient(cb)
821828
}
822829

823-
var port = (self.port || self.socketPath)
830+
var port = (self.servers.length && self.servers[self.nextServer].port) || self.socketPath
831+
var hostname = (self.servers.length && self.servers[self.nextServer].hostname) || undefined
824832
if (self.secure) {
825-
socket = tls.connect(port, self.host, self.tlsOptions)
833+
socket = tls.connect(port, hostname, self.tlsOptions)
826834
socket.once('secureConnect', onConnect)
827835
} else {
828-
socket = net.connect(port, self.host)
836+
socket = net.connect(port, hostname)
829837
socket.once('connect', onConnect)
830838
}
831839
socket.once('error', onResult)
@@ -975,6 +983,9 @@ Client.prototype.connect = function connect () {
975983
maxDelay: this.reconnect.maxDelay
976984
})
977985
failAfter = this.reconnect.failAfter
986+
if (this.servers.length > 1 && failAfter) {
987+
failAfter *= this.servers.length
988+
}
978989
} else {
979990
retry = backoff.exponential({
980991
initialDelay: 1,

0 commit comments

Comments
 (0)