Skip to content

Commit

Permalink
Moved ClusterParser exceptions to BaseParser class (#3475)
Browse files Browse the repository at this point in the history
* Moved ClusterParser exceptions to BaseParser class

* Codestyle fixes

* Removed ubused imports

* Sorted imports
  • Loading branch information
vladvildanov authored Jan 15, 2025
1 parent 7a6b412 commit fab31b4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 45 deletions.
4 changes: 2 additions & 2 deletions docs/clustering.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Nodes <#specifying-target-nodes>`__ \| `Multi-key
Commands <#multi-key-commands>`__ \| `Known PubSub
Limitations <#known-pubsub-limitations>`__

Creating clusters
-----------------
Connecting to cluster
---------------------

Connecting redis-py to a Redis Cluster instance(s) requires at a minimum
a single node for cluster discovery. There are multiple ways in which a
Expand Down
12 changes: 12 additions & 0 deletions redis/_parsers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@
from async_timeout import timeout as async_timeout

from ..exceptions import (
AskError,
AuthenticationError,
AuthenticationWrongNumberOfArgsError,
BusyLoadingError,
ClusterCrossSlotError,
ClusterDownError,
ConnectionError,
ExecAbortError,
MasterDownError,
ModuleError,
MovedError,
NoPermissionError,
NoScriptError,
OutOfMemoryError,
ReadOnlyError,
RedisError,
ResponseError,
TryAgainError,
)
from ..typing import EncodableT
from .encoders import Encoder
Expand Down Expand Up @@ -72,6 +78,12 @@ class BaseParser(ABC):
"READONLY": ReadOnlyError,
"NOAUTH": AuthenticationError,
"NOPERM": NoPermissionError,
"ASK": AskError,
"TRYAGAIN": TryAgainError,
"MOVED": MovedError,
"CLUSTERDOWN": ClusterDownError,
"CROSSSLOT": ClusterCrossSlotError,
"MASTERDOWN": MasterDownError,
}

@classmethod
Expand Down
27 changes: 2 additions & 25 deletions redis/asyncio/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
_RedisCallbacksRESP3,
)
from redis.asyncio.client import ResponseCallbackT
from redis.asyncio.connection import Connection, DefaultParser, SSLConnection, parse_url
from redis.asyncio.connection import Connection, SSLConnection, parse_url
from redis.asyncio.lock import Lock
from redis.asyncio.retry import Retry
from redis.auth.token import TokenInterface
Expand All @@ -50,12 +50,10 @@
from redis.exceptions import (
AskError,
BusyLoadingError,
ClusterCrossSlotError,
ClusterDownError,
ClusterError,
ConnectionError,
DataError,
MasterDownError,
MaxConnectionsError,
MovedError,
RedisClusterException,
Expand All @@ -66,33 +64,13 @@
TryAgainError,
)
from redis.typing import AnyKeyT, EncodableT, KeyT
from redis.utils import (
deprecated_function,
dict_merge,
get_lib_version,
safe_str,
str_if_bytes,
)
from redis.utils import deprecated_function, get_lib_version, safe_str, str_if_bytes

TargetNodesT = TypeVar(
"TargetNodesT", str, "ClusterNode", List["ClusterNode"], Dict[Any, "ClusterNode"]
)


class ClusterParser(DefaultParser):
EXCEPTION_CLASSES = dict_merge(
DefaultParser.EXCEPTION_CLASSES,
{
"ASK": AskError,
"CLUSTERDOWN": ClusterDownError,
"CROSSSLOT": ClusterCrossSlotError,
"MASTERDOWN": MasterDownError,
"MOVED": MovedError,
"TRYAGAIN": TryAgainError,
},
)


class RedisCluster(AbstractRedis, AbstractRedisCluster, AsyncRedisClusterCommands):
"""
Create a new RedisCluster client.
Expand Down Expand Up @@ -297,7 +275,6 @@ def __init__(
kwargs: Dict[str, Any] = {
"max_connections": max_connections,
"connection_class": Connection,
"parser_class": ClusterParser,
# Client related kwargs
"credential_provider": credential_provider,
"username": username,
Expand Down
19 changes: 1 addition & 18 deletions redis/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from redis.client import CaseInsensitiveDict, PubSub, Redis
from redis.commands import READ_COMMANDS, RedisClusterCommands
from redis.commands.helpers import list_or_args
from redis.connection import ConnectionPool, DefaultParser, parse_url
from redis.connection import ConnectionPool, parse_url
from redis.crc import REDIS_CLUSTER_HASH_SLOTS, key_slot
from redis.event import (
AfterPooledConnectionsInstantiationEvent,
Expand All @@ -24,12 +24,10 @@
from redis.exceptions import (
AskError,
AuthenticationError,
ClusterCrossSlotError,
ClusterDownError,
ClusterError,
ConnectionError,
DataError,
MasterDownError,
MovedError,
RedisClusterException,
RedisError,
Expand Down Expand Up @@ -193,20 +191,6 @@ def cleanup_kwargs(**kwargs):
return connection_kwargs


class ClusterParser(DefaultParser):
EXCEPTION_CLASSES = dict_merge(
DefaultParser.EXCEPTION_CLASSES,
{
"ASK": AskError,
"TRYAGAIN": TryAgainError,
"MOVED": MovedError,
"CLUSTERDOWN": ClusterDownError,
"CROSSSLOT": ClusterCrossSlotError,
"MASTERDOWN": MasterDownError,
},
)


class AbstractRedisCluster:
RedisClusterRequestTTL = 16

Expand Down Expand Up @@ -692,7 +676,6 @@ def on_connect(self, connection):
Initialize the connection, authenticate and select a database and send
READONLY if it is set during object initialization.
"""
connection.set_parser(ClusterParser)
connection.on_connect()

if self.read_from_replicas:
Expand Down

0 comments on commit fab31b4

Please sign in to comment.