Skip to content

Commit 110bb6b

Browse files
committed
add is_supported_error() to retry
1 parent ae88892 commit 110bb6b

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

redis/cluster.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,7 @@ def execute_command(self, *args, **kwargs):
11031103
# The nodes and slots cache were reinitialized.
11041104
# Try again with the new cluster setup.
11051105
retry_attempts -= 1
1106-
if self.retry and isinstance(e, self.retry._supported_errors):
1106+
if self.retry and self.retry.is_supported_error(e):
11071107
backoff = self.retry._backoff.compute(
11081108
self.cluster_error_retry_attempts - retry_attempts
11091109
)
@@ -2039,9 +2039,7 @@ def _send_cluster_commands(
20392039
n.connection_pool.release(n.connection)
20402040
n.connection = None
20412041
nodes = {}
2042-
if self.retry and isinstance(
2043-
e, self.retry._supported_errors
2044-
):
2042+
if self.retry and self.retry.is_supported_error(e):
20452043
backoff = self.retry._backoff.compute(attempts_count)
20462044
if backoff > 0:
20472045
time.sleep(backoff)

redis/retry.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ def update_supported_errors(self, specified_errors: list):
3232
set(self._supported_errors + tuple(specified_errors))
3333
)
3434

35+
def is_supported_error(self, error):
36+
return isinstance(error, self._supported_errors)
37+
3538
def call_with_retry(self, do, fail):
3639
"""
3740
Execute an operation that might fail and returns its result, or

tests/test_retry.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from redis.exceptions import (
99
BusyLoadingError,
1010
ConnectionError,
11+
RedisClusterException,
12+
RedisError,
1113
ReadOnlyError,
1214
TimeoutError,
1315
)
@@ -122,6 +124,16 @@ def test_infinite_retry(self):
122124
assert self.actual_attempts == 5
123125
assert self.actual_failures == 5
124126

127+
@pytest.mark.parametrize("exception_class", [ConnectionError, TimeoutError])
128+
def test_is_supported_error_true(self, exception_class):
129+
retry = Retry(BackoffMock(), -1)
130+
assert retry.is_supported_error(exception_class())
131+
132+
@pytest.mark.parametrize("exception_class", [RedisClusterException, RedisError])
133+
def test_is_supported_error_false(self, exception_class):
134+
retry = Retry(BackoffMock(), -1)
135+
assert not retry.is_supported_error(exception_class())
136+
125137

126138
@pytest.mark.onlynoncluster
127139
class TestRedisClientRetry:

0 commit comments

Comments
 (0)