Skip to content

Commit ba38657

Browse files
committed
better inline
1 parent 8ab5688 commit ba38657

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

redis/asyncio/sentinel.py

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -198,23 +198,21 @@ async def get_connection(
198198
) -> SentinelManagedConnection:
199199
"""
200200
Get a connection from the pool.
201-
`xxx_scan_iter` commands needs to be handled specially.
201+
'xxxscan_iter' ('scan_iter', 'hscan_iter', 'sscan_iter', 'zscan_iter')
202+
commands needs to be handled specially.
202203
If the client is created using a connection pool, in replica mode,
203-
all `scan` command-equivalent of the `xxx_scan_iter` commands needs
204+
all 'scan' command-equivalent of the 'xxx_scan_iter' commands needs
204205
to be issued to the same Redis replica.
205206
206207
The way each server positions each key is different with one another,
207-
and the cursor acts as the 'offset' of the scan.
208-
Hence, all scans coming from a single xxx_scan_iter_channel command
208+
and the cursor acts as the offset of the scan.
209+
Hence, all scans coming from a single 'xxx_scan_iter_channel' command
209210
should go to the same replica.
210211
"""
211-
# If not an iter command or in master mode, call super()
212-
# No custom logic for master, because there's only 1 master.
213-
# The bug is only when Redis has the possibility to connect to multiple replicas
212+
# If not an iter command or in master mode, call superclass' implementation
214213
if not (iter_req_id := options.get("_iter_req_id", None)) or self.is_master:
215214
return await super().get_connection(command_name, *keys, **options)
216215

217-
# Check if this iter request has already been directed to a particular server
218216
# Check if this iter request has already been directed to a particular server
219217
(
220218
server_host,
@@ -232,8 +230,8 @@ async def get_connection(
232230
else:
233231
# Check from the available connections, if any of the connection
234232
# is connected to the host and port that we want
235-
# If yes, use that connection
236233
for available_connection in self._available_connections.copy():
234+
# if yes, use that connection
237235
if (
238236
available_connection.host == server_host
239237
and available_connection.port == server_port
@@ -247,22 +245,20 @@ async def get_connection(
247245
assert connection
248246
self._in_use_connections.add(connection)
249247
try:
250-
# ensure this connection is connected to Redis
251-
# If this is the first scan request,
252-
# just call the SentinelManagedConnection.connect()
253-
# This will call rotate_slaves
254-
# and connect to a random replica
248+
# Ensure this connection is connected to Redis
249+
# If this is the first scan request, it will
250+
# call rotate_slaves and connect to a random replica
255251
if server_port is None or server_port is None:
256252
await connection.connect()
257253
# If this is not the first scan request,
258-
# connect to the particular address and port
254+
# connect to the previous replica.
255+
# This will connect to the host and port of the replica
259256
else:
260-
# This will connect to the host and port that we've specified above
261257
await connection.connect_to_address(server_host, server_port)
262-
# connections that the pool provides should be ready to send
263-
# a command. if not, the connection was either returned to the
258+
# Connections that the pool provides should be ready to send
259+
# a command. If not, the connection was either returned to the
264260
# pool before all data has been read or the socket has been
265-
# closed. either way, reconnect and verify everything is good.
261+
# closed. Either way, reconnect and verify everything is good.
266262
try:
267263
if await connection.can_read_destructive():
268264
raise ConnectionError("Connection has data") from None
@@ -272,7 +268,7 @@ async def get_connection(
272268
if await connection.can_read_destructive():
273269
raise ConnectionError("Connection not ready") from None
274270
except BaseException:
275-
# release the connection back to the pool so that we don't
271+
# Release the connection back to the pool so that we don't
276272
# leak it
277273
await self.release(connection)
278274
raise
@@ -281,7 +277,6 @@ async def get_connection(
281277
connection.host,
282278
connection.port,
283279
)
284-
285280
return connection
286281

287282

0 commit comments

Comments
 (0)