Skip to content

Commit c2a62b2

Browse files
committed
[GROW-2938] add backoff to more errors
1 parent 1ea657e commit c2a62b2

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

redis/cluster.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,10 @@ def execute_command(self, *args, **kwargs):
10841084
# The nodes and slots cache were reinitialized.
10851085
# Try again with the new cluster setup.
10861086
retry_attempts -= 1
1087+
if self.retry and isinstance(e, self.retry._supported_errors):
1088+
backoff = self.retry._backoff.compute(self.cluster_error_retry_attempts - retry_attempts)
1089+
if backoff > 0:
1090+
time.sleep(backoff)
10871091
continue
10881092
else:
10891093
# raise the exception

tests/test_cluster.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import pytest
1313

1414
from redis import Redis
15-
from redis.backoff import ExponentialBackoff, NoBackoff, default_backoff
15+
from redis.backoff import ExponentialBackoff, NoBackoff, default_backoff, ConstantBackoff
1616
from redis.cluster import (
1717
PRIMARY,
1818
REDIS_CLUSTER_HASH_SLOTS,
@@ -900,6 +900,28 @@ def address_remap(address):
900900
n_used = sum((1 if p.n_connections else 0) for p in proxies)
901901
assert n_used > 1
902902

903+
@pytest.mark.parametrize('error', [ConnectionError, TimeoutError])
904+
def test_additional_backoff_redis_cluster(self, error):
905+
with patch.object(ConstantBackoff, "compute") as compute:
906+
def _compute(target_node, *args, **kwargs):
907+
return 1
908+
909+
compute.side_effect = _compute
910+
with patch.object(RedisCluster, "_execute_command") as execute_command:
911+
def raise_error(target_node, *args, **kwargs):
912+
execute_command.failed_calls += 1
913+
raise error("mocked error")
914+
915+
execute_command.side_effect = raise_error
916+
917+
rc = get_mocked_redis_client(
918+
host=default_host, port=default_port, retry=Retry(ConstantBackoff(1), 3)
919+
)
920+
921+
with pytest.raises(error):
922+
rc.get("bar")
923+
assert compute.call_count == rc.cluster_error_retry_attempts
924+
903925

904926
@pytest.mark.onlycluster
905927
class TestClusterRedisCommands:

0 commit comments

Comments
 (0)