@@ -82,10 +82,8 @@ def __whoami(self):
82
82
try :
83
83
self .__enter__ ()
84
84
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" )
89
87
90
88
@property
91
89
def conn (self ):
@@ -188,6 +186,7 @@ def __enter__(self):
188
186
)
189
187
try :
190
188
self ._conn = ldap .initialize (self .uri .initializeUrl ())
189
+ self .__set_connection_parameters__ ()
191
190
if self .params .get ("autoBind" , False ):
192
191
(
193
192
f"ConnectionPool { self } autoBind with { self .binddn } password { 'x' * len (self .bindpw )} "
@@ -199,8 +198,19 @@ def __enter__(self):
199
198
self .established = True
200
199
return self .conn
201
200
202
- def giveback (self ):
201
+ def giveback (self , force = False ):
203
202
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
+
204
214
if self .params .get ("autoBind" , False ):
205
215
if not self .params .get ("keep" , False ):
206
216
logging .debug (f"ConnectionPool unbind connection { self } " )
@@ -227,7 +237,7 @@ def __exit__(self, type, value, traceback):
227
237
self ._pool .delete (self )
228
238
229
239
def __cmp__ (self , other ):
230
- if isinstance (other , LDAPUrl ):
240
+ if isinstance (other , Connection ):
231
241
return self .uri .initializeUrl () == other .uri .initializeUrl ()
232
242
return False
233
243
@@ -293,27 +303,33 @@ def set_uri(self, uri: LDAPUrl):
293
303
if not isinstance (uri , LDAPUrl ):
294
304
uri = LDAPUrl (uri )
295
305
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
+ )
299
311
)
300
312
self .uri = uri
301
313
return True
302
314
303
315
def set_binddn (self , binddn : str ):
304
316
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
+ )
308
322
)
309
323
self .binddn = binddn
310
324
return True
311
325
312
326
def set_bindpw (self , bindpw : str ):
313
327
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
+ )
317
333
)
318
334
self .bindpw = bindpw
319
335
return True
@@ -365,7 +381,7 @@ def ping(self):
365
381
def get (self , binddn : str = "" , bindpw : str = "" ):
366
382
if len (self ._pool ) == 0 :
367
383
self .scale
368
- self ._lock .acquire ()
384
+ self ._lock .acquire (timeout = 1 )
369
385
if len (self ._pool ) == 0 :
370
386
self ._lock .release ()
371
387
logging .warning (
@@ -395,7 +411,7 @@ def get(self, binddn: str = "", bindpw: str = ""):
395
411
return con
396
412
397
413
def put (self , connection ):
398
- self ._lock .acquire ()
414
+ self ._lock .acquire (timeout = 1 )
399
415
if connection .inUse :
400
416
connection .giveback ()
401
417
if not connection in self ._pool :
@@ -405,7 +421,7 @@ def put(self, connection):
405
421
return True
406
422
407
423
def status (self ):
408
- self ._lock .acquire ()
424
+ self ._lock .acquire (timeout = 1 )
409
425
for p in self ._pool :
410
426
if p .inUse :
411
427
if sys .getrefcount (p ) < 4 :
@@ -414,9 +430,12 @@ def status(self):
414
430
self ._lock .release ()
415
431
416
432
def delete (self , connection , force = True ):
417
- return
418
- self ._lock .acquire ()
433
+ self ._lock .acquire (timeout = 1 )
419
434
if connection in self ._pool :
420
435
if any ([not self .params .get ("keep" , False ), force ]):
421
436
self ._pool .remove (connection )
437
+ del connection
422
438
self ._lock .release ()
439
+
440
+ def __len__ (self ):
441
+ return len (self ._pool )
0 commit comments