Skip to content

Commit 056963b

Browse files
orientortimabbott
authored andcommitted
api: Add delay_cap as class variable of CountingBackoff class.
1 parent 62555f9 commit 056963b

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

Diff for: zulip/zulip/__init__.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,13 @@
5656
API_VERSTRING = "v1/"
5757

5858
class CountingBackoff(object):
59-
def __init__(self, maximum_retries=10, timeout_success_equivalent=None):
60-
# type: (int, Optional[float]) -> None
59+
def __init__(self, maximum_retries=10, timeout_success_equivalent=None, delay_cap=90.0):
60+
# type: (int, Optional[float], float) -> None
6161
self.number_of_retries = 0
6262
self.maximum_retries = maximum_retries
6363
self.timeout_success_equivalent = timeout_success_equivalent
6464
self.last_attempt_time = 0.0
65+
self.delay_cap = delay_cap
6566

6667
def keep_going(self):
6768
# type: () -> bool
@@ -94,7 +95,7 @@ def fail(self):
9495
# Exponential growth with ratio sqrt(2); compute random delay
9596
# between x and 2x where x is growing exponentially
9697
delay_scale = int(2 ** (self.number_of_retries / 2.0 - 1)) + 1
97-
delay = delay_scale + random.randint(1, delay_scale)
98+
delay = min(delay_scale + random.randint(1, delay_scale), delay_cap)
9899
message = "Sleeping for %ss [max %s] before retrying." % (delay, delay_scale * 2)
99100
try:
100101
logger.warning(message)

0 commit comments

Comments
 (0)