Skip to content

Commit 6d414f5

Browse files
committed
fix connect_to args
1 parent 74fa506 commit 6d414f5

File tree

4 files changed

+17
-28
lines changed

4 files changed

+17
-28
lines changed

redis/asyncio/sentinel.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,16 @@ async def connect_to(self, address):
5656
if str_if_bytes(await self.read_response()) != "PONG":
5757
raise ConnectionError("PING failed")
5858

59-
async def _connect_retry(self):
59+
async def _connect_retry(self, same_address: bool = False):
6060
if self._reader:
6161
return # already connected
62+
# If same_server is True, it means that the connection
63+
# is not rotating to the next slave (if the connection pool is not master)
64+
if same_address:
65+
self.connect_to(self.host, self.port)
66+
return
67+
# If same_server is False, connnect to master in master mode
68+
# and rotate to the next slave in slave mode
6269
if self.connection_pool.is_master:
6370
await self.connect_to(await self.connection_pool.get_master_address())
6471
else:
@@ -75,22 +82,6 @@ async def connect(self):
7582
lambda error: asyncio.sleep(0),
7683
)
7784

78-
async def _connect_to_address_retry(self, host: str, port: int) -> None:
79-
if self._reader:
80-
return # already connected
81-
try:
82-
return await self.connect_to((host, port))
83-
except ConnectionError:
84-
raise SlaveNotFoundError
85-
86-
async def connect_to_address(self, host: str, port: int) -> None:
87-
# Connect to the specified host and port
88-
# instead of connecting to the master / rotated slaves
89-
return await self.retry.call_with_retry(
90-
lambda: self._connect_to_address_retry(host, port),
91-
lambda error: asyncio.sleep(0),
92-
)
93-
9485
async def connect_to_same_address(self):
9586
"""
9687
Similar to connect, but instead of rotating to the next slave (if not in master mode),
@@ -274,7 +265,7 @@ async def get_connection(
274265
host=server_host, port=server_port
275266
)
276267
# If not, make a new dummy connection object, and set its host and
277-
# port to the one that we want later in the call to ``connect_to_address``
268+
# port to the one that we want later in the call to ``connect_to_same_address``
278269
if not connection:
279270
connection = self.make_connection()
280271
assert connection
@@ -289,7 +280,7 @@ async def get_connection(
289280
# connect to the previous replica.
290281
# This will connect to the host and port of the replica
291282
else:
292-
await connection.connect_to_address(server_host, server_port)
283+
await connection.connect_to_same_address()
293284
self.ensure_connection_connected_to_address(connection)
294285
except BaseException:
295286
# Release the connection back to the pool so that we don't

redis/sentinel.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def _connect_retry(self, same_address: bool = False):
4747
# If same_server is True, it means that the connection
4848
# is not rotating to the next slave (if the connection pool is not master)
4949
if same_address:
50-
self.connect_to(self.host, self.port)
50+
self.connect_to((self.host, self.port))
5151
return
5252
# If same_server is False, connnect to master in master mode
5353
# and rotate to the next slave in slave mode
@@ -297,7 +297,7 @@ def get_connection(
297297
host=server_host, port=server_port
298298
)
299299
# If not, make a new dummy connection object, and set its host and port
300-
# to the one that we want later in the call to ``connect_to_address``
300+
# to the one that we want later in the call to ``connect_to_same_address``
301301
if not connection:
302302
connection = self.make_connection()
303303
assert connection
@@ -312,7 +312,7 @@ def get_connection(
312312
# connect to the previous replica.
313313
# This will connect to the host and port of the replica
314314
else:
315-
connection.connect_to_address(server_host, server_port)
315+
connection.connect_to_same_address()
316316
self.ensure_connection_connected_to_address(connection)
317317
except BaseException:
318318
# Release the connection back to the pool so that we don't

tests/test_asyncio/test_sentinel_managed_connection.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ async def mock_connect():
4343

4444

4545
class SentinelManagedConnectionMock(SentinelManagedConnection):
46-
async def connect_to_address(self, host: str, port: int) -> None:
47-
self.host = host
48-
self.port = port
46+
async def connect_to_same_address(self) -> None:
47+
pass
4948

5049
async def can_read_destructive(self) -> bool:
5150
# Mock this function to always return False.

tests/test_sentinel_managed_connection.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ async def mock_connect():
4242

4343

4444
class SentinelManagedConnectionMock(SentinelManagedConnection):
45-
def connect_to_address(self, host: str, port: int) -> None:
46-
self.host = host
47-
self.port = port
45+
def connect_to_same_address(self) -> None:
46+
pass
4847

4948
def can_read_destructive(self) -> bool:
5049
# Mock this function to always return False.

0 commit comments

Comments
 (0)