Skip to content

Commit dbb7cca

Browse files
terencehonlespetyaslavova
authored andcommitted
add async Retry __eq__ and __hash__ & fix ExponentialWithJitterBackoff __eq__ (#3668)
* fix ExponentialWithJitterBackoff ``__eq__`` This change fixes a typo in `ExponentialWithJitterBackoff`'s `__eq__` method. * create abstract retry class to share methods between retry implementations * update retry classes: remove slots & move `__eq__` to concrete classes * implement __init__ methods in retry subclasses to make the supported errors more obvious
1 parent 02313e1 commit dbb7cca

File tree

4 files changed

+68
-59
lines changed

4 files changed

+68
-59
lines changed

redis/asyncio/retry.py

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

44
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
96

107
T = TypeVar("T")
118

9+
if TYPE_CHECKING:
10+
from redis.backoff import AbstractBackoff
1211

13-
class Retry:
14-
"""Retry a specific number of times after a failure"""
1512

16-
__slots__ = "_backoff", "_retries", "_supported_errors"
13+
class Retry(AbstractRetry[RedisError]):
14+
__hash__ = AbstractRetry.__hash__
1715

1816
def __init__(
1917
self,
@@ -24,36 +22,17 @@ def __init__(
2422
TimeoutError,
2523
),
2624
):
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
25+
super().__init__(backoff, retries, supported_errors)
3726

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
27+
def __eq__(self, other: Any) -> bool:
28+
if not isinstance(other, Retry):
29+
return NotImplemented
5130

52-
def update_retries(self, value: int) -> None:
53-
"""
54-
Set the number of retries.
55-
"""
56-
self._retries = value
31+
return (
32+
self._backoff == other._backoff
33+
and self._retries == other._retries
34+
and set(self._supported_errors) == set(other._supported_errors)
35+
)
5736

5837
async def call_with_retry(
5938
self, do: Callable[[], Awaitable[T]], fail: Callable[[RedisError], Any]

redis/backoff.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def __hash__(self) -> int:
170170
return hash((self._base, self._cap))
171171

172172
def __eq__(self, other) -> bool:
173-
if not isinstance(other, EqualJitterBackoff):
173+
if not isinstance(other, ExponentialWithJitterBackoff):
174174
return NotImplemented
175175

176176
return self._base == other._base and self._cap == other._cap

redis/retry.py

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1+
import abc
12
import socket
23
from time import sleep
3-
from typing import TYPE_CHECKING, Any, Callable, Iterable, Tuple, Type, TypeVar
4+
from typing import TYPE_CHECKING, Any, Callable, Generic, Iterable, Tuple, Type, TypeVar
45

56
from redis.exceptions import ConnectionError, TimeoutError
67

78
T = TypeVar("T")
9+
E = TypeVar("E", bound=Exception, covariant=True)
810

911
if TYPE_CHECKING:
1012
from redis.backoff import AbstractBackoff
1113

1214

13-
class Retry:
15+
class AbstractRetry(Generic[E], abc.ABC):
1416
"""Retry a specific number of times after a failure"""
1517

18+
_supported_errors: Tuple[Type[E], ...]
19+
1620
def __init__(
1721
self,
1822
backoff: "AbstractBackoff",
1923
retries: int,
20-
supported_errors: Tuple[Type[Exception], ...] = (
21-
ConnectionError,
22-
TimeoutError,
23-
socket.timeout,
24-
),
24+
supported_errors: Tuple[Type[E], ...],
2525
):
2626
"""
2727
Initialize a `Retry` object with a `Backoff` object
@@ -34,22 +34,14 @@ def __init__(
3434
self._retries = retries
3535
self._supported_errors = supported_errors
3636

37+
@abc.abstractmethod
3738
def __eq__(self, other: Any) -> bool:
38-
if not isinstance(other, Retry):
39-
return NotImplemented
40-
41-
return (
42-
self._backoff == other._backoff
43-
and self._retries == other._retries
44-
and set(self._supported_errors) == set(other._supported_errors)
45-
)
39+
return NotImplemented
4640

4741
def __hash__(self) -> int:
4842
return hash((self._backoff, self._retries, frozenset(self._supported_errors)))
4943

50-
def update_supported_errors(
51-
self, specified_errors: Iterable[Type[Exception]]
52-
) -> None:
44+
def update_supported_errors(self, specified_errors: Iterable[Type[E]]) -> None:
5345
"""
5446
Updates the supported errors with the specified error types
5547
"""
@@ -69,6 +61,32 @@ def update_retries(self, value: int) -> None:
6961
"""
7062
self._retries = value
7163

64+
65+
class Retry(AbstractRetry[Exception]):
66+
__hash__ = AbstractRetry.__hash__
67+
68+
def __init__(
69+
self,
70+
backoff: "AbstractBackoff",
71+
retries: int,
72+
supported_errors: Tuple[Type[Exception], ...] = (
73+
ConnectionError,
74+
TimeoutError,
75+
socket.timeout,
76+
),
77+
):
78+
super().__init__(backoff, retries, supported_errors)
79+
80+
def __eq__(self, other: Any) -> bool:
81+
if not isinstance(other, Retry):
82+
return NotImplemented
83+
84+
return (
85+
self._backoff == other._backoff
86+
and self._retries == other._retries
87+
and set(self._supported_errors) == set(other._supported_errors)
88+
)
89+
7290
def call_with_retry(
7391
self,
7492
do: Callable[[], T],

tests/test_retry.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from unittest.mock import patch
22

33
import pytest
4+
from redis.asyncio.retry import Retry as AsyncRetry
45
from redis.backoff import (
56
AbstractBackoff,
67
ConstantBackoff,
@@ -89,6 +90,7 @@ def test_retry_on_error_retry(self, Class, retries):
8990
assert c.retry._retries == retries
9091

9192

93+
@pytest.mark.parametrize("retry_class", [Retry, AsyncRetry])
9294
@pytest.mark.parametrize(
9395
"args",
9496
[
@@ -108,8 +110,8 @@ def test_retry_on_error_retry(self, Class, retries):
108110
for backoff in ((Backoff(), 2), (Backoff(25), 5), (Backoff(25, 5), 5))
109111
],
110112
)
111-
def test_retry_eq_and_hashable(args):
112-
assert Retry(*args) == Retry(*args)
113+
def test_retry_eq_and_hashable(retry_class, args):
114+
assert retry_class(*args) == retry_class(*args)
113115

114116
# create another retry object with different parameters
115117
copy = list(args)
@@ -118,9 +120,19 @@ def test_retry_eq_and_hashable(args):
118120
else:
119121
copy[0] = ConstantBackoff(9000)
120122

121-
assert Retry(*args) != Retry(*copy)
122-
assert Retry(*copy) != Retry(*args)
123-
assert len({Retry(*args), Retry(*args), Retry(*copy), Retry(*copy)}) == 2
123+
assert retry_class(*args) != retry_class(*copy)
124+
assert retry_class(*copy) != retry_class(*args)
125+
assert (
126+
len(
127+
{
128+
retry_class(*args),
129+
retry_class(*args),
130+
retry_class(*copy),
131+
retry_class(*copy),
132+
}
133+
)
134+
== 2
135+
)
124136

125137

126138
class TestRetry:

0 commit comments

Comments
 (0)