Skip to content

Commit 1386824

Browse files
committed
fix connect_to_address
1 parent a9f6f80 commit 1386824

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

redis/asyncio/sentinel.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ async def connect_to_same_address(self):
9292
lambda error: asyncio.sleep(0),
9393
)
9494

95+
async def connect_to_address(self, address):
96+
"""
97+
Similar to connect, but instead of rotating to the next slave (if not in master mode),
98+
it just connects to the same address of the connection object.
99+
"""
100+
self.host, self.port = address
101+
return await self.connect_to_same_address()
102+
95103
async def read_response(
96104
self,
97105
disable_decoding: bool = False,
@@ -280,8 +288,8 @@ async def get_connection(
280288
# connect to the previous replica.
281289
# This will connect to the host and port of the replica
282290
else:
283-
await connection.connect_to_same_address()
284-
self.ensure_connection_connected_to_address(connection)
291+
await connection.connect_to_address((server_host, server_port))
292+
await self.ensure_connection_connected_to_address(connection)
285293
except BaseException:
286294
# Release the connection back to the pool so that we don't
287295
# leak it

redis/sentinel.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ def connect_to_same_address(self):
7474
lambda error: None
7575
)
7676

77+
def connect_to_address(self, address):
78+
"""
79+
Similar to connect, but instead of rotating to the next slave (if not in master mode),
80+
it just connects to the address supplied.
81+
"""
82+
self.host, self.port = address
83+
return self.connect_to_same_address()
84+
7785
def can_read_same_address(self, timeout=0):
7886
"""Similar to can_read_same_address, but calls connect_to_same_address instead of connect"""
7987
sock = self._sock
@@ -312,7 +320,7 @@ def get_connection(
312320
# connect to the previous replica.
313321
# This will connect to the host and port of the replica
314322
else:
315-
connection.connect_to_same_address()
323+
connection.connect_to_address((server_host, server_port))
316324
self.ensure_connection_connected_to_address(connection)
317325
except BaseException:
318326
# Release the connection back to the pool so that we don't

tests/test_asyncio/test_connection_pool.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ class DummyConnection(Connection):
9494

9595
def __init__(self, **kwargs):
9696
self.kwargs = kwargs
97+
self.host = kwargs.get("host", None)
98+
self.port = kwargs.get("port", None)
9799

98100
def repr_pieces(self):
99101
return [("id", id(self)), ("kwargs", self.kwargs)]

tests/test_connection_pool.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class DummyConnection:
2020
def __init__(self, **kwargs):
2121
self.kwargs = kwargs
2222
self.pid = os.getpid()
23+
self.host = kwargs.get("host", None)
24+
self.port = kwargs.get("port", None)
2325

2426
def connect(self):
2527
pass

0 commit comments

Comments
 (0)