Exponential backoff upon failure, with jitter
| 156 | |
| 157 | |
| 158 | class ExponentialWithJitterBackoff(AbstractBackoff): |
| 159 | """Exponential backoff upon failure, with jitter""" |
| 160 | |
| 161 | def __init__(self, cap: float = DEFAULT_CAP, base: float = DEFAULT_BASE) -> None: |
| 162 | """ |
| 163 | `cap`: maximum backoff time in seconds |
| 164 | `base`: base backoff time in seconds |
| 165 | """ |
| 166 | self._cap = cap |
| 167 | self._base = base |
| 168 | |
| 169 | def __hash__(self) -> int: |
| 170 | return hash((self._base, self._cap)) |
| 171 | |
| 172 | def __eq__(self, other) -> bool: |
| 173 | if not isinstance(other, ExponentialWithJitterBackoff): |
| 174 | return NotImplemented |
| 175 | |
| 176 | return self._base == other._base and self._cap == other._cap |
| 177 | |
| 178 | def compute(self, failures: int) -> float: |
| 179 | return min(self._cap, random.random() * self._base * 2**failures) |
| 180 | |
| 181 | |
| 182 | def default_backoff(): |
no outgoing calls