Skip to content

Commit

Permalink
[GROW-2938] style fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zach-iee committed Jun 22, 2023
1 parent bc3192f commit 902935f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
8 changes: 5 additions & 3 deletions redis/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ def __init__(
kwargs.update({"retry": self.retry})
else:
self.retry = Retry(default_backoff(), 0)
kwargs['retry'] = self.retry
kwargs["retry"] = self.retry

self.encoder = Encoder(
kwargs.get("encoding", "utf-8"),
Expand Down Expand Up @@ -1095,7 +1095,9 @@ def execute_command(self, *args, **kwargs):
# Try again with the new cluster setup.
retry_attempts -= 1
if self.retry and isinstance(e, self.retry._supported_errors):
backoff = self.retry._backoff.compute(self.cluster_error_retry_attempts - retry_attempts)
backoff = self.retry._backoff.compute(
self.cluster_error_retry_attempts - retry_attempts
)
if backoff > 0:
time.sleep(backoff)
continue
Expand Down Expand Up @@ -1955,7 +1957,7 @@ def send_cluster_commands(
stack,
raise_on_error=raise_on_error,
allow_redirections=allow_redirections,
attempts_count=self.cluster_error_retry_attempts - retry_attempts
attempts_count=self.cluster_error_retry_attempts - retry_attempts,
)
except (ClusterDownError, ConnectionError, TimeoutError) as e:
if retry_attempts > 0:
Expand Down
20 changes: 15 additions & 5 deletions tests/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
import pytest

from redis import Redis
from redis.backoff import ExponentialBackoff, NoBackoff, default_backoff, ConstantBackoff
from redis.backoff import (
ConstantBackoff,
ExponentialBackoff,
NoBackoff,
default_backoff,
)
from redis.cluster import (
PRIMARY,
REDIS_CLUSTER_HASH_SLOTS,
Expand All @@ -36,7 +41,8 @@
RedisClusterException,
RedisError,
ResponseError,
TimeoutError, SlotNotCoveredError,
SlotNotCoveredError,
TimeoutError,
)
from redis.retry import Retry
from redis.utils import str_if_bytes
Expand Down Expand Up @@ -901,29 +907,33 @@ def address_remap(address):
n_used = sum((1 if p.n_connections else 0) for p in proxies)
assert n_used > 1

@pytest.mark.parametrize('error', [ConnectionError, TimeoutError])
@pytest.mark.parametrize("error", [ConnectionError, TimeoutError])
def test_additional_backoff_redis_cluster(self, error):
with patch.object(ConstantBackoff, "compute") as compute:

def _compute(target_node, *args, **kwargs):
return 1

compute.side_effect = _compute
with patch.object(RedisCluster, "_execute_command") as execute_command:

def raise_error(target_node, *args, **kwargs):
execute_command.failed_calls += 1
raise error("mocked error")

execute_command.side_effect = raise_error

rc = get_mocked_redis_client(
host=default_host, port=default_port, retry=Retry(ConstantBackoff(1), 3)
host=default_host,
port=default_port,
retry=Retry(ConstantBackoff(1), 3),
)

with pytest.raises(error):
rc.get("bar")
assert compute.call_count == rc.cluster_error_retry_attempts

@pytest.mark.parametrize('reinitialize_steps', [2, 10, 99])
@pytest.mark.parametrize("reinitialize_steps", [2, 10, 99])
def test_recover_slot_not_covered_error(self, request, reinitialize_steps):
rc = _get_client(RedisCluster, request, reinitialize_steps=reinitialize_steps)
key = uuid.uuid4().hex
Expand Down

0 comments on commit 902935f

Please sign in to comment.