Merge "Allow max_backoff and use count instead of attempts"

This commit is contained in:
Jenkins
2014-01-03 14:15:46 +00:00
committed by Gerrit Code Review
2 changed files with 7 additions and 6 deletions

View File

@@ -188,7 +188,7 @@ class SQLAlchemyBackend(base.Backend):
# after a given number of backoffs (with a backoff sleeping period # after a given number of backoffs (with a backoff sleeping period
# between each attempt)... # between each attempt)...
attempts_left = max_retries attempts_left = max_retries
for sleepy_secs in misc.ExponentialBackoff(attempts=max_retries): for sleepy_secs in misc.ExponentialBackoff(max_retries):
LOG.warn("SQL connection failed due to '%s', %s attempts left.", LOG.warn("SQL connection failed due to '%s', %s attempts left.",
failures[-1].exc, attempts_left) failures[-1].exc, attempts_left)
LOG.info("Attempting to test the connection again in %s seconds.", LOG.info("Attempting to test the connection again in %s seconds.",

View File

@@ -148,15 +148,16 @@ class ExponentialBackoff(object):
provided an exponent and a number of items to yield. This object may be provided an exponent and a number of items to yield. This object may be
iterated over multiple times (yielding the same sequence each time). iterated over multiple times (yielding the same sequence each time).
""" """
def __init__(self, attempts, exponent=2): def __init__(self, count, exponent=2, max_backoff=3600):
self.attempts = int(attempts) self.count = max(0, int(count))
self.exponent = exponent self.exponent = exponent
self.max_backoff = max(0, int(max_backoff))
def __iter__(self): def __iter__(self):
if self.attempts <= 0: if self.count <= 0:
raise StopIteration() raise StopIteration()
for i in six.moves.range(0, self.attempts): for i in six.moves.range(0, self.count):
yield self.exponent ** i yield min(self.exponent ** i, self.max_backoff)
def __str__(self): def __str__(self):
return "ExponentialBackoff: %s" % ([str(v) for v in self]) return "ExponentialBackoff: %s" % ([str(v) for v in self])