Skip to content

Commit 859da31

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

File tree

2 files changed

+8
-47
lines changed

2 files changed

+8
-47
lines changed

redis/asyncio/retry.py

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import TYPE_CHECKING, Any, Awaitable, Callable, Tuple, Type, TypeVar
33

44
from redis.exceptions import ConnectionError, RedisError, TimeoutError
5+
from redis.retry import AbstractRetry
56

67
if TYPE_CHECKING:
78
from redis.backoff import AbstractBackoff
@@ -10,51 +11,7 @@
1011
T = TypeVar("T")
1112

1213

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
57-
14+
class Retry(AbstractRetry):
5815
async def call_with_retry(
5916
self, do: Callable[[], Awaitable[T]], fail: Callable[[RedisError], Any]
6017
) -> T:

redis/retry.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
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+
1618
def __init__(
1719
self,
1820
backoff: "AbstractBackoff",
@@ -35,7 +37,7 @@ def __init__(
3537
self._supported_errors = supported_errors
3638

3739
def __eq__(self, other: Any) -> bool:
38-
if not isinstance(other, Retry):
40+
if not isinstance(other, AbstractRetry):
3941
return NotImplemented
4042

4143
return (
@@ -69,6 +71,8 @@ def update_retries(self, value: int) -> None:
6971
"""
7072
self._retries = value
7173

74+
75+
class Retry(AbstractRetry):
7276
def call_with_retry(
7377
self,
7478
do: Callable[[], T],

0 commit comments

Comments
 (0)