diff --git a/app/infrastructure/repositories.py b/app/infrastructure/repositories.py index 8e91897..dfd6be8 100644 --- a/app/infrastructure/repositories.py +++ b/app/infrastructure/repositories.py @@ -3353,6 +3353,11 @@ _SHARED_ENGINES: dict[str, "object"] = {} def _shared_engine(url: str): + # 统一引擎:主库 URL 直接复用 db 模块级单例 engine(同一进程同一 URL 只有一个 + # 连接池,避免多 engine 多池叠加导致连接数倍增 / 池不互济再满载)。 + if url == config.DATABASE_URL: + from .db import engine as _main_engine + return _main_engine eng = _SHARED_ENGINES.get(url) if eng is None: from .db import make_async_engine diff --git a/app/park/tenants.py b/app/park/tenants.py index 22b96b7..fd812ec 100644 --- a/app/park/tenants.py +++ b/app/park/tenants.py @@ -41,7 +41,9 @@ _ASYNC_ENGINE = None def _get_session() -> AsyncSession: global _ASYNC_ENGINE if _ASYNC_ENGINE is None: - _ASYNC_ENGINE = make_async_engine(DATABASE_URL) + # 统一引擎:复用 db 模块级单例 engine(同一 URL 同一池,不另开池) + from ..infrastructure.db import engine as _main_engine + _ASYNC_ENGINE = _main_engine return async_sessionmaker(bind=_ASYNC_ENGINE, expire_on_commit=False)()