diff --git a/backend/config/__init__.py b/backend/config/__init__.py index ed32dde1b..b929dd9b5 100644 --- a/backend/config/__init__.py +++ b/backend/config/__init__.py @@ -72,6 +72,8 @@ DB_PASSWD: Final[str | None] = _get_env("DB_PASSWD") DB_NAME: Final[str] = _get_env("DB_NAME", "romm") DB_QUERY_JSON: Final[str | None] = _get_env("DB_QUERY_JSON") ROMM_DB_DRIVER: Final[str] = _get_env("ROMM_DB_DRIVER", "mariadb") +# Kept under the idle `wait_timeout` a host may impose; -1 never recycles. +DB_POOL_RECYCLE_SECONDS: Final[int] = safe_int(_get_env("DB_POOL_RECYCLE_SECONDS"), 300) # REDIS REDIS_HOST: Final[str | None] = _get_env("REDIS_HOST") diff --git a/backend/handler/database/base_handler.py b/backend/handler/database/base_handler.py index 8e079dff8..0fdc95cd4 100644 --- a/backend/handler/database/base_handler.py +++ b/backend/handler/database/base_handler.py @@ -4,11 +4,16 @@ import time from sqlalchemy import create_engine, event from sqlalchemy.orm import sessionmaker -from config import DEV_SQL_ECHO +from config import DB_POOL_RECYCLE_SECONDS, DEV_SQL_ECHO from config.config_manager import ConfigManager +# A ping on a socket the server already closed blocks the worker until TCP gives +# up, so `pool_pre_ping` alone is not enough. sync_engine = create_engine( - ConfigManager.get_db_engine(), pool_pre_ping=True, echo=False + ConfigManager.get_db_engine(), + pool_pre_ping=True, + pool_recycle=DB_POOL_RECYCLE_SECONDS, + echo=False, ) sync_session = sessionmaker(bind=sync_engine, expire_on_commit=False) diff --git a/docs/BACKEND_ARCHITECTURE.md b/docs/BACKEND_ARCHITECTURE.md index 93f5e6830..70eb042fb 100644 --- a/docs/BACKEND_ARCHITECTURE.md +++ b/docs/BACKEND_ARCHITECTURE.md @@ -1545,14 +1545,16 @@ Falls back to `FakeRedis` in test mode. #### Database -| Variable | Default | Description | -| ---------------- | --------- | ----------------------------------- | -| `ROMM_DB_DRIVER` | `mariadb` | `mariadb`, `mysql`, or `postgresql` | -| `DB_HOST` | | Database host | -| `DB_PORT` | `3306` | Database port | -| `DB_USER` | | Database user | -| `DB_PASSWD` | | Database password | -| `DB_NAME` | `romm` | Database name | +| Variable | Default | Description | +| ------------------------- | --------- | ------------------------------------------------------------------ | +| `ROMM_DB_DRIVER` | `mariadb` | `mariadb`, `mysql`, or `postgresql` | +| `DB_HOST` | | Database host | +| `DB_PORT` | `3306` | Database port | +| `DB_USER` | | Database user | +| `DB_PASSWD` | | Database password | +| `DB_NAME` | `romm` | Database name | +| `DB_QUERY_JSON` | | Extra connection parameters, as JSON | +| `DB_POOL_RECYCLE_SECONDS` | `300` | Retire a pooled connection after this long (`-1` to never recycle) | #### Redis diff --git a/env.template b/env.template index 2eca619de..0c2ec104e 100644 --- a/env.template +++ b/env.template @@ -14,6 +14,7 @@ DB_USER= # Database username (should match MARIADB_USER in MariaDB) [REQUIRED] DB_PASSWD= # Database password (should match MARIADB_PASSWORD in MariaDB) [REQUIRED] DB_ROOT_PASSWD= # Database root user password (only used by the bundled MariaDB container) DB_QUERY_JSON= # Extra query parameters for the database connection, as JSON +DB_POOL_RECYCLE_SECONDS=300 # Retire a pooled connection after this long, before the server drops it for being idle (-1 to never recycle) # Redis/Valkey REDIS_HOST=127.0.0.1 # Host name of the Redis/Valkey instance