Skip to content

Commit 8ab5688

Browse files
committed
lint and format
1 parent e6003e8 commit 8ab5688

File tree

3 files changed

+128
-75
lines changed

3 files changed

+128
-75
lines changed

redis/asyncio/sentinel.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import asyncio
22
import random
33
import weakref
4-
import uuid
5-
from typing import AsyncIterator, Iterable, Mapping, Optional, Sequence, Tuple, Type, Any
4+
from typing import (
5+
Any,
6+
AsyncIterator,
7+
Iterable,
8+
Mapping,
9+
Optional,
10+
Sequence,
11+
Tuple,
12+
Type,
13+
)
614

715
from redis.asyncio.client import Redis
816
from redis.asyncio.connection import (
@@ -66,13 +74,13 @@ async def connect(self):
6674
self._connect_retry,
6775
lambda error: asyncio.sleep(0),
6876
)
69-
77+
7078
async def _connect_to_address_retry(self, host: str, port: int) -> None:
7179
if self._reader:
7280
return # already connected
7381
try:
7482
return await self.connect_to((host, port))
75-
except ConnectionError as exc:
83+
except ConnectionError:
7684
raise SlaveNotFoundError
7785

7886
async def connect_to_address(self, host: str, port: int) -> None:
@@ -170,11 +178,6 @@ async def get_master_address(self):
170178

