From 91f5cbc36f7bd9d3c0516249bcb871b8841a4f96 Mon Sep 17 00:00:00 2001 From: Charles Duffy Date: Thu, 12 May 2016 19:27:14 -0700 Subject: [PATCH] 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 --- ldappool/__init__.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/ldappool/__init__.py b/ldappool/__init__.py index 9cc8a9b..997b5ae 100644 --- a/ldappool/__init__.py +++ b/ldappool/__init__.py @@ -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)