Skip to content

Commit 9e8cfaa

Browse files
Fix retry attribute in UnixDomainSocketConnection (#1604)
1 parent 358c293 commit 9e8cfaa

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

redis/connection.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,13 @@ def __init__(self, path='', db=0, username=None, password=None,
880880
encoding_errors='strict', decode_responses=False,
881881
retry_on_timeout=False,
882882
parser_class=DefaultParser, socket_read_size=65536,
883-
health_check_interval=0, client_name=None):
883+
health_check_interval=0, client_name=None,
884+
retry=None):
885+
"""
886+
Initialize a new UnixDomainSocketConnection.
887+
To specify a retry policy, first set `retry_on_timeout` to `True`
888+
then set `retry` to a valid `Retry` object
889+
"""
884890
self.pid = os.getpid()
885891
self.path = path
886892
self.db = db
@@ -889,6 +895,14 @@ def __init__(self, path='', db=0, username=None, password=None,
889895
self.password = password
890896
self.socket_timeout = socket_timeout
891897
self.retry_on_timeout = retry_on_timeout
898+
if retry_on_timeout:
899+
if retry is None:
900+
self.retry = Retry(NoBackoff(), 1)
901+
else:
902+
# deep-copy the Retry object as it is mutable
903+
self.retry = copy.deepcopy(retry)
904+
else:
905+
self.retry = Retry(NoBackoff(), 0)
892906
self.health_check_interval = health_check_interval
893907
self.next_health_check = 0
894908
self.encoder = Encoder(encoding, encoding_errors, decode_responses)

tests/test_retry.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pytest
33

44
from redis.exceptions import ConnectionError
5-
from redis.connection import Connection
5+
from redis.connection import Connection, UnixDomainSocketConnection
66
from redis.retry import Retry
77

88

@@ -20,20 +20,22 @@ def compute(self, failures):
2020

2121

2222
class TestConnectionConstructorWithRetry:
23-
"Test that the Connection constructor properly handles Retry objects"
23+
"Test that the Connection constructors properly handles Retry objects"
2424

2525
@pytest.mark.parametrize("retry_on_timeout", [False, True])
26-
def test_retry_on_timeout_boolean(self, retry_on_timeout):
27-
c = Connection(retry_on_timeout=retry_on_timeout)
26+
@pytest.mark.parametrize("Class", [Connection, UnixDomainSocketConnection])
27+
def test_retry_on_timeout_boolean(self, Class, retry_on_timeout):
28+
c = Class(retry_on_timeout=retry_on_timeout)
2829
assert c.retry_on_timeout == retry_on_timeout
2930
assert isinstance(c.retry, Retry)
3031
assert c.retry._retries == (1 if retry_on_timeout else 0)
3132

3233
@pytest.mark.parametrize("retries", range(10))
33-
def test_retry_on_timeout_retry(self, retries):
34+
@pytest.mark.parametrize("Class", [Connection, UnixDomainSocketConnection])
35+
def test_retry_on_timeout_retry(self, Class, retries):
3436
retry_on_timeout = retries > 0
35-
c = Connection(retry_on_timeout=retry_on_timeout,
36-
retry=Retry(NoBackoff(), retries))
37+
c = Class(retry_on_timeout=retry_on_timeout,
38+
retry=Retry(NoBackoff(), retries))
3739
assert c.retry_on_timeout == retry_on_timeout
3840
assert isinstance(c.retry, Retry)
3941
assert c.retry._retries == retries

0 commit comments

Comments
 (0)