A retry limiter that performs exponential backoff with jitter.
| 88 | |
| 89 | |
| 90 | class _RetryPolicy: |
| 91 | """A retry limiter that performs exponential backoff with jitter.""" |
| 92 | |
| 93 | def __init__( |
| 94 | self, |
| 95 | attempts: int = MAX_ADAPTIVE_RETRIES, |
| 96 | backoff_initial: float = _BACKOFF_INITIAL, |
| 97 | backoff_max: float = _BACKOFF_MAX, |
| 98 | ): |
| 99 | self.attempts = attempts |
| 100 | self.backoff_initial = backoff_initial |
| 101 | self.backoff_max = backoff_max |
| 102 | |
| 103 | def backoff(self, attempt: int) -> float: |
| 104 | """Return the backoff duration for the given attempt.""" |
| 105 | return _backoff(max(0, attempt - 1), self.backoff_initial, self.backoff_max) |
| 106 | |
| 107 | def should_retry(self, attempt: int, delay: float) -> bool: |
| 108 | """Return if we have retry attempts remaining and the next backoff would not exceed a timeout.""" |
| 109 | if attempt > self.attempts: |
| 110 | return False |
| 111 | |
| 112 | if _csot.get_timeout(): |
| 113 | if time.monotonic() + delay > _csot.get_deadline(): |
| 114 | return False |
| 115 | |
| 116 | return True |
| 117 | |
| 118 | |
| 119 | def _getaddrinfo( |