Skip to content

Commit 0db3650

Browse files
committed
Refactor Redis connection parameters to avoid duplication
1 parent e597f86 commit 0db3650

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

src/common/connection.py

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,36 @@ class RedisConnectionManager:
1616
def get_connection(cls, decode_responses=True) -> Redis:
1717
if cls._instance is None:
1818
try:
19+
# Common connection parameters
20+
common_params = {
21+
"host": REDIS_CFG["host"],
22+
"port": REDIS_CFG["port"],
23+
"username": REDIS_CFG["username"],
24+
"password": REDIS_CFG["password"],
25+
"ssl": REDIS_CFG["ssl"],
26+
"ssl_ca_path": REDIS_CFG["ssl_ca_path"],
27+
"ssl_keyfile": REDIS_CFG["ssl_keyfile"],
28+
"ssl_certfile": REDIS_CFG["ssl_certfile"],
29+
"ssl_cert_reqs": REDIS_CFG["ssl_cert_reqs"],
30+
"ssl_ca_certs": REDIS_CFG["ssl_ca_certs"],
31+
"decode_responses": decode_responses,
32+
"lib_name": f"redis-py(mcp-server_v{__version__})"
33+
}
34+
1935
if REDIS_CFG["cluster_mode"]:
20-
cls._instance = RedisCluster(
21-
host=REDIS_CFG["host"],
22-
port=REDIS_CFG["port"],
23-
username=REDIS_CFG["username"],
24-
password=REDIS_CFG["password"],
25-
ssl=REDIS_CFG["ssl"],
26-
ssl_ca_path=REDIS_CFG["ssl_ca_path"],
27-
ssl_keyfile=REDIS_CFG["ssl_keyfile"],
28-
ssl_certfile=REDIS_CFG["ssl_certfile"],
29-
ssl_cert_reqs=REDIS_CFG["ssl_cert_reqs"],
30-
ssl_ca_certs=REDIS_CFG["ssl_ca_certs"],
31-
decode_responses=decode_responses,
32-
max_connections_per_node=10,
33-
lib_name=f"redis-py(mcp-server_v{__version__})"
34-
)
36+
# Cluster-specific parameters
37+
cluster_params = {
38+
**common_params,
39+
"max_connections_per_node": 10
40+
}
41+
cls._instance = RedisCluster(**cluster_params)
3542
else:
36-
cls._instance = redis.Redis(
37-
host=REDIS_CFG["host"],
38-
port=REDIS_CFG["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=10,
49-
lib_name=f"redis-py(mcp-server_v{__version__})"
50-
)
43+
# Standalone-specific parameters
44+
standalone_params = {
45+
**common_params,
46+
"max_connections": 10
47+
}
48+
cls._instance = redis.Redis(**standalone_params)
5149

5250
except redis.exceptions.ConnectionError:
5351
print("Failed to connect to Redis server", file=sys.stderr)

0 commit comments

Comments
 (0)