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

Connection management (Client API) #317

Closed
felixfbecker opened this issue Nov 3, 2015 · 4 comments
Closed

Connection management (Client API) #317

felixfbecker opened this issue Nov 3, 2015 · 4 comments
Labels

Comments

@felixfbecker
Copy link

The docs don't name any way to manage the connection. If you just do

ldap.createClient({url: 'ldap://localhost:9000'})

without a running server on port 9000 will crash the process with an ECONNREFUSED. Looking at the source code, to prevent this, you would have to attach an onerror listener and check for ECONNREFUSED. If you want to make sure the connection is established, you apparently need to listen for connect. None of this is mentioned in the docs :(

I saw that there is a connect() method that is automatically called when the LdapClient is created. But it doesn't take a callback. In other modules like node-mysql you can create the connection object first and then call connect() with a callback that gets called when the connection is established or with an error.

Similar to this, how do you close the connection? In other modules you have close() or end() with a callback.

(Btw thank you for making/maintaining this module, LDAP is so essential for my applications, without it NodeJS wouldn't be an option)

@pfmooney
Copy link
Contributor

pfmooney commented Nov 3, 2015

Correct, ldapjs consumers are expected to use the error and connect events for monitoring state.
The unbind method is typically used for ending a client connection.

The client documentation is admittedly lacking. I've been trying to improve it over time.

@felixfbecker
Copy link
Author

Thanks for the reply.
Is the error event only emitted for connection-related errors? Or do I need to check the error code?
Does unbind destroy the underlying socket or is there something else I need to call, like destroy() or close() or something like that?
I'm trying to set up a pool with common-pool like this:

const ldapPool = new Pool(
  // create function
  cb => {
    const client = ldap.createClient({url: process.env.LDAP})
    client.once('connect', () => cb(null, client))
    client.once('error', err => cb(err))
  },
  // destroy function
  client => {
    client.unbind(function noop() {})
  },
  10, // max connections
  ms('1 hour'), // max idle time
  ms('10 seconds') // min time between reconnects
)

@pfmooney
Copy link
Contributor

pfmooney commented Nov 4, 2015

Generally speaking, yes. Events which cause the client to become unavailable should be emitted via the error event. There is no need to check the result of unbind. (It will close the socket on your behalf after sending a message to the server)

There are (albeit poorly documented) built-in mechanisms in the client for reconnect and idleness detection. You might want to look around in the source code.

@jsumners
Copy link
Member

⚠️ This issue has been locked due to age. If you have encountered a recent
problem that seems to be covered by this issue, please open a new issue.

Please include a minimal reproducible example
when opening a new issue.

@ldapjs ldapjs locked as resolved and limited conversation to collaborators Mar 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants