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: make SQLALCHEMY_POOL_TIMEOUT configurable (#25468)

Saurabh Singh5a0bf8e02892
diff --git a/api/.env.example b/api/.env.example
index 7193a371fea..b89111a8e36 100644
--- a/api/.env.example
+++ b/api/.env.example
@@ -76,6 +76,7 @@ DB_HOST=localhost
DB_PORT=5432
DB_DATABASE=dify
SQLALCHEMY_POOL_PRE_PING=true
+SQLALCHEMY_POOL_TIMEOUT=30
# Storage configuration
# use for store upload files, private keys...
diff --git a/api/configs/middleware/__init__.py b/api/configs/middleware/__init__.py
index dbad90270eb..62b3cc9842c 100644
--- a/api/configs/middleware/__init__.py
+++ b/api/configs/middleware/__init__.py
@@ -187,6 +187,11 @@ class DatabaseConfig(BaseSettings):
default=False,
)
+ SQLALCHEMY_POOL_TIMEOUT: NonNegativeInt = Field(
+ description="Number of seconds to wait for a connection from the pool before raising a timeout error.",
+ default=30,
+ )
+
RETRIEVAL_SERVICE_EXECUTORS: NonNegativeInt = Field(
description="Number of processes for the retrieval service, default to CPU cores.",
default=os.cpu_count() or 1,
@@ -216,6 +221,7 @@ class DatabaseConfig(BaseSettings):
"connect_args": connect_args,
"pool_use_lifo": self.SQLALCHEMY_POOL_USE_LIFO,
"pool_reset_on_return": None,
+ "pool_timeout": self.SQLALCHEMY_POOL_TIMEOUT,
}
diff --git a/api/tests/unit_tests/configs/test_dify_config.py b/api/tests/unit_tests/configs/test_dify_config.py
index 0c7473019ae..fbe14f1cb54 100644
--- a/api/tests/unit_tests/configs/test_dify_config.py
+++ b/api/tests/unit_tests/configs/test_dify_config.py
@@ -91,6 +91,7 @@ def test_flask_configs(monkeypatch: pytest.MonkeyPatch):
"pool_size": 30,
"pool_use_lifo": False,
"pool_reset_on_return": None,
+ "pool_timeout": 30,
}
assert config["CONSOLE_WEB_URL"] == "https://example.com"
diff --git a/docker/.env.example b/docker/.env.example
index 33a3c6275c2..4575e11b99c 100644
--- a/docker/.env.example
+++ b/docker/.env.example
@@ -225,6 +225,9 @@ SQLALCHEMY_ECHO=false
SQLALCHEMY_POOL_PRE_PING=false
# Whether to enable the Last in first out option or use default FIFO queue if is false
SQLALCHEMY_POOL_USE_LIFO=false
+# Number of seconds to wait for a connection from the pool before raising a timeout error.
+# Default is 30
+SQLALCHEMY_POOL_TIMEOUT=30
# Maximum number of connections to the database
# Default is 100
diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml
index 12283e77da0..1d412d714ff 100644
--- a/docker/docker-compose.yaml
+++ b/docker/docker-compose.yaml
@@ -62,6 +62,7 @@ x-shared-env: &shared-api-worker-env
SQLALCHEMY_ECHO: ${SQLALCHEMY_ECHO:-false}
SQLALCHEMY_POOL_PRE_PING: ${SQLALCHEMY_POOL_PRE_PING:-false}
SQLALCHEMY_POOL_USE_LIFO: ${SQLALCHEMY_POOL_USE_LIFO:-false}
+ SQLALCHEMY_POOL_TIMEOUT: ${SQLALCHEMY_POOL_TIMEOUT:-30}
POSTGRES_MAX_CONNECTIONS: ${POSTGRES_MAX_CONNECTIONS:-100}
POSTGRES_SHARED_BUFFERS: ${POSTGRES_SHARED_BUFFERS:-128MB}
POSTGRES_WORK_MEM: ${POSTGRES_WORK_MEM:-4MB}