Skip to content

Commit b32dc30

Browse files
committed
create abstract retry class to share methods between retry implementations
1 parent 56a251c commit b32dc30

File tree

2 files changed

+25
-58
lines changed

2 files changed

+25
-58
lines changed

redis/asyncio/retry.py

Lines changed: 8 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,21 @@
11
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
33

44
from redis.exceptions import ConnectionError, RedisError, TimeoutError
55

6-
if TYPE_CHECKING:
7-
from redis.backoff import AbstractBackoff
8-
6+
from redis.retry import AbstractRetry
97

108
T = TypeVar("T")
119

1210

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
11+
class Retry(AbstractRetry):
12+
_supported_errors: Tuple[Type[RedisError], ...] = (
13+
ConnectionError,
14+
TimeoutError,
15+
)
5716

5817
async def call_with_retry(
59-
self, do: Callable[[], Awaitable[T]], fail: Callable[[RedisError], Any]
18+
self, do: Callable[[], Awaitable[T]], fail: Callable[[Exception], Any]
6019
) -> T:
6120
"""
6221
Execute an operation that might fail and returns its result, or

redis/retry.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import socket
22
from time import sleep
3-
from typing import TYPE_CHECKING, Any, Callable, Iterable, Tuple, Type, TypeVar
3+
from typing import TYPE_CHECKING, Any, Callable, Iterable, Tuple, Type, TypeVar, Union
44

55
from redis.exceptions import ConnectionError, TimeoutError
66

@@ -10,18 +10,17 @@
1010
from redis.backoff import AbstractBackoff
1111

1212

13-
class Retry:
13+
class AbstractRetry:
1414
"""Retry a specific number of times after a failure"""
1515

16+
__slots__ = "_backoff", "_retries", "_supported_errors"
17+
_supported_errors: Tuple[Type[Exception], ...]
18+
1619
def __init__(
1720
self,
1821
backoff: "AbstractBackoff",
1922
retries: int,
20-
supported_errors: Tuple[Type[Exception], ...] = (
21-
ConnectionError,
22-
TimeoutError,
23-
socket.timeout,
24-
),
23+
supported_errors: Union[Tuple[Type[Exception], ...], None]
2524
):
2625
"""
2726
Initialize a `Retry` object with a `Backoff` object
@@ -32,10 +31,11 @@ def __init__(
3231
"""
3332
self._backoff = backoff
3433
self._retries = retries
35-
self._supported_errors = supported_errors
34+
if supported_errors:
35+
self._supported_errors = supported_errors
3636

3737
def __eq__(self, other: Any) -> bool:
38-
if not isinstance(other, Retry):
38+
if not isinstance(other, AbstractRetry):
3939
return NotImplemented
4040

4141
return (
@@ -69,6 +69,14 @@ def update_retries(self, value: int) -> None:
6969
"""
7070
self._retries = value
7171

72+
73+
class Retry(AbstractRetry):
74+
_supported_errors: Tuple[Type[Exception], ...] = (
75+
ConnectionError,
76+
TimeoutError,
77+
socket.timeout,
78+
)
79+
7280
def call_with_retry(
7381
self,
7482
do: Callable[[], T],

0 commit comments

Comments
 (0)