How determine if persistent Net::LDAP object is alive?

[email protected] (Nick Urbanik) Wed, 22 Feb 2012 16:20:47 +1100
Newsgroups perl.ldap
Message-ID <[email protected]>
Dear Folks,

We use a lot of busy OpenLDAP servers and a lot of Perl, with
persistent Net::LDAP connections to the servers.  The problem is:
How good is the connection to the server?

I found that:

{
     my $ldap;
     sub ldapopen {
         if ( not $ldap ) {
             $ldap = Net::LDAP->new('localhost');
         }
         return $ldap;
     }
}

doesn't work to determine the viability of the connection.  Truth is
insufficient.  

So next we tried
if ( not $ldap or not $ldap->socket ) {
    ...
}

and still this didn't catch timed out connections.

Now we are doing things like this:
[$BASE is the DN of the top-level "suffix" entry]

{
     my $ldap;
     if ( $ldap and $ldap->socket ) {
         my $result = $ldap->search(
             base => $BASE,
             scope => 'base',
             filter => '(objectClass=*)',
         );
         loginfo "Testing result of search for $BASE";
         if ( $result and not $result->code ) {
             return $ldap if $result->count() == 1;
         }
     }
     loginfo '(Re)connecting to LDAP server';
     $ldap = Net::LDAP->new( 'localhost' );
     return $ldap;
}

which is two searches for one, rather overkill.

Radiator, the Perl RADIUS server from open.com uses a rather complex
piece of code to check the life of the socket.

How does everyone else cope with this problem?

If we use non-persistent connections, then we run out of TCP sockets
under heavy load.
-- 
Nick Urbanik http://nicku.org 808-71011 [email protected]
GPG: 7FFA CDC7 5A77 0558 DC7A 790A 16DF EC5B BB9D 2C24  ID: BB9D2C24
I disclaim, therefore I am.
signature.asc (application/pgp-signature, 198 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAk9Eey4ACgkQFt/sW7udLCS+kwCdFgRQjzqsYUOlvU/a1c5K9wPv
zXsAn3wjhp3SlEYtbKkrVASA4AaYnfj8
=MXOg
-----END PGP SIGNATURE-----