Skip to content

Commit 91c594b

Browse files
author
jimcockburn
committed
expoential with backoff
1 parent 601c1aa commit 91c594b

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

redis/backoff.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,20 @@ def compute(self, failures: int) -> float:
109109
self._previous_backoff = min(self._cap, temp)
110110
return self._previous_backoff
111111

112+
class ExponentialWithJitterBackoff(AbstractBackoff):
113+
"""Exponential backoff upon failure, with jitter"""
114+
115+
def __init__(self, cap: float = DEFAULT_CAP, base: float = DEFAULT_BASE) -> None:
116+
"""
117+
`cap`: maximum backoff time in seconds
118+
`base`: base backoff time in seconds
119+
"""
120+
self._cap = cap
121+
self._base = base
122+
123+
def compute(self, failures: int) -> float:
124+
return min(self._cap, random.random() * self._base * 2**failures)
125+
112126

113127
def default_backoff():
114128
return EqualJitterBackoff()

0 commit comments

Comments
 (0)