This commit is contained in:
Martin Cizek 2020-09-17 16:46:34 +02:00
parent 543cb976f3
commit 23e44b2959
3 changed files with 25 additions and 38 deletions

View File

@ -24,7 +24,7 @@ client is:
|Attribute |Description |
|---------------|-----------------------------------------------------------|
|url |A valid LDAP URL(s) (proto/host(s)/port(s) |
|url |A string or array of valid LDAP URL(s) (proto/host/port) |
|socketPath |Socket path if using AF\_UNIX sockets |
|log |A compatible logger instance (Default: no-op logger) |
|timeout |Milliseconds client should let operations live for before timing out (Default: Infinity)|

View File

@ -110,23 +110,14 @@ function Client (options) {
EventEmitter.call(this, options)
var self = this
this.servers = []
if (options.url) {
var urls = options.url
if (typeof options.url === 'string') {
var [, protocol, hosts, path] = options.url.match(/^([a-zA-Z]+:\/\/)([^/]*)(.*)/)
urls = hosts.split(',').map((host) => {
return `${protocol}${host}${path}`
})
}
this.servers = urls.map(url.parse)
}
this.urls = options.url ? [].concat(options.url).map(url.parse) : []
// select random server at the start
this.nextServer = Math.floor(Math.random() * this.servers.length)
this._nextServer = Math.floor(Math.random() * this.urls.length)
// updated in connectSocket() after each connect
this.host = undefined
this.port = false
this.secure = false
this.url = undefined
this.tlsOptions = options.tlsOptions
this.socketPath = options.socketPath || false
@ -803,8 +794,15 @@ Client.prototype.connect = function connect () {
// Establish basic socket connection
function connectSocket (cb) {
self.nextServer = (self.nextServer + 1) % self.servers.length
self.url = self.servers[self.nextServer]
self._nextServer = (self._nextServer + 1) % self.urls.length
self.url = self.urls[self._nextServer]
if (self.url) {
self.host = url.hostname
self.port = url.port
self.secure = url.secure
}
cb = once(cb)
function onResult (err, res) {
@ -833,8 +831,8 @@ Client.prototype.connect = function connect () {
setupClient(cb)
}
var port = (self.servers.length && self.servers[self.nextServer].port) || self.socketPath
var hostname = (self.servers.length && self.servers[self.nextServer].hostname) || undefined
var port = (self.urls.length && self.urls[self._nextServer].port) || self.socketPath
var hostname = (self.urls.length && self.urls[self._nextServer].hostname) || undefined
if (self.secure) {
socket = tls.connect(port, hostname, self.tlsOptions)
socket.once('secureConnect', onConnect)
@ -843,7 +841,7 @@ Client.prototype.connect = function connect () {
socket.once('connect', onConnect)
}
socket.once('error', onResult)
initSocket()
initSocket(self.url)
// Setup connection timeout handling, if desired
if (self.connectTimeout) {
@ -858,9 +856,9 @@ Client.prototype.connect = function connect () {
}
// Initialize socket events and LDAP parser.
function initSocket () {
function initSocket (url) {
tracker = messageTrackerFactory({
id: self.url ? self.url.href : self.socketPath,
id: url ? url.href : self.socketPath,
parser: new Parser({ log: log })
})
@ -989,8 +987,8 @@ Client.prototype.connect = function connect () {
maxDelay: this.reconnect.maxDelay
})
failAfter = this.reconnect.failAfter
if (this.servers.length > 1 && failAfter) {
failAfter *= this.servers.length
if (this.urls.length > 1 && failAfter) {
failAfter *= this.urls.length
}
} else {
retry = backoff.exponential({

View File

@ -388,18 +388,7 @@ tap.test('createClient', t => {
connectTimeout: 1
})
t.equal(client.servers.length, 2)
})
})
t.test('url string is correctly parsed and assigned', async t => {
getPort().then(function (unusedPortNumber) {
const client = ldap.createClient({
url: `ldap://0.0.0.0:${unusedPortNumber},0.0.0.1:${unusedPortNumber}`,
connectTimeout: 1
})
t.equal(client.servers.length, 2)
t.equal(client.urls.length, 2)
})
})