diff options
author | Utkarsh Gupta <utkarshgupta137@gmail.com> | 2022-06-02 19:48:16 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-02 17:18:16 +0300 |
commit | 535a612d6365f64220ec80384dcecd4a8f0095e3 (patch) | |
tree | d2ab070ac1957d4250f3300a2ff7ebec0e2cb36a | |
parent | a2365d1582dc59d28c262cd410c0b126a4e5adc6 (diff) | |
download | redis-py-535a612d6365f64220ec80384dcecd4a8f0095e3.tar.gz |
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
-rw-r--r-- | docs/connections.rst | 7 | ||||
-rw-r--r-- | redis/asyncio/cluster.py | 73 |
2 files changed, 42 insertions, 38 deletions
diff --git a/docs/connections.rst b/docs/connections.rst index b481689..1c826a0 100644 --- a/docs/connections.rst +++ b/docs/connections.rst @@ -70,16 +70,19 @@ RedisCluster (Async) ==================== .. autoclass:: redis.asyncio.cluster.RedisCluster :members: + :member-order: bysource ClusterNode (Async) =================== .. autoclass:: redis.asyncio.cluster.ClusterNode :members: + :member-order: bysource ClusterPipeline (Async) -=================== +======================= .. autoclass:: redis.asyncio.cluster.ClusterPipeline - :members: + :members: execute_command, execute + :member-order: bysource Connection diff --git a/redis/asyncio/cluster.py b/redis/asyncio/cluster.py index b53f848..89789b7 100644 --- a/redis/asyncio/cluster.py +++ b/redis/asyncio/cluster.py @@ -379,14 +379,9 @@ class RedisCluster(AbstractRedis, AbstractRedisCluster, AsyncRedisClusterCommand if str_if_bytes(await connection.read_response_without_lock()) != "OK": raise ConnectionError("READONLY command failed") - def get_node( - self, - host: Optional[str] = None, - port: Optional[int] = None, - node_name: Optional[str] = None, - ) -> Optional["ClusterNode"]: - """Get node by (host, port) or node_name.""" - return self.nodes_manager.get_node(host, port, node_name) + def get_nodes(self) -> List["ClusterNode"]: + """Get all nodes of the cluster.""" + return list(self.nodes_manager.nodes_cache.values()) def get_primaries(self) -> List["ClusterNode"]: """Get the primary nodes of the cluster.""" @@ -400,9 +395,29 @@ class RedisCluster(AbstractRedis, AbstractRedisCluster, AsyncRedisClusterCommand """Get a random node of the cluster.""" return random.choice(list(self.nodes_manager.nodes_cache.values())) - def get_nodes(self) -> List["ClusterNode"]: - """Get all nodes of the cluster.""" - return list(self.nodes_manager.nodes_cache.values()) + def get_default_node(self) -> "ClusterNode": + """Get the default node of the client.""" + return self.nodes_manager.default_node + + def set_default_node(self, node: "ClusterNode") -> None: + """ + Set the default node of the client. + + :raises DataError: if None is passed or node does not exist in cluster. + """ + if not node or not self.get_node(node_name=node.name): + raise DataError("The requested node does not exist in the cluster.") + + self.nodes_manager.default_node = node + + def get_node( + self, + host: Optional[str] = None, + port: Optional[int] = None, + node_name: Optional[str] = None, + ) -> Optional["ClusterNode"]: + """Get node by (host, port) or node_name.""" + return self.nodes_manager.get_node(host, port, node_name) def get_node_from_key( self, key: str, replica: bool = False @@ -413,6 +428,7 @@ class RedisCluster(AbstractRedis, AbstractRedisCluster, AsyncRedisClusterCommand :param key: :param replica: | Indicates if a replica should be returned + | None will returned if no replica holds this key :raises SlotNotCoveredError: if the key is not covered by any slot. @@ -431,24 +447,13 @@ class RedisCluster(AbstractRedis, AbstractRedisCluster, AsyncRedisClusterCommand return slot_cache[node_idx] - def get_default_node(self) -> "ClusterNode": - """Get the default node of the client.""" - return self.nodes_manager.default_node - - def set_default_node(self, node: "ClusterNode") -> None: + def keyslot(self, key: EncodableT) -> int: """ - Set the default node of the client. + Find the keyslot for a given key. - :raises DataError: if None is passed or node does not exist in cluster. + See: https://redis.io/docs/manual/scaling/#redis-cluster-data-sharding """ - if not node or not self.get_node(node_name=node.name): - raise DataError("The requested node does not exist in the cluster.") - - self.nodes_manager.default_node = node - - def set_response_callback(self, command: str, callback: ResponseCallbackT) -> None: - """Set a custom response callback.""" - self.response_callbacks[command] = callback + return key_slot(self.encoder.encode(key)) def get_encoder(self) -> Encoder: """Get the encoder object of the client.""" @@ -458,14 +463,9 @@ class RedisCluster(AbstractRedis, AbstractRedisCluster, AsyncRedisClusterCommand """Get the kwargs passed to :class:`~redis.asyncio.connection.Connection`.""" return self.connection_kwargs - def keyslot(self, key: EncodableT) -> int: - """ - Find the keyslot for a given key. - - See: https://redis.io/docs/manual/scaling/#redis-cluster-data-sharding - """ - k = self.encoder.encode(key) - return key_slot(k) + def set_response_callback(self, command: str, callback: ResponseCallbackT) -> None: + """Set a custom response callback.""" + self.response_callbacks[command] = callback async def _determine_nodes( self, command: str, *args: Any, node_flag: Optional[str] = None @@ -776,7 +776,6 @@ class ClusterNode: server_type: Optional[str] = None, max_connections: int = 2**31, connection_class: Type[Connection] = Connection, - response_callbacks: Dict[str, Any] = RedisCluster.RESPONSE_CALLBACKS, **connection_kwargs: Any, ) -> None: if host == "localhost": @@ -792,7 +791,9 @@ class ClusterNode: self.max_connections = max_connections self.connection_class = connection_class self.connection_kwargs = connection_kwargs - self.response_callbacks = response_callbacks + self.response_callbacks = connection_kwargs.pop( + "response_callbacks", RedisCluster.RESPONSE_CALLBACKS + ) self._connections: List[Connection] = [] self._free: Deque[Connection] = collections.deque(maxlen=self.max_connections) |