| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import enum |
| 4 |
import typing as t |
| 5 |
|
| 6 |
import requests |
| 7 |
|
| 8 |
if t.TYPE_CHECKING: |
| 9 |
from .caller_info import RequestCallerInfo |
| 10 |
|
| 11 |
C = t.TypeVar("C", bound=t.Callable[..., t.Any]) |
| 12 |
|
| 13 |
|
| 14 |
# alias useful for declaring retry-related types |
| 15 |
RetryCheck = t.Callable[["RetryContext"], "RetryCheckResult"] |
| 16 |
|
| 17 |
|
| 18 |
class RetryContext: |
| 19 |
""" |
| 20 |
The RetryContext is an object passed to retry checks in order to determine whether |
| 21 |
or not a request should be retried. The context is constructed after each request, |
| 22 |
regardless of success or failure. |
| 23 |
|
| 24 |
If an exception was raised, the context will contain that exception object. |
| 25 |
Otherwise, the context will contain a response object. Exactly one of ``response`` |
| 26 |
or ``exception`` will be present. |
| 27 |
|
| 28 |
:param attempt: The request attempt number, starting at 0. |
| 29 |
:param caller_info: Contextual information about the caller, including authorizer |
| 30 |
:param response: The response on a successful request |
| 31 |
:param exception: The error raised when trying to send the request |
| 32 |
""" |
| 33 |
|
| 34 |
def __init__( |
| 35 |
self, |
| 36 |
attempt: int, |
| 37 |
*, |
| 38 |
caller_info: RequestCallerInfo, |
| 39 |
response: requests.Response | None = None, |
| 40 |
exception: Exception | None = None, |
| 41 |
) -> None: |
| 42 |
# retry attempt number |
| 43 |
self.attempt = attempt |
| 44 |
# caller info provides contextual information about the request |
| 45 |
self.caller_info = caller_info |
| 46 |
# the response or exception from a request |
| 47 |
# we expect exactly one of these to be non-null |
| 48 |
self.response = response |
| 49 |
self.exception = exception |
| 50 |
# the retry delay or "backoff" before retrying |
| 51 |
self.backoff: float | None = None |
| 52 |
|
| 53 |
|
| 54 |
class RetryCheckResult(enum.Enum): |
| 55 |
#: yes, retry the request |
| 56 |
do_retry = enum.auto() |
| 57 |
#: no, do not retry the request |
| 58 |
do_not_retry = enum.auto() |
| 59 |
#: "I don't know", ask other checks for an answer |
| 60 |
no_decision = enum.auto() |
| 61 |
|
| 62 |
|
| 63 |
class RetryCheckFlags(enum.Flag): |
| 64 |
#: no flags (default) |
| 65 |
NONE = enum.auto() |
| 66 |
#: only run this check once per request |
| 67 |
RUN_ONCE = enum.auto() |
| 68 |
|
| 69 |
|
| 70 |
# stub for mypy |
| 71 |
class _RetryCheckFunc: |
| 72 |
_retry_check_flags: RetryCheckFlags |
| 73 |
|
| 74 |
|
| 75 |
def set_retry_check_flags(flag: RetryCheckFlags) -> t.Callable[[C], C]: |
| 76 |
""" |
| 77 |
A decorator for setting retry check flags on a retry check function. |
| 78 |
Usage: |
| 79 |
|
| 80 |
>>> @set_retry_check_flags(RetryCheckFlags.RUN_ONCE) |
| 81 |
>>> def foo(ctx): ... |
| 82 |
|
| 83 |
:param flag: The flag to set on the check |
| 84 |
""" |
| 85 |
|
| 86 |
def decorator(func: C) -> C: |
| 87 |
as_check = t.cast(_RetryCheckFunc, func) |
| 88 |
as_check._retry_check_flags = flag |
| 89 |
return func |
| 90 |
|
| 91 |
return decorator |
| 92 |
|
| 93 |
|
| 94 |
class RetryCheckCollection: |
| 95 |
""" |
| 96 |
A RetryCheckCollection is an ordered collection of retry checks which are |
| 97 |
used to determine whether or not a request should be retried. |
| 98 |
|
| 99 |
Checks are stored in registration order. |
| 100 |
|
| 101 |
Notably, the collection does not decide |
| 102 |
- how many times a request should retry |
| 103 |
- how or how long the call should wait between attempts |
| 104 |
(except via the backoff which may be set) |
| 105 |
- what kinds of request parameters (e.g., timeouts) are used |
| 106 |
|
| 107 |
It *only* contains ``RetryCheck`` functions which can look at a response or |
| 108 |
error and decide whether or not to retry. |
| 109 |
""" |
| 110 |
|
| 111 |
def __init__(self) -> None: |
| 112 |
self._data: list[RetryCheck] = [] |
| 113 |
|
| 114 |
def register_check(self, func: RetryCheck) -> RetryCheck: |
| 115 |
""" |
| 116 |
Register a retry check with this policy. |
| 117 |
|
| 118 |
A retry checker is a callable responsible for implementing |
| 119 |
`check(RetryContext) -> RetryCheckResult` |
| 120 |
|
| 121 |
`check` should *not* perform any sleeps or delays. |
| 122 |
Multiple checks should be chainable and callable in any order. |
| 123 |
|
| 124 |
:param func: The function or other callable to register as a retry check |
| 125 |
""" |
| 126 |
self._data.append(func) |
| 127 |
return func |
| 128 |
|
| 129 |
def register_many_checks(self, funcs: t.Iterable[RetryCheck]) -> None: |
| 130 |
""" |
| 131 |
Register all checks in a collection of checks. |
| 132 |
|
| 133 |
:param funcs: An iterable collection of retry check callables |
| 134 |
""" |
| 135 |
for f in funcs: |
| 136 |
self.register_check(f) |
| 137 |
|
| 138 |
def __iter__(self) -> t.Iterator[RetryCheck]: |
| 139 |
yield from self._data |
| 140 |
|
| 141 |
def __len__(self) -> int: |
| 142 |
return len(self._data) |