From 056963bedd15bd3ca51ff45a088d4cf55edfd1cb Mon Sep 17 00:00:00 2001 From: orientor Date: Thu, 5 Mar 2020 04:17:35 +0530 Subject: [PATCH] api: Add delay_cap as class variable of CountingBackoff class. --- zulip/zulip/__init__.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/zulip/zulip/__init__.py b/zulip/zulip/__init__.py index 474ab95..a63fc50 100644 --- a/zulip/zulip/__init__.py +++ b/zulip/zulip/__init__.py @@ -56,12 +56,13 @@ requests_json_is_function = callable(requests.Response.json) API_VERSTRING = "v1/" class CountingBackoff(object): - def __init__(self, maximum_retries=10, timeout_success_equivalent=None): - # type: (int, Optional[float]) -> None + def __init__(self, maximum_retries=10, timeout_success_equivalent=None, delay_cap=90.0): + # type: (int, Optional[float], float) -> None self.number_of_retries = 0 self.maximum_retries = maximum_retries self.timeout_success_equivalent = timeout_success_equivalent self.last_attempt_time = 0.0 + self.delay_cap = delay_cap def keep_going(self): # type: () -> bool @@ -94,7 +95,7 @@ class RandomExponentialBackoff(CountingBackoff): # Exponential growth with ratio sqrt(2); compute random delay # between x and 2x where x is growing exponentially delay_scale = int(2 ** (self.number_of_retries / 2.0 - 1)) + 1 - delay = delay_scale + random.randint(1, delay_scale) + delay = min(delay_scale + random.randint(1, delay_scale), delay_cap) message = "Sleeping for %ss [max %s] before retrying." % (delay, delay_scale * 2) try: logger.warning(message)