| 1 |
import abc |
| 2 |
import random |
| 3 |
from typing import Any, Awaitable, Callable, Iterable, List, Optional, Set, Type |
| 4 |
from warnings import warn |
| 5 |
|
| 6 |
from aiohttp import ClientResponse |
| 7 |
|
| 8 |
EvaluateResponseCallbackType = Callable[[ClientResponse], Awaitable[bool]] |
| 9 |
|
| 10 |
|
| 11 |
class RetryOptionsBase: |
| 12 |
def __init__( |
| 13 |
self, |
| 14 |
attempts: int = 3, # How many times we should retry |
| 15 |
statuses: Optional[Iterable[int]] = None, # On which statuses we should retry |
| 16 |
exceptions: Optional[Iterable[Type[Exception]]] = None, # On which exceptions we should retry |
| 17 |
retry_all_server_errors: bool = True, # If should retry all 500 errors or not |
| 18 |
# a callback that will run on response to decide if retry |
| 19 |
evaluate_response_callback: Optional[EvaluateResponseCallbackType] = None, |
| 20 |
): |
| 21 |
self.attempts: int = attempts |
| 22 |
if statuses is None: |
| 23 |
statuses = set() |
| 24 |
self.statuses: Iterable[int] = statuses |
| 25 |
|
| 26 |
if exceptions is None: |
| 27 |
exceptions = set() |
| 28 |
self.exceptions: Iterable[Type[Exception]] = exceptions |
| 29 |
|
| 30 |
self.retry_all_server_errors = retry_all_server_errors |
| 31 |
self.evaluate_response_callback = evaluate_response_callback |
| 32 |
|
| 33 |
@abc.abstractmethod |
| 34 |
def get_timeout(self, attempt: int, response: Optional[ClientResponse] = None) -> float: |
| 35 |
raise NotImplementedError |
| 36 |
|
| 37 |
|
| 38 |
class ExponentialRetry(RetryOptionsBase): |
| 39 |
def __init__( |
| 40 |
self, |
| 41 |
attempts: int = 3, # How many times we should retry |
| 42 |
start_timeout: float = 0.1, # Base timeout time, then it exponentially grow |
| 43 |
max_timeout: float = 30.0, # Max possible timeout between tries |
| 44 |
factor: float = 2.0, # How much we increase timeout each time |
| 45 |
statuses: Optional[Set[int]] = None, # On which statuses we should retry |
| 46 |
exceptions: Optional[Set[Type[Exception]]] = None, # On which exceptions we should retry |
| 47 |
retry_all_server_errors: bool = True, |
| 48 |
evaluate_response_callback: Optional[EvaluateResponseCallbackType] = None, |
| 49 |
): |
| 50 |
super().__init__( |
| 51 |
attempts=attempts, |
| 52 |
statuses=statuses, |
| 53 |
exceptions=exceptions, |
| 54 |
retry_all_server_errors=retry_all_server_errors, |
| 55 |
evaluate_response_callback=evaluate_response_callback, |
| 56 |
) |
| 57 |
|
| 58 |
self._start_timeout: float = start_timeout |
| 59 |
self._max_timeout: float = max_timeout |
| 60 |
self._factor: float = factor |
| 61 |
|
| 62 |
def get_timeout(self, attempt: int, response: Optional[ClientResponse] = None) -> float: |
| 63 |
"""Return timeout with exponential backoff.""" |
| 64 |
timeout = self._start_timeout * (self._factor ** attempt) |
| 65 |
return min(timeout, self._max_timeout) |
| 66 |
|
| 67 |
|
| 68 |
def RetryOptions(*args: Any, **kwargs: Any) -> ExponentialRetry: |
| 69 |
warn("RetryOptions is deprecated, use ExponentialRetry") |
| 70 |
return ExponentialRetry(*args, **kwargs) |
| 71 |
|
| 72 |
|
| 73 |
class RandomRetry(RetryOptionsBase): |
| 74 |
def __init__( |
| 75 |
self, |
| 76 |
attempts: int = 3, # How many times we should retry |
| 77 |
statuses: Optional[Iterable[int]] = None, # On which statuses we should retry |
| 78 |
exceptions: Optional[Iterable[Type[Exception]]] = None, # On which exceptions we should retry |
| 79 |
min_timeout: float = 0.1, # Minimum possible timeout |
| 80 |
max_timeout: float = 3.0, # Maximum possible timeout between tries |
| 81 |
random_func: Callable[[], float] = random.random, # Random number generator |
| 82 |
retry_all_server_errors: bool = True, |
| 83 |
evaluate_response_callback: Optional[EvaluateResponseCallbackType] = None, |
| 84 |
): |
| 85 |
super().__init__( |
| 86 |
attempts=attempts, |
| 87 |
statuses=statuses, |
| 88 |
exceptions=exceptions, |
| 89 |
retry_all_server_errors=retry_all_server_errors, |
| 90 |
evaluate_response_callback=evaluate_response_callback, |
| 91 |
) |
| 92 |
|
| 93 |
self.attempts: int = attempts |
| 94 |
self.min_timeout: float = min_timeout |
| 95 |
self.max_timeout: float = max_timeout |
| 96 |
self.random = random_func |
| 97 |
|
| 98 |
def get_timeout(self, attempt: int, response: Optional[ClientResponse] = None) -> float: |
| 99 |
"""Generate random timeouts.""" |
| 100 |
return self.min_timeout + self.random() * (self.max_timeout - self.min_timeout) |
| 101 |
|
| 102 |
|
| 103 |
class ListRetry(RetryOptionsBase): |
| 104 |
def __init__( |
| 105 |
self, |
| 106 |
timeouts: List[float], |
| 107 |
statuses: Optional[Iterable[int]] = None, # On which statuses we should retry |
| 108 |
exceptions: Optional[Iterable[Type[Exception]]] = None, # On which exceptions we should retry |
| 109 |
retry_all_server_errors: bool = True, |
| 110 |
evaluate_response_callback: Optional[EvaluateResponseCallbackType] = None, |
| 111 |
): |
| 112 |
super().__init__( |
| 113 |
attempts=len(timeouts), |
| 114 |
statuses=statuses, |
| 115 |
exceptions=exceptions, |
| 116 |
retry_all_server_errors=retry_all_server_errors, |
| 117 |
evaluate_response_callback=evaluate_response_callback, |
| 118 |
) |
| 119 |
self.timeouts = timeouts |
| 120 |
|
| 121 |
def get_timeout(self, attempt: int, response: Optional[ClientResponse] = None) -> float: |
| 122 |
"""timeouts from a defined list.""" |
| 123 |
return self.timeouts[attempt] |
| 124 |
|
| 125 |
|
| 126 |
class FibonacciRetry(RetryOptionsBase): |
| 127 |
def __init__( |
| 128 |
self, |
| 129 |
attempts: int = 3, |
| 130 |
multiplier: float = 1.0, |
| 131 |
statuses: Optional[Iterable[int]] = None, |
| 132 |
exceptions: Optional[Iterable[Type[Exception]]] = None, |
| 133 |
max_timeout: float = 3.0, # Maximum possible timeout between tries |
| 134 |
retry_all_server_errors: bool = True, |
| 135 |
evaluate_response_callback: Optional[EvaluateResponseCallbackType] = None, |
| 136 |
): |
| 137 |
super().__init__( |
| 138 |
attempts=attempts, |
| 139 |
statuses=statuses, |
| 140 |
exceptions=exceptions, |
| 141 |
retry_all_server_errors=retry_all_server_errors, |
| 142 |
evaluate_response_callback=evaluate_response_callback, |
| 143 |
) |
| 144 |
|
| 145 |
self.max_timeout = max_timeout |
| 146 |
self.multiplier = multiplier |
| 147 |
self.prev_step = 1.0 |
| 148 |
self.current_step = 1.0 |
| 149 |
|
| 150 |
def get_timeout(self, attempt: int, response: Optional[ClientResponse] = None) -> float: |
| 151 |
new_current_step = self.prev_step + self.current_step |
| 152 |
self.prev_step = self.current_step |
| 153 |
self.current_step = new_current_step |
| 154 |
|
| 155 |
return min(self.multiplier * new_current_step, self.max_timeout) |
| 156 |
|
| 157 |
|
| 158 |
class JitterRetry(ExponentialRetry): |
| 159 |
"""https://github.com/inyutin/aiohttp_retry/issues/44""" |
| 160 |
|
| 161 |
def __init__( |
| 162 |
self, |
| 163 |
attempts: int = 3, # How many times we should retry |
| 164 |
start_timeout: float = 0.1, # Base timeout time, then it exponentially grow |
| 165 |
max_timeout: float = 30.0, # Max possible timeout between tries |
| 166 |
factor: float = 2.0, # How much we increase timeout each time |
| 167 |
statuses: Optional[Set[int]] = None, # On which statuses we should retry |
| 168 |
exceptions: Optional[Set[Type[Exception]]] = None, # On which exceptions we should retry |
| 169 |
random_interval_size: float = 2.0, # size of interval for random component |
| 170 |
retry_all_server_errors: bool = True, |
| 171 |
evaluate_response_callback: Optional[EvaluateResponseCallbackType] = None, |
| 172 |
): |
| 173 |
super().__init__( |
| 174 |
attempts=attempts, |
| 175 |
start_timeout=start_timeout, |
| 176 |
max_timeout=max_timeout, |
| 177 |
factor=factor, |
| 178 |
statuses=statuses, |
| 179 |
exceptions=exceptions, |
| 180 |
retry_all_server_errors=retry_all_server_errors, |
| 181 |
evaluate_response_callback=evaluate_response_callback, |
| 182 |
) |
| 183 |
|
| 184 |
self._start_timeout: float = start_timeout |
| 185 |
self._max_timeout: float = max_timeout |
| 186 |
self._factor: float = factor |
| 187 |
self._random_interval_size = random_interval_size |
| 188 |
|
| 189 |
def get_timeout(self, attempt: int, response: Optional[ClientResponse] = None) -> float: |
| 190 |
timeout: float = super().get_timeout(attempt) + random.uniform(0, self._random_interval_size) ** self._factor |
| 191 |
return timeout |