Skip to content

Commit

Permalink
add is_supported_error() to retry
Browse files Browse the repository at this point in the history
  • Loading branch information
zach-iee committed Aug 21, 2023
1 parent ae88892 commit 8d17920
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
6 changes: 2 additions & 4 deletions redis/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@ def execute_command(self, *args, **kwargs):
# The nodes and slots cache were reinitialized.
# Try again with the new cluster setup.
retry_attempts -= 1
if self.retry and isinstance(e, self.retry._supported_errors):
if self.retry and self.retry.is_supported_error(e):
backoff = self.retry._backoff.compute(
self.cluster_error_retry_attempts - retry_attempts
)
Expand Down Expand Up @@ -2039,9 +2039,7 @@ def _send_cluster_commands(
n.connection_pool.release(n.connection)
n.connection = None
nodes = {}
if self.retry and isinstance(
e, self.retry._supported_errors
):
if self.retry and self.retry.is_supported_error(e):
backoff = self.retry._backoff.compute(attempts_count)
if backoff > 0:
time.sleep(backoff)
Expand Down
3 changes: 3 additions & 0 deletions redis/retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def update_supported_errors(self, specified_errors: list):
set(self._supported_errors + tuple(specified_errors))
)

def is_supported_error(self, error):
return isinstance(error, self._supported_errors)

def call_with_retry(self, do, fail):
"""
Execute an operation that might fail and returns its result, or
Expand Down
12 changes: 12 additions & 0 deletions tests/test_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
BusyLoadingError,
ConnectionError,
ReadOnlyError,
RedisClusterException,
RedisError,
TimeoutError,
)
from redis.retry import Retry
Expand Down Expand Up @@ -122,6 +124,16 @@ def test_infinite_retry(self):
assert self.actual_attempts == 5
assert self.actual_failures == 5

@pytest.mark.parametrize("exception_class", [ConnectionError, TimeoutError])
def test_is_supported_error_true(self, exception_class):
retry = Retry(BackoffMock(), -1)
assert retry.is_supported_error(exception_class())

@pytest.mark.parametrize("exception_class", [RedisClusterException, RedisError])
def test_is_supported_error_false(self, exception_class):
retry = Retry(BackoffMock(), -1)
assert not retry.is_supported_error(exception_class())


@pytest.mark.onlynoncluster
class TestRedisClientRetry:
Expand Down

0 comments on commit 8d17920

Please sign in to comment.