Skip to content

Commit 7fb08ec

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

File tree

2 files changed

+10
-55
lines changed

2 files changed

+10
-55
lines changed

redis/asyncio/retry.py

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,13 @@
11
from asyncio import sleep
2-
from typing import TYPE_CHECKING, Any, Awaitable, Callable, Tuple, Type, TypeVar
3-
4-
from redis.exceptions import ConnectionError, RedisError, TimeoutError
5-
6-
if TYPE_CHECKING:
7-
from redis.backoff import AbstractBackoff
8-
2+
from redis.retry import AbstractRetry
3+
from typing import Any, Awaitable, Callable, TypeVar
94

105
T = TypeVar("T")
116

127

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-
8+
class Retry(AbstractRetry):
589
async def call_with_retry(
59-
self, do: Callable[[], Awaitable[T]], fail: Callable[[RedisError], Any]
10+
self, do: Callable[[], Awaitable[T]], fail: Callable[[Exception], Any]
6011
) -> T:
6112
"""
6213
Execute an operation that might fail and returns its result, or

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)