2
2
from version import __version__
3
3
import redis
4
4
from redis import Redis
5
- from typing import Optional
6
- from common .config import REDIS_CFG
5
+ from redis .cluster import RedisCluster
6
+ from typing import Optional , Union , List , Dict , Any , Tuple
7
+ from common .config import REDIS_CFG , REDIS_CLUSTER_MODE , REDIS_CLUSTER_NODES
7
8
8
9
from common .config import generate_redis_uri
9
10
10
11
11
12
class RedisConnectionManager :
12
- _instance : Optional [Redis ] = None
13
+ _instance : Optional [Union [ Redis , RedisCluster ] ] = None
13
14
14
15
@classmethod
15
- def get_connection (cls , decode_responses = True ) -> Redis :
16
+ def get_connection (cls , decode_responses = True ) -> Union [ Redis , RedisCluster ] :
16
17
if cls ._instance is None :
17
18
try :
18
- cls ._instance = redis .Redis (
19
- host = REDIS_CFG ["host" ],
20
- port = REDIS_CFG ["port" ],
21
- username = REDIS_CFG ["username" ],
22
- password = REDIS_CFG ["password" ],
23
- ssl = REDIS_CFG ["ssl" ],
24
- ssl_ca_path = REDIS_CFG ["ssl_ca_path" ],
25
- ssl_keyfile = REDIS_CFG ["ssl_keyfile" ],
26
- ssl_certfile = REDIS_CFG ["ssl_certfile" ],
27
- ssl_cert_reqs = REDIS_CFG ["ssl_cert_reqs" ],
28
- ssl_ca_certs = REDIS_CFG ["ssl_ca_certs" ],
29
- decode_responses = decode_responses ,
30
- max_connections = 10 ,
31
- lib_name = f"redis-py(mcp-server_v{ __version__ } )"
32
- )
19
+ if REDIS_CLUSTER_MODE :
20
+ # In cluster mode, we can connect to one node and the client will discover the rest
21
+ # If specific cluster nodes are provided, use the first one as the startup node
22
+ if REDIS_CLUSTER_NODES and REDIS_CLUSTER_NODES [0 ]:
23
+ node = REDIS_CLUSTER_NODES [0 ]
24
+ if ':' in node :
25
+ host , port = node .split (':' )
26
+ port = int (port )
27
+ else :
28
+ # Default to the configured port if only host is provided
29
+ host = node
30
+ port = REDIS_CFG ["port" ]
31
+ else :
32
+ # Use the primary node from REDIS_CFG as the startup node
33
+ host = REDIS_CFG ["host" ]
34
+ port = REDIS_CFG ["port" ]
35
+
36
+ cls ._instance = RedisCluster (
37
+ host = host ,
38
+ port = port ,
39
+ username = REDIS_CFG ["username" ],
40
+ password = REDIS_CFG ["password" ],
41
+ ssl = REDIS_CFG ["ssl" ],
42
+ ssl_ca_path = REDIS_CFG ["ssl_ca_path" ],
43
+ ssl_keyfile = REDIS_CFG ["ssl_keyfile" ],
44
+ ssl_certfile = REDIS_CFG ["ssl_certfile" ],
45
+ ssl_cert_reqs = REDIS_CFG ["ssl_cert_reqs" ],
46
+ ssl_ca_certs = REDIS_CFG ["ssl_ca_certs" ],
47
+ decode_responses = decode_responses ,
48
+ max_connections_per_node = 10 ,
49
+ lib_name = f"redis-py(mcp-server_v{ __version__ } )"
50
+ )
51
+ else :
52
+ cls ._instance = redis .Redis (
53
+ host = REDIS_CFG ["host" ],
54
+ port = REDIS_CFG ["port" ],
55
+ username = REDIS_CFG ["username" ],
56
+ password = REDIS_CFG ["password" ],
57
+ ssl = REDIS_CFG ["ssl" ],
58
+ ssl_ca_path = REDIS_CFG ["ssl_ca_path" ],
59
+ ssl_keyfile = REDIS_CFG ["ssl_keyfile" ],
60
+ ssl_certfile = REDIS_CFG ["ssl_certfile" ],
61
+ ssl_cert_reqs = REDIS_CFG ["ssl_cert_reqs" ],
62
+ ssl_ca_certs = REDIS_CFG ["ssl_ca_certs" ],
63
+ decode_responses = decode_responses ,
64
+ max_connections = 10 ,
65
+ lib_name = f"redis-py(mcp-server_v{ __version__ } )"
66
+ )
33
67
34
68
except redis .exceptions .ConnectionError :
35
69
print ("Failed to connect to Redis server" , file = sys .stderr )
@@ -46,6 +80,9 @@ def get_connection(cls, decode_responses=True) -> Redis:
46
80
except redis .exceptions .RedisError as e :
47
81
print (f"Redis error: { e } " , file = sys .stderr )
48
82
raise
83
+ except redis .exceptions .ClusterError as e :
84
+ print (f"Redis Cluster error: { e } " , file = sys .stderr )
85
+ raise
49
86
except Exception as e :
50
87
print (f"Unexpected error: { e } " , file = sys .stderr )
51
88
raise
0 commit comments