Skip to content

Commit 10367d2

Browse files
committed
Changed get_default_backoff to default_backoff, removed retry_on_error and connection_error_retry_attempts from RedisCluster, default retry changed to no retries
1 parent eba9a09 commit 10367d2

File tree

7 files changed

+27
-51
lines changed

7 files changed

+27
-51
lines changed

redis/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
22

3-
from redis.backoff import get_default_backoff
3+
from redis.backoff import default_backoff
44
from redis.client import Redis, StrictRedis
55
from redis.cluster import RedisCluster
66
from redis.connection import (
@@ -65,7 +65,7 @@ def int_or_str(value):
6565
"ConnectionPool",
6666
"DataError",
6767
"from_url",
68-
"get_default_backoff",
68+
"default_backoff",
6969
"InvalidResponse",
7070
"PubSubError",
7171
"ReadOnlyError",

redis/asyncio/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
SentinelManagedSSLConnection,
1616
)
1717
from redis.asyncio.utils import from_url
18-
from redis.backoff import get_default_backoff
18+
from redis.backoff import default_backoff
1919
from redis.exceptions import (
2020
AuthenticationError,
2121
AuthenticationWrongNumberOfArgsError,
@@ -44,7 +44,7 @@
4444
"ConnectionPool",
4545
"DataError",
4646
"from_url",
47-
"get_default_backoff",
47+
"default_backoff",
4848
"InvalidResponse",
4949
"PubSubError",
5050
"ReadOnlyError",

redis/asyncio/cluster.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
)
2727
from redis.asyncio.parser import CommandsParser
2828
from redis.asyncio.retry import Retry
29-
from redis.backoff import get_default_backoff
29+
from redis.backoff import default_backoff
3030
from redis.client import EMPTY_RESPONSE, NEVER_DECODE, AbstractRedis
3131
from redis.cluster import (
3232
PIPELINE_BLOCKED_COMMANDS,
@@ -138,7 +138,7 @@ class RedisCluster(AbstractRedis, AbstractRedisCluster, AsyncRedisClusterCommand
138138
| Number of times to retry before reinitializing when :class:`~.TimeoutError`
139139
or :class:`~.ConnectionError` are encountered.
140140
The default backoff strategy will be set if Retry object is not passed (see
141-
get_default_backoff in backoff.py). To change it, pass a custom Retry object
141+
default_backoff in backoff.py). To change it, pass a custom Retry object
142142
using the "retry" keyword.
143143
:param max_connections:
144144
| Maximum number of connections per node. If there are no free connections & the
@@ -309,7 +309,7 @@ def __init__(
309309
if retry or retry_on_error or connection_error_retry_attempts > 0:
310310
# Set a retry object for all cluster nodes
311311
self.retry = retry or Retry(
312-
get_default_backoff(), connection_error_retry_attempts
312+
default_backoff(), connection_error_retry_attempts
313313
)
314314
if retry_on_error is None:
315315
# Default errors for retrying
@@ -642,7 +642,9 @@ async def execute_command(self, *args: EncodableT, **kwargs: Any) -> Any:
642642
target_nodes_specified = True
643643
retry_attempts = 0
644644

645-
while True:
645+
# Add one for the first execution
646+
execute_attempts = 1 + retry_attempts
647+
for _ in range(execute_attempts):
646648
if self._initialize:
647649
await self.initialize()
648650
try:

redis/backoff.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import random
22
from abc import ABC, abstractmethod
33

4+
# Maximum backoff between each retry in seconds
45
DEFAULT_CAP = 0.512
6+
# Minimum backoff between each retry in seconds
57
DEFAULT_BASE = 0.008
68

79

@@ -108,5 +110,5 @@ def compute(self, failures):
108110
return self._previous_backoff
109111

110112

111-
def get_default_backoff():
113+
def default_backoff():
112114
return EqualJitterBackoff()

redis/cluster.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from collections import OrderedDict
77
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
88

9-
from redis.backoff import get_default_backoff
9+
from redis.backoff import default_backoff, NoBackoff
1010
from redis.client import CaseInsensitiveDict, PubSub, Redis, parse_scan
1111
from redis.commands import READ_COMMANDS, CommandsParser, RedisClusterCommands
1212
from redis.connection import ConnectionPool, DefaultParser, Encoder, parse_url
@@ -430,9 +430,7 @@ def __init__(
430430
port: int = 6379,
431431
startup_nodes: Optional[List["ClusterNode"]] = None,
432432
cluster_error_retry_attempts: int = 3,
433-
connection_error_retry_attempts: int = 3,
434433
retry: Optional["Retry"] = None,
435-
retry_on_error: Optional[List[Exception]] = None,
436434
require_full_coverage: bool = False,
437435
reinitialize_steps: int = 10,
438436
read_from_replicas: bool = False,
@@ -476,13 +474,7 @@ def __init__(
476474
Number of times to retry before raising an error when
477475
:class:`~.TimeoutError` or :class:`~.ConnectionError` or
478476
:class:`~.ClusterDownError` are encountered
479-
:param connection_error_retry_attempts:
480-
Number of times to retry before reinitializing when :class:`~.TimeoutError`
481-
or :class:`~.ConnectionError` are encountered.
482-
The default backoff strategy will be set if Retry object is not passed (see
483-
get_default_backoff in backoff.py). To change it, pass a custom Retry object
484-
using the "retry" keyword.
485-
:reinitialize_steps: 'int'
477+
:param reinitialize_steps:
486478
Specifies the number of MOVED errors that need to occur before
487479
reinitializing the whole cluster topology. If a MOVED error occurs
488480
and the cluster does not need to be reinitialized on this current
@@ -550,17 +542,11 @@ def __init__(
550542
self.user_on_connect_func = kwargs.pop("redis_connect_func", None)
551543
kwargs.update({"redis_connect_func": self.on_connect})
552544
kwargs = cleanup_kwargs(**kwargs)
553-
554-
if retry or retry_on_error or connection_error_retry_attempts > 0:
555-
# Set a retry object for all cluster nodes
556-
self.retry = retry or Retry(
557-
get_default_backoff(), connection_error_retry_attempts
558-
)
559-
if retry_on_error is None:
560-
# Default errors for retrying
561-
retry_on_error = [ConnectionError, TimeoutError]
562-
self.retry.update_supported_errors(retry_on_error)
545+
if retry:
546+
self.retry = retry
563547
kwargs.update({"retry": self.retry})
548+
else:
549+
kwargs.update({"retry": Retry(default_backoff(), 0)})
564550

565551
self.encoder = Encoder(
566552
kwargs.get("encoding", "utf-8"),
@@ -1019,7 +1005,9 @@ def execute_command(self, *args, **kwargs):
10191005
retry_attempts = (
10201006
0 if target_nodes_specified else self.cluster_error_retry_attempts
10211007
)
1022-
while True:
1008+
# Add one for the first execution
1009+
execute_attempts = 1 + retry_attempts
1010+
for _ in range(execute_attempts):
10231011
try:
10241012
res = {}
10251013
if not target_nodes_specified:

tests/test_asyncio/test_cluster.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from redis.asyncio.connection import Connection, SSLConnection
1515
from redis.asyncio.parser import CommandsParser
1616
from redis.asyncio.retry import Retry
17-
from redis.backoff import ExponentialBackoff, NoBackoff, get_default_backoff
17+
from redis.backoff import ExponentialBackoff, NoBackoff, default_backoff
1818
from redis.cluster import PIPELINE_BLOCKED_COMMANDS, PRIMARY, REPLICA, get_node_name
1919
from redis.crc import REDIS_CLUSTER_HASH_SLOTS, key_slot
2020
from redis.exceptions import (
@@ -278,7 +278,7 @@ async def test_cluster_retry_object(self, request: FixtureRequest) -> None:
278278
retry = rc_default.connection_kwargs.get("retry")
279279
assert isinstance(retry, Retry)
280280
assert retry._retries == 3
281-
assert isinstance(retry._backoff, type(get_default_backoff()))
281+
assert isinstance(retry._backoff, type(default_backoff()))
282282
assert rc_default.get_node("127.0.0.1", 16379).connection_kwargs.get(
283283
"retry"
284284
) == rc_default.get_node("127.0.0.1", 16380).connection_kwargs.get("retry")

tests/test_cluster.py

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pytest
88

99
from redis import Redis
10-
from redis.backoff import ExponentialBackoff, NoBackoff, get_default_backoff
10+
from redis.backoff import ExponentialBackoff, NoBackoff, default_backoff
1111
from redis.cluster import (
1212
PRIMARY,
1313
REDIS_CLUSTER_HASH_SLOTS,
@@ -770,8 +770,8 @@ def test_cluster_retry_object(self, r) -> None:
770770
# Test default retry
771771
retry = r.get_connection_kwargs().get("retry")
772772
assert isinstance(retry, Retry)
773-
assert retry._retries == 3
774-
assert isinstance(retry._backoff, type(get_default_backoff()))
773+
assert retry._retries == 0
774+
assert isinstance(retry._backoff, type(default_backoff()))
775775
node1 = r.get_node("127.0.0.1", 16379).redis_connection
776776
node2 = r.get_node("127.0.0.1", 16380).redis_connection
777777
assert node1.get_retry()._retries == node2.get_retry()._retries
@@ -786,22 +786,6 @@ def test_cluster_retry_object(self, r) -> None:
786786
== retry._retries
787787
)
788788

789-
# Test no connection retries
790-
rc_no_retries = RedisCluster(
791-
"127.0.0.1", 16379, connection_error_retry_attempts=0
792-
)
793-
assert (
794-
rc_no_retries.get_node("127.0.0.1", 16379).redis_connection.get_retry()
795-
is None
796-
)
797-
rc_no_retries = RedisCluster("127.0.0.1", 16379, retry=Retry(NoBackoff(), 0))
798-
assert (
799-
rc_no_retries.get_node("127.0.0.1", 16379)
800-
.redis_connection.get_retry()
801-
._retries
802-
== 0
803-
)
804-
805789

806790
@pytest.mark.onlycluster
807791
class TestClusterRedisCommands:

0 commit comments

Comments
 (0)