Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

feat: redis connection support max connections (#32935)

wangxiaoleib584434e2862
diff --git a/api/.env.example b/api/.env.example
index 38a096da0a7..ab8b6c5287f 100644
--- a/api/.env.example
+++ b/api/.env.example
@@ -42,6 +42,8 @@ REFRESH_TOKEN_EXPIRE_DAYS=30
# redis configuration
REDIS_HOST=localhost
REDIS_PORT=6379
+# Optional: limit total connections in connection pool (unset for default)
+# REDIS_MAX_CONNECTIONS=200
REDIS_USERNAME=
REDIS_PASSWORD=difyai123456
REDIS_USE_SSL=false
diff --git a/api/configs/middleware/cache/redis_config.py b/api/configs/middleware/cache/redis_config.py
index 4705b28c69d..367cb527311 100644
--- a/api/configs/middleware/cache/redis_config.py
+++ b/api/configs/middleware/cache/redis_config.py
@@ -111,3 +111,8 @@ class RedisConfig(BaseSettings):
description="Enable client side cache in redis",
default=False,
)
+
+ REDIS_MAX_CONNECTIONS: PositiveInt | None = Field(
+ description="Maximum connections in the Redis connection pool (unset for library default)",
+ default=None,
+ )
diff --git a/api/extensions/ext_redis.py b/api/extensions/ext_redis.py
index 658e6a0738a..cadd9cb2638 100644
--- a/api/extensions/ext_redis.py
+++ b/api/extensions/ext_redis.py
@@ -181,13 +181,18 @@ def _create_sentinel_client(redis_params: dict[str, Any]) -> Union[redis.Redis,
sentinel_hosts = [(node.split(":")[0], int(node.split(":")[1])) for node in dify_config.REDIS_SENTINELS.split(",")]
+ sentinel_kwargs = {
+ "socket_timeout": dify_config.REDIS_SENTINEL_SOCKET_TIMEOUT,
+ "username": dify_config.REDIS_SENTINEL_USERNAME,
+ "password": dify_config.REDIS_SENTINEL_PASSWORD,
+ }
+
+ if dify_config.REDIS_MAX_CONNECTIONS:
+ sentinel_kwargs["max_connections"] = dify_config.REDIS_MAX_CONNECTIONS
+
sentinel = Sentinel(
sentinel_hosts,
- sentinel_kwargs={
- "socket_timeout": dify_config.REDIS_SENTINEL_SOCKET_TIMEOUT,
- "username": dify_config.REDIS_SENTINEL_USERNAME,
- "password": dify_config.REDIS_SENTINEL_PASSWORD,
- },
+ sentinel_kwargs=sentinel_kwargs,
)
master: redis.Redis = sentinel.master_for(dify_config.REDIS_SENTINEL_SERVICE_NAME, **redis_params)
@@ -204,12 +209,15 @@ def _create_cluster_client() -> Union[redis.Redis, RedisCluster]:
for node in dify_config.REDIS_CLUSTERS.split(",")
]
- cluster: RedisCluster = RedisCluster(
- startup_nodes=nodes,
- password=dify_config.REDIS_CLUSTERS_PASSWORD,
- protocol=dify_config.REDIS_SERIALIZATION_PROTOCOL,
- cache_config=_get_cache_configuration(),
- )
+ cluster_kwargs: dict[str, Any] = {
+ "startup_nodes": nodes,
+ "password": dify_config.REDIS_CLUSTERS_PASSWORD,
+ "protocol": dify_config.REDIS_SERIALIZATION_PROTOCOL,
+ "cache_config": _get_cache_configuration(),
+ }
+ if dify_config.REDIS_MAX_CONNECTIONS:
+ cluster_kwargs["max_connections"] = dify_config.REDIS_MAX_CONNECTIONS
+ cluster: RedisCluster = RedisCluster(**cluster_kwargs)
return cluster
@@ -225,6 +233,9 @@ def _create_standalone_client(redis_params: dict[str, Any]) -> Union[redis.Redis
}
)
+ if dify_config.REDIS_MAX_CONNECTIONS:
+ redis_params["max_connections"] = dify_config.REDIS_MAX_CONNECTIONS
+
if ssl_kwargs:
redis_params.update(ssl_kwargs)
@@ -234,9 +245,17 @@ def _create_standalone_client(redis_params: dict[str, Any]) -> Union[redis.Redis
def _create_pubsub_client(pubsub_url: str, use_clusters: bool) -> redis.Redis | RedisCluster:
+ max_conns = dify_config.REDIS_MAX_CONNECTIONS
if use_clusters:
- return RedisCluster.from_url(pubsub_url)
- return redis.Redis.from_url(pubsub_url)
+ if max_conns:
+ return RedisCluster.from_url(pubsub_url, max_connections=max_conns)
+ else:
+ return RedisCluster.from_url(pubsub_url)
+
+ if max_conns:
+ return redis.Redis.from_url(pubsub_url, max_connections=max_conns)
+ else:
+ return redis.Redis.from_url(pubsub_url)
def init_app(app: DifyApp):
diff --git a/docker/.env.example b/docker/.env.example
index 3d0009711d3..399242cea32 100644
--- a/docker/.env.example
+++ b/docker/.env.example
@@ -349,6 +349,9 @@ REDIS_SSL_CERTFILE=
REDIS_SSL_KEYFILE=
# Path to client private key file for SSL authentication
REDIS_DB=0
+# Optional: limit total Redis connections used by API/Worker (unset for default)
+# Align with API's REDIS_MAX_CONNECTIONS in configs
+REDIS_MAX_CONNECTIONS=
# Whether to use Redis Sentinel mode.
# If set to true, the application will automatically discover and connect to the master node through Sentinel.
diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml
index 62421d7ec47..8ab3af9788d 100644
--- a/docker/docker-compose.yaml
+++ b/docker/docker-compose.yaml
@@ -90,6 +90,7 @@ x-shared-env: &shared-api-worker-env
REDIS_SSL_CERTFILE: ${REDIS_SSL_CERTFILE:-}
REDIS_SSL_KEYFILE: ${REDIS_SSL_KEYFILE:-}
REDIS_DB: ${REDIS_DB:-0}
+ REDIS_MAX_CONNECTIONS: ${REDIS_MAX_CONNECTIONS:-}
REDIS_USE_SENTINEL: ${REDIS_USE_SENTINEL:-false}
REDIS_SENTINELS: ${REDIS_SENTINELS:-}
REDIS_SENTINEL_SERVICE_NAME: ${REDIS_SENTINEL_SERVICE_NAME:-}
diff --git a/docker/middleware.env.example b/docker/middleware.env.example
index c88dbe5511f..7b28a77fe3e 100644
--- a/docker/middleware.env.example
+++ b/docker/middleware.env.example
@@ -91,6 +91,9 @@ MYSQL_INNODB_FLUSH_LOG_AT_TRX_COMMIT=2
# -----------------------------
REDIS_HOST_VOLUME=./volumes/redis/data
REDIS_PASSWORD=difyai123456
+# Optional: limit total Redis connections used by API/Worker (unset for default)
+# Align with API's REDIS_MAX_CONNECTIONS in configs
+REDIS_MAX_CONNECTIONS=
# ------------------------------
# Environment Variables for sandbox Service