Skip to content

Commit 535a612

Browse files
async_cluster: improve docs (#2208)
- move response_callbacks kwarg inside __init__ to hide it from docs - use :member-order: bysource to group similar commands together
1 parent a2365d1 commit 535a612

File tree

2 files changed

+42
-38
lines changed

2 files changed

+42
-38
lines changed

docs/connections.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,19 @@ RedisCluster (Async)
7070
====================
7171
.. autoclass:: redis.asyncio.cluster.RedisCluster
7272
:members:
73+
:member-order: bysource
7374

7475
ClusterNode (Async)
7576
===================
7677
.. autoclass:: redis.asyncio.cluster.ClusterNode
7778
:members:
79+
:member-order: bysource
7880

7981
ClusterPipeline (Async)
80-
===================
82+
=======================
8183
.. autoclass:: redis.asyncio.cluster.ClusterPipeline
82-
:members:
84+
:members: execute_command, execute
85+
:member-order: bysource
8386

8487

8588
Connection

redis/asyncio/cluster.py

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -379,14 +379,9 @@ async def on_connect(self, connection: Connection) -> None:
379379
if str_if_bytes(await connection.read_response_without_lock()) != "OK":
380380
raise ConnectionError("READONLY command failed")
381381

382-
def get_node(
383-
self,
384-
host: Optional[str] = None,
385-
port: Optional[int] = None,
386-
node_name: Optional[str] = None,
387-
) -> Optional["ClusterNode"]:
388-
"""Get node by (host, port) or node_name."""
389-
return self.nodes_manager.get_node(host, port, node_name)
382+
def get_nodes(self) -> List["ClusterNode"]:
383+
"""Get all nodes of the cluster."""
384+
return list(self.nodes_manager.nodes_cache.values())
390385

391386
def get_primaries(self) -> List["ClusterNode"]:
392387
"""Get the primary nodes of the cluster."""
@@ -400,9 +395,29 @@ def get_random_node(self) -> "ClusterNode":
400395
"""Get a random node of the cluster."""
401396
return random.choice(list(self.nodes_manager.nodes_cache.values()))
402397

403-
def get_nodes(self) -> List["ClusterNode"]:
404-
"""Get all nodes of the cluster."""
405-
return list(self.nodes_manager.nodes_cache.values())
398+
def get_default_node(self) -> "ClusterNode":
399+
"""Get the default node of the client."""
400+
return self.nodes_manager.default_node
401+
402+
def set_default_node(self, node: "ClusterNode") -> None:
403+
"""
404+
Set the default node of the client.
405+
406+
:raises DataError: if None is passed or node does not exist in cluster.
407+
"""
408+
if not node or not self.get_node(node_name=node.name):
409+
raise DataError("The requested node does not exist in the cluster.")
410+
411+
self.nodes_manager.default_node = node
412+
413+
def get_node(
414+
self,
415+
host: Optional[str] = None,
416+
port: Optional[int] = None,
417+
node_name: Optional[str] = None,
418+
) -> Optional["ClusterNode"]:
419+
"""Get node by (host, port) or node_name."""
420+
return self.nodes_manager.get_node(host, port, node_name)
406421

407422
def get_node_from_key(
408423
self, key: str, replica: bool = False
@@ -413,6 +428,7 @@ def get_node_from_key(
413428
:param key:
414429
:param replica:
415430
| Indicates if a replica should be returned
431+
|
416432
None will returned if no replica holds this key
417433
418434
:raises SlotNotCoveredError: if the key is not covered by any slot.
@@ -431,24 +447,13 @@ def get_node_from_key(
431447

432448
return slot_cache[node_idx]
433449

434-
def get_default_node(self) -> "ClusterNode":
435-
"""Get the default node of the client."""
436-
return self.nodes_manager.default_node
437-
438-
def set_default_node(self, node: "ClusterNode") -> None:
450+
def keyslot(self, key: EncodableT) -> int:
439451
"""
440-
Set the default node of the client.
452+
Find the keyslot for a given key.
441453
442-
:raises DataError: if None is passed or node does not exist in cluster.
454+
See: https://redis.io/docs/manual/scaling/#redis-cluster-data-sharding
443455
"""
444-
if not node or not self.get_node(node_name=node.name):
445-
raise DataError("The requested node does not exist in the cluster.")
446-
447-
self.nodes_manager.default_node = node
448-
449-
def set_response_callback(self, command: str, callback: ResponseCallbackT) -> None:
450-
"""Set a custom response callback."""
451-
self.response_callbacks[command] = callback
456+
return key_slot(self.encoder.encode(key))
452457

453458
def get_encoder(self) -> Encoder:
454459
"""Get the encoder object of the client."""
@@ -458,14 +463,9 @@ def get_connection_kwargs(self) -> Dict[str, Optional[Any]]:
458463
"""Get the kwargs passed to :class:`~redis.asyncio.connection.Connection`."""
459464
return self.connection_kwargs
460465

461-
def keyslot(self, key: EncodableT) -> int:
462-
"""
463-
Find the keyslot for a given key.
464-
465-
See: https://redis.io/docs/manual/scaling/#redis-cluster-data-sharding
466-
"""
467-
k = self.encoder.encode(key)
468-
return key_slot(k)
466+
def set_response_callback(self, command: str, callback: ResponseCallbackT) -> None:
467+
"""Set a custom response callback."""
468+
self.response_callbacks[command] = callback
469469

470470
async def _determine_nodes(
471471
self, command: str, *args: Any, node_flag: Optional[str] = None
@@ -776,7 +776,6 @@ def __init__(
776776
server_type: Optional[str] = None,
777777
max_connections: int = 2**31,
778778
connection_class: Type[Connection] = Connection,
779-
response_callbacks: Dict[str, Any] = RedisCluster.RESPONSE_CALLBACKS,
780779
**connection_kwargs: Any,
781780
) -> None:
782781
if host == "localhost":
@@ -792,7 +791,9 @@ def __init__(
792791
self.max_connections = max_connections
793792
self.connection_class = connection_class
794793
self.connection_kwargs = connection_kwargs
795-
self.response_callbacks = response_callbacks
794+
self.response_callbacks = connection_kwargs.pop(
795+
"response_callbacks", RedisCluster.RESPONSE_CALLBACKS
796+
)
796797

797798
self._connections: List[Connection] = []
798799
self._free: Deque[Connection] = collections.deque(maxlen=self.max_connections)

0 commit comments

Comments
 (0)