171179
async def rotate_slaves(self) -> AsyncIterator:
172180
"""Round-robin slave balancer"""
173-
(
174-
server_host,
175-
server_port,
176-
) = self._request_id_to_replica_address.get(iter_req_id, (None, None))
177-
178181
slaves = await self.sentinel_manager.discover_slaves(self.service_name)
179182
if slaves:
180183
if self.slave_rr_counter is None:
@@ -201,15 +204,15 @@ async def get_connection(
201204
to be issued to the same Redis replica.
202205
203206
The way each server positions each key is different with one another,
204-
and the cursor acts as the 'offset' of the scan.
205-
Hence, all scans coming from a single xxx_scan_iter_channel command
207+
and the cursor acts as the 'offset' of the scan.
208+
Hence, all scans coming from a single xxx_scan_iter_channel command
206209
should go to the same replica.
207210
"""
208211
# If not an iter command or in master mode, call super()
209212
# No custom logic for master, because there's only 1 master.
210213
# The bug is only when Redis has the possibility to connect to multiple replicas
211214
if not (iter_req_id := options.get("_iter_req_id", None)) or self.is_master:
212-
return await super().get_connection(command_name, *keys, **options) # type: ignore[no-any-return]
215+
return await super().get_connection(command_name, *keys, **options)
213216

214217
# Check if this iter request has already been directed to a particular server
215218
# Check if this iter request has already been directed to a particular server
@@ -222,7 +225,7 @@ async def get_connection(
222225
# get a connection from the pool
223226
if server_host is None or server_port is None:
224227
try:
225-
connection = self._available_connections.pop() # type: ignore [assignment]
228+
connection = self._available_connections.pop()
226229
except IndexError:
227230
connection = self.make_connection()
228231
# If this is not the first scan request of the iter command
@@ -236,7 +239,7 @@ async def get_connection(
236239
and available_connection.port == server_port
237240
):
238241
self._available_connections.remove(available_connection)
239-
connection = available_connection # type: ignore[assignment]
242+
connection = available_connection
240243
# If not, make a new dummy connection object, and set its host and port
241244
# to the one that we want later in the call to ``connect_to_address``
242245
if not connection:
@@ -255,22 +258,18 @@ async def get_connection(
255258
# connect to the particular address and port
256259
else:
257260
# This will connect to the host and port that we've specified above
258-
await connection.connect_to_address(server_host, server_port) # type: ignore[arg-type]
261+
await connection.connect_to_address(server_host, server_port)
259262
# connections that the pool provides should be ready to send
260263
# a command. if not, the connection was either returned to the
261264
# pool before all data has been read or the socket has been
262265
# closed. either way, reconnect and verify everything is good.
263266
try:
264-
# type ignore below:
265-
# attr Not defined in redis stubs and
266-
# we don't need to create a subclass to help with this single attr
267-
if await connection.can_read_destructive(): # type: ignore[attr-defined]
267+
if await connection.can_read_destructive():
268268
raise ConnectionError("Connection has data") from None
269269
except (ConnectionError, OSError):
270270
await connection.disconnect()
271271
await connection.connect()
272-
# type ignore below: similar to above
273-
if await connection.can_read_destructive(): # type: ignore[attr-defined]
272+
if await connection.can_read_destructive():
274273
raise ConnectionError("Connection not ready") from None
275274
except BaseException:
276275
# release the connection back to the pool so that we don't
@@ -286,7 +285,6 @@ async def get_connection(
286285
return connection
287286

288287

289-
290288
class Sentinel(AsyncSentinelCommands):
291289
"""
292290
Redis Sentinel cluster client

redis/commands/core.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# from __future__ import annotations
22

3-
import uuid
43
import datetime
54
import hashlib
5+
import uuid
66
import warnings
77
from typing import (
88
TYPE_CHECKING,
@@ -3222,12 +3222,19 @@ async def scan_iter(
32223222
HASH, LIST, SET, STREAM, STRING, ZSET
32233223
Additionally, Redis modules can expose other types as well.
32243224
"""
3225-
# DO NOT inline this statement to the scan call
3226-
iter_req_id = uuid.uuid4() # each iter command should have an ID to maintain connection to the same replica
3225+
# DO NOT inline this statement to the scan call
3226+
# Each iter command should have an ID to maintain
3227+
# connection to the same replica
3228+
iter_req_id = uuid.uuid4()
32273229
cursor = "0"
32283230
while cursor != 0:
32293231
cursor, data = await self.scan(
3230-
cursor=cursor, match=match, count=count, _type=_type, _iter_req_id=iter_req_id, **kwargs
3232+
cursor=cursor,
3233+
match=match,
3234+
count=count,
3235+
_type=_type,
3236+
_iter_req_id=iter_req_id,
3237+
**kwargs,
32313238
)
32323239
for d in data:
32333240
yield d
@@ -3246,8 +3253,10 @@ async def sscan_iter(
32463253
32473254
``count`` allows for hint the minimum number of returns
32483255
"""
3249-
# DO NOT inline this statement to the scan call
3250-
iter_req_id = uuid.uuid4() # each iter command should have an ID to maintain connection to the same replica
3256+
# DO NOT inline this statement to the scan call
3257+
# Each iter command should have an ID to maintain
3258+
# connection to the same replica
3259+
iter_req_id = uuid.uuid4()
32513260
cursor = "0"
32523261
while cursor != 0:
32533262
cursor, data = await self.sscan(
@@ -3270,8 +3279,10 @@ async def hscan_iter(
32703279
32713280
``count`` allows for hint the minimum number of returns
32723281
"""
3273-
# DO NOT inline this statement to the scan call
3274-
iter_req_id = uuid.uuid4() # each iter command should have an ID to maintain connection to the same replica
3282+
# DO NOT inline this statement to the scan call
3283+
# Each iter command should have an ID to maintain
3284+
# connection to the same replica
3285+
iter_req_id = uuid.uuid4()
32753286
cursor = "0"
32763287
while cursor != 0:
32773288
cursor, data = await self.hscan(
@@ -3297,8 +3308,10 @@ async def zscan_iter(
32973308
32983309
``score_cast_func`` a callable used to cast the score return value
32993310
"""
3300-
# DO NOT inline this statement to the scan call
3301-
iter_req_id = uuid.uuid4() # each iter command should have an ID to maintain connection to the same replica
3311+
# DO NOT inline this statement to the scan call
3312+
# Each iter command should have an ID to maintain
3313+
# connection to the same replica
3314+
iter_req_id = uuid.uuid4()
33023315
cursor = "0"
33033316
while cursor != 0:
33043317
cursor, data = await self.zscan(
@@ -3307,7 +3320,7 @@ async def zscan_iter(
33073320
match=match,
33083321
count=count,
33093322
score_cast_func=score_cast_func,
3310-
_iter_req_id=iter_req_id
3323+
_iter_req_id=iter_req_id,
33113324
)
33123325
for d in data:
33133326
yield d

0 commit comments

Comments
 (0)