getConnection method

Future<LdapConnection> getConnection({
  1. bool bind = false,
})

Return a LdapConnection from the pool. If bind is true An ldap bind will be performed using the credentials provided in the original LdapConnection used to create the pool.

Implementation

Future<LdapConnection> getConnection({bool bind = false}) async {
  loggerPool.finest('pool getConnection bind=$bind');
  var c =
      _connections.firstWhereOrNull((c) => c.connectionInfo.inUse == false);
  if (c == null) {
    throw LdapPoolException(
        'No available connections in the pool. Does your code leak connections?');
  }

  if (c.state == ConnectionState.closed) {
    await _open(c);
  }

  // If bind is true and the connection is not already bound...
  if (bind && c.state != ConnectionState.bound) {
    var result = await c.bind();
    if (result.resultCode != ResultCode.OK) {
      throw LdapPoolException('LdapPool can not bind connection', result);
    }
  }
  c.connectionInfo.inUse = true;
  return c;
}