|
1 | 1 | from asyncio import sleep |
2 | | -from typing import TYPE_CHECKING, Any, Awaitable, Callable, Tuple, Type, TypeVar |
| 2 | +from typing import Any, Awaitable, Callable, Tuple, Type, TypeVar |
3 | 3 |
|
4 | 4 | from redis.exceptions import ConnectionError, RedisError, TimeoutError |
5 | | - |
6 | | -if TYPE_CHECKING: |
7 | | - from redis.backoff import AbstractBackoff |
8 | | - |
| 5 | +from redis.retry import AbstractRetry |
9 | 6 |
|
10 | 7 | T = TypeVar("T") |
11 | 8 |
|
12 | 9 |
|
13 | | -class Retry: |
14 | | - """Retry a specific number of times after a failure""" |
15 | | - |
16 | | - __slots__ = "_backoff", "_retries", "_supported_errors" |
17 | | - |
18 | | - def __init__( |
19 | | - self, |
20 | | - backoff: "AbstractBackoff", |
21 | | - retries: int, |
22 | | - supported_errors: Tuple[Type[RedisError], ...] = ( |
23 | | - ConnectionError, |
24 | | - TimeoutError, |
25 | | - ), |
26 | | - ): |
27 | | - """ |
28 | | - Initialize a `Retry` object with a `Backoff` object |
29 | | - that retries a maximum of `retries` times. |
30 | | - `retries` can be negative to retry forever. |
31 | | - You can specify the types of supported errors which trigger |
32 | | - a retry with the `supported_errors` parameter. |
33 | | - """ |
34 | | - self._backoff = backoff |
35 | | - self._retries = retries |
36 | | - self._supported_errors = supported_errors |
37 | | - |
38 | | - def update_supported_errors(self, specified_errors: list): |
39 | | - """ |
40 | | - Updates the supported errors with the specified error types |
41 | | - """ |
42 | | - self._supported_errors = tuple( |
43 | | - set(self._supported_errors + tuple(specified_errors)) |
44 | | - ) |
45 | | - |
46 | | - def get_retries(self) -> int: |
47 | | - """ |
48 | | - Get the number of retries. |
49 | | - """ |
50 | | - return self._retries |
51 | | - |
52 | | - def update_retries(self, value: int) -> None: |
53 | | - """ |
54 | | - Set the number of retries. |
55 | | - """ |
56 | | - self._retries = value |
| 10 | +class Retry(AbstractRetry): |
| 11 | + _supported_errors: Tuple[Type[RedisError], ...] = ( |
| 12 | + ConnectionError, |
| 13 | + TimeoutError, |
| 14 | + ) |
57 | 15 |
|
58 | 16 | async def call_with_retry( |
59 | | - self, do: Callable[[], Awaitable[T]], fail: Callable[[RedisError], Any] |
| 17 | + self, do: Callable[[], Awaitable[T]], fail: Callable[[Exception], Any] |
60 | 18 | ) -> T: |
61 | 19 | """ |
62 | 20 | Execute an operation that might fail and returns its result, or |
|
0 commit comments