Implements exponential backoff with random jitter to avoid thundering herd problems in retry scenarios.
class ExponentialWithJitterBackoff(AbstractBackoff):
"""Exponential backoff upon failure, with jitter"""
def __init__(self, cap: float = DEFAULT_CAP, base: float = DEFAULT_BASE) -> None:
"""
`cap`: maximum backoff time in seconds
`base`: base backoff time in seconds
"""
self._cap = cap
self._base = base
def __hash__(self) -> int:
return hash((self._base, self._cap))
def __eq__(self, other) -> bool:
if not isinstance(other, ExponentialWithJitterBackoff):
return NotImplemented
return self._base == other._base and self._cap == other._cap
def compute(self, failures: int) -> float:
return min(self._cap, random.random() * self._base * 2**failures)
See the full source, get the GitHub permalink, and search 40K more like it.
Get a free API key