Skip to content

Commit ca9bd63

Browse files
committed
lint and format
1 parent 8f2ec5f commit ca9bd63

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,
@@ -3226,12 +3226,19 @@ async def scan_iter(
32263226
HASH, LIST, SET, STREAM, STRING, ZSET
32273227
Additionally, Redis modules can expose other types as well.
32283228
"""
3229-
# DO NOT inline this statement to the scan call
3230-
iter_req_id = uuid.uuid4() # each iter command should have an ID to maintain connection to the same replica
3229+
# DO NOT inline this statement to the scan call
3230+
# Each iter command should have an ID to maintain
3231+
# connection to the same replica
3232+
iter_req_id = uuid.uuid4()
32313233
cursor = "0"
32323234
while cursor != 0:
32333235
cursor, data = await self.scan(
3234-
cursor=cursor, match=match, count=count, _type=_type, _iter_req_id=iter_req_id, **kwargs
3236+
cursor=cursor,
3237+
match=match,
3238+
count=count,
3239+
_type=_type,
3240+
_iter_req_id=iter_req_id,
3241+
**kwargs,
32353242
)
32363243
for d in data:
32373244
yield d
@@ -3250,8 +3257,10 @@ async def sscan_iter(
32503257
32513258
``count`` allows for hint the minimum number of returns
32523259
"""
3253-
# DO NOT inline this statement to the scan call
3254-
iter_req_id = uuid.uuid4() # each iter command should have an ID to maintain connection to the same replica
3260+
# DO NOT inline this statement to the scan call
3261+
# Each iter command should have an ID to maintain
3262+
# connection to the same replica
3263+
iter_req_id = uuid.uuid4()
32553264
cursor = "0"
32563265
while cursor != 0:
32573266
cursor, data = await self.sscan(
@@ -3277,8 +3286,10 @@ async def hscan_iter(
32773286
32783287
``no_values`` indicates to return only the keys, without values
32793288
"""
3280-
# DO NOT inline this statement to the scan call
3281-
iter_req_id = uuid.uuid4() # each iter command should have an ID to maintain connection to the same replica
3289+
# DO NOT inline this statement to the scan call
3290+
# Each iter command should have an ID to maintain
3291+
# connection to the same replica
3292+
iter_req_id = uuid.uuid4()
32823293
cursor = "0"
32833294
while cursor != 0:
32843295
cursor, data = await self.hscan(
@@ -3312,8 +3323,10 @@ async def zscan_iter(
33123323
33133324
``score_cast_func`` a callable used to cast the score return value
33143325
"""
3315-
# DO NOT inline this statement to the scan call
3316-
iter_req_id = uuid.uuid4() # each iter command should have an ID to maintain connection to the same replica
3326+
# DO NOT inline this statement to the scan call
3327+
# Each iter command should have an ID to maintain
3328+
# connection to the same replica
3329+
iter_req_id = uuid.uuid4()
33173330
cursor = "0"
33183331
while cursor != 0:
33193332
cursor, data = await self.zscan(
@@ -3322,7 +3335,7 @@ async def zscan_iter(
33223335
match=match,
33233336
count=count,
33243337
score_cast_func=score_cast_func,
3325-
_iter_req_id=iter_req_id
3338+
_iter_req_id=iter_req_id,
33263339
)
33273340
for d in data:
33283341
yield d

0 commit comments

Comments
 (0)