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