Exponential backoff upon failure
| 52 | |
| 53 | |
| 54 | class ExponentialBackoff(AbstractBackoff): |
| 55 | """Exponential backoff upon failure""" |
| 56 | |
| 57 | def __init__(self, cap: float = DEFAULT_CAP, base: float = DEFAULT_BASE): |
| 58 | """ |
| 59 | `cap`: maximum backoff time in seconds |
| 60 | `base`: base backoff time in seconds |
| 61 | """ |
| 62 | self._cap = cap |
| 63 | self._base = base |
| 64 | |
| 65 | def __hash__(self) -> int: |
| 66 | return hash((self._base, self._cap)) |
| 67 | |
| 68 | def __eq__(self, other) -> bool: |
| 69 | if not isinstance(other, ExponentialBackoff): |
| 70 | return NotImplemented |
| 71 | |
| 72 | return self._base == other._base and self._cap == other._cap |
| 73 | |
| 74 | def compute(self, failures: int) -> float: |
| 75 | return min(self._cap, self._base * 2**failures) |
| 76 | |
| 77 | |
| 78 | class FullJitterBackoff(AbstractBackoff): |
no outgoing calls