diff --git a/taskflow/persistence/backends/impl_sqlalchemy.py b/taskflow/persistence/backends/impl_sqlalchemy.py index f1d94f12..f9d9519d 100644 --- a/taskflow/persistence/backends/impl_sqlalchemy.py +++ b/taskflow/persistence/backends/impl_sqlalchemy.py @@ -188,7 +188,7 @@ class SQLAlchemyBackend(base.Backend): # after a given number of backoffs (with a backoff sleeping period # between each attempt)... 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.", failures[-1].exc, attempts_left) LOG.info("Attempting to test the connection again in %s seconds.", diff --git a/taskflow/utils/misc.py b/taskflow/utils/misc.py index 3ec136c7..a74acc92 100644 --- a/taskflow/utils/misc.py +++ b/taskflow/utils/misc.py @@ -148,15 +148,16 @@ class ExponentialBackoff(object): provided an exponent and a number of items to yield. This object may be iterated over multiple times (yielding the same sequence each time). """ - def __init__(self, attempts, exponent=2): - self.attempts = int(attempts) + def __init__(self, count, exponent=2, max_backoff=3600): + self.count = max(0, int(count)) self.exponent = exponent + self.max_backoff = max(0, int(max_backoff)) def __iter__(self): - if self.attempts <= 0: + if self.count <= 0: raise StopIteration() - for i in six.moves.range(0, self.attempts): - yield self.exponent ** i + for i in six.moves.range(0, self.count): + yield min(self.exponent ** i, self.max_backoff) def __str__(self): return "ExponentialBackoff: %s" % ([str(v) for v in self])