From 5a0bf8e02892417f0c68e8feb734cda65d75e240 Mon Sep 17 00:00:00 2001 From: Saurabh Singh <56667478+saurabhy27@users.noreply.github.com> Date: Thu, 18 Sep 2025 13:43:56 +0530 Subject: [PATCH] feat: make SQLALCHEMY_POOL_TIMEOUT configurable (#25468) Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- api/.env.example | 1 + api/configs/middleware/__init__.py | 6 ++++++ api/tests/unit_tests/configs/test_dify_config.py | 1 + docker/.env.example | 3 +++ docker/docker-compose.yaml | 1 + 5 files changed, 12 insertions(+) diff --git a/api/.env.example b/api/.env.example index 7193a371fe..b89111a8e3 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 dbad90270e..62b3cc9842 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 0c7473019a..fbe14f1cb5 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 33a3c6275c..4575e11b99 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 12283e77da..1d412d714f 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}