Skip to content

Commit 665dc81

Browse files
committed
rebased for easier integration
1 parent 5c0d549 commit 665dc81

File tree

3 files changed

+556
-37
lines changed

3 files changed

+556
-37
lines changed

Lib/ldappool/__init__.py

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,8 @@ def __whoami(self):
8282
try:
8383
self.__enter__()
8484
except:
85-
pass
86-
raise ldap.SERVER_DOWN(
87-
f"max retries {self.params.get('retries', 3)} reached"
88-
)
85+
continue
86+
raise ldap.SERVER_DOWN(f"max retries {self.params.get('retries', 3)} reached")
8987

9088
@property
9189
def conn(self):
@@ -188,6 +186,7 @@ def __enter__(self):
188186
)
189187
try:
190188
self._conn = ldap.initialize(self.uri.initializeUrl())
189+
self.__set_connection_parameters__()
191190
if self.params.get("autoBind", False):
192191
(
193192
f"ConnectionPool {self} autoBind with {self.binddn} password {'x'*len(self.bindpw)}"
@@ -199,8 +198,19 @@ def __enter__(self):
199198
self.established = True
200199
return self.conn
201200

202-
def giveback(self):
201+
def giveback(self, force=False):
203202
try:
203+
if force:
204+
try:
205+
self._conn.unbind_s()
206+
except Exception as ldaperr:
207+
logging.error(
208+
"ConnectionPool unbind connection"
209+
+ f"{self} exception {ldaperr}"
210+
)
211+
self.inUse = False
212+
return
213+
204214
if self.params.get("autoBind", False):
205215
if not self.params.get("keep", False):
206216
logging.debug(f"ConnectionPool unbind connection {self}")
@@ -227,7 +237,7 @@ def __exit__(self, type, value, traceback):
227237
self._pool.delete(self)
228238

229239
def __cmp__(self, other):
230-
if isinstance(other, LDAPUrl):
240+
if isinstance(other, Connection):
231241
return self.uri.initializeUrl() == other.uri.initializeUrl()
232242
return False
233243

@@ -293,27 +303,33 @@ def set_uri(self, uri: LDAPUrl):
293303
if not isinstance(uri, LDAPUrl):
294304
uri = LDAPUrl(uri)
295305
if len(self._pool) > 0:
296-
map(
297-
lambda c: (c.set_uri(uri), c.giveback(force=True)),
298-
filter(lambda cp: cp.uri != uri, self._pool),
306+
list(
307+
map(
308+
lambda c: (c.set_uri(uri), c.giveback(force=True)),
309+
filter(lambda cp: cp.uri != uri, self._pool),
310+
)
299311
)
300312
self.uri = uri
301313
return True
302314

303315
def set_binddn(self, binddn: str):
304316
if len(self._pool) > 0:
305-
map(
306-
lambda c: (c.set_binddn(binddn), c.giveback(force=True)),
307-
filter(lambda cp: cp.binddn != binddn, self._pool),
317+
list(
318+
map(
319+
lambda c: (c.set_binddn(binddn), c.giveback(force=True)),
320+
filter(lambda cp: cp.binddn != binddn, self._pool),
321+
)
308322
)
309323
self.binddn = binddn
310324
return True
311325

312326
def set_bindpw(self, bindpw: str):
313327
if len(self._pool) > 0:
314-
map(
315-
lambda c: (c.set_bindpw(bindpw), c.giveback(force=True)),
316-
filter(lambda cp: cp.bindpw != bindpw, self._pool),
328+
list(
329+
map(
330+
lambda c: (c.set_bindpw(bindpw), c.giveback(force=True)),
331+
filter(lambda cp: cp.bindpw != bindpw, self._pool),
332+
)
317333
)
318334
self.bindpw = bindpw
319335
return True
@@ -365,7 +381,7 @@ def ping(self):
365381
def get(self, binddn: str = "", bindpw: str = ""):
366382
if len(self._pool) == 0:
367383
self.scale
368-
self._lock.acquire()
384+
self._lock.acquire(timeout=1)
369385
if len(self._pool) == 0:
370386
self._lock.release()
371387
logging.warning(
@@ -395,7 +411,7 @@ def get(self, binddn: str = "", bindpw: str = ""):
395411
return con
396412

397413
def put(self, connection):
398-
self._lock.acquire()
414+
self._lock.acquire(timeout=1)
399415
if connection.inUse:
400416
connection.giveback()
401417
if not connection in self._pool:
@@ -405,7 +421,7 @@ def put(self, connection):
405421
return True
406422

407423
def status(self):
408-
self._lock.acquire()
424+
self._lock.acquire(timeout=1)
409425
for p in self._pool:
410426
if p.inUse:
411427
if sys.getrefcount(p) < 4:
@@ -414,9 +430,12 @@ def status(self):
414430
self._lock.release()
415431

416432
def delete(self, connection, force=True):
417-
return
418-
self._lock.acquire()
433+
self._lock.acquire(timeout=1)
419434
if connection in self._pool:
420435
if any([not self.params.get("keep", False), force]):
421436
self._pool.remove(connection)
437+
del connection
422438
self._lock.release()
439+
440+
def __len__(self):
441+
return len(self._pool)

Tests/__init__.py

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,7 @@
44
See https://www.python-ldap.org/ for details.
55
"""
66

7-
8-
from . import t_bind
9-
from . import t_cext
10-
from . import t_cidict
11-
from . import t_ldap_dn
12-
from . import t_ldap_filter
13-
from . import t_ldap_functions
14-
from . import t_ldap_modlist
15-
from . import t_ldap_schema_tokenizer
16-
from . import t_ldapurl
17-
from . import t_ldif
18-
from . import t_ldapobject
19-
from . import t_edit
20-
from . import t_ldap_schema_subentry
21-
from . import t_untested_mods
22-
from . import t_ldap_controls_libldap
23-
from . import t_ldap_options
7+
from . import (t_bind, t_cext, t_cidict, t_edit, t_ldap_controls_libldap, t_ldap_dn,
8+
t_ldap_filter, t_ldap_functions, t_ldap_modlist, t_ldap_options,
9+
t_ldap_schema_subentry, t_ldap_schema_tokenizer, t_ldapobject,
10+
t_ldappool, t_ldapurl, t_ldif, t_untested_mods)

0 commit comments

Comments
 (0)