noirello / bonsai

Simple Python 3 module for LDAP, using libldap2 and winldap C libraries.
MIT License
116 stars 32 forks source link

Is there a way to make the connection auto reconnect while network restored? #51

Closed programus closed 2 years ago

programus commented 3 years ago

In an unstable network environment, once the network down, the LDAP search will never success even after the network connection is back. Is there an auto reconnect flag or some other ways to make searches work after the network is restored?

import bonsai

client = bonsai.LDAPClient(ldap_url)
conn = client.connect(timeout=3)
conn.search(base, scope, filter, timeout=3)  # here it works
# here the network was gone
conn.search(base, scope, filter, timeout=3) # timeout error, of course
# here the network restored
conn.search(base, scope, filter, timeout=3) # still timeout error! I hope it can work here.
print(conn.closed)  # it is False here, so I cannot manually reconnect it with a check of close status
noirello commented 3 years ago

Tried to reproduce this with a macOS client and an OpenLDAP running in a docker container, using 100% packet loss on the container's interface. But after I dropped the settings the search gave me a result. The only thing I noticed that the network didn't get back to normal instantly (had to wait a couple secs). Sometimes (using the same pattern that you did) my third try gave me a TimeoutError just like in your case, but then the fourth search gave me a result without manual reconnecting.