Use standard-library logging to record errors

This is a port of a pull request from the old ldappool repo [1]

ldappool frequently catches and ignores transient errors.
There's nothing generally wrong with this -- but if the user
chooses to give the ldappool module a DEBUG priority level,
they really ought to be able to see these. This change makes it
so (and logs final connection errors with the higher ERROR level).

[1] https://github.com/mozilla-services/ldappool/pull/9

Change-Id: I223584393e6010845a5e7eb6aa619a77f6aba84f
This commit is contained in:
Charles Duffy 2016-05-12 19:27:14 -07:00 committed by Steve Martinelli
parent feed0a08c8
commit 91f5cbc36f
1 changed files with 19 additions and 3 deletions

View File

@ -36,6 +36,7 @@
""" LDAP Connection Pool.
"""
from contextlib import contextmanager
import logging
from threading import RLock
import time
@ -43,6 +44,9 @@ import ldap
from ldap.ldapobject import ReconnectLDAPObject
log = logging.getLogger(__name__)
class MaxConnectionReachedError(Exception):
pass
@ -158,7 +162,8 @@ class ConnectionManager(object):
try:
conn.unbind_s()
except Exception:
pass # XXX we will see later
log.debug('Failure attempting to unbind after '
'timeout; should be harmless', exc_info=True)
self._pool.remove(conn)
continue
@ -177,6 +182,8 @@ class ConnectionManager(object):
self._bind(conn, bind, passwd)
return conn
except Exception:
log.debug('Removing connection from pool after '
'failure to rebind', exc_info=True)
self._pool.remove(conn)
return None
@ -218,6 +225,13 @@ class ConnectionManager(object):
connected = True
except ldap.LDAPError as exc:
time.sleep(self.retry_delay)
if tries < self.retry_max:
log.info('Failure attempting to create and bind '
'connector; will retry after %r seconds',
self.retry_delay, exc_info=True)
else:
log.error('Failure attempting to create and bind '
'connector', exc_info=True)
tries += 1
if not connected:
@ -278,7 +292,8 @@ class ConnectionManager(object):
connection.unbind_ext_s()
except ldap.LDAPError:
# avoid error on invalid state
pass
log.debug('Failure attempting to unbind on release; '
'should be harmless', exc_info=True)
@contextmanager
def connection(self, bind=None, passwd=None):
@ -340,5 +355,6 @@ class ConnectionManager(object):
conn.unbind_ext_s()
except ldap.LDAPError:
# invalid state
pass
log.debug('Failure attempting to unbind on purge; '
'should be harmless', exc_info=True)
self._pool.remove(conn)