fix(platform-fulfillment): 释放空闲连接并合并选号请求

This commit is contained in:
yml
2026-11-21 17:35:34 +08:00
parent f74573a9c0
commit 7f7943a1e8
17 changed files with 690 additions and 74 deletions
+44 -14
View File
@@ -7,6 +7,7 @@ from urllib.parse import quote_plus
from sqlalchemy import DateTime, create_engine, event
from sqlalchemy.orm import declarative_base, sessionmaker
from sqlalchemy.pool import NullPool
from sqlalchemy.types import TypeDecorator
PROJECT_ROOT = Path(__file__).resolve().parents[2]
@@ -21,6 +22,11 @@ def _env_int(name: str, default: int) -> int:
return value if value > 0 else default
def _pool_mode(name: str, default: str = "queue") -> str:
mode = os.getenv(name, default).strip().lower()
return mode if mode in {"queue", "null"} else default
def _get_database_url() -> str:
"""优先使用完整连接串,否则按 DB_* 环境变量构造 MySQL 连接。"""
database_url = os.getenv("DATABASE_URL", "").strip()
@@ -61,13 +67,16 @@ engine_options = {
"echo": False,
}
if not DATABASE_URL.startswith("sqlite"):
engine_options.update(
pool_pre_ping=True,
pool_size=_env_int("DB_POOL_SIZE", 20),
max_overflow=_env_int("DB_MAX_OVERFLOW", 20),
pool_timeout=_env_int("DB_POOL_TIMEOUT", 30),
pool_recycle=_env_int("DB_POOL_RECYCLE", 1800),
)
if _pool_mode("DB_POOL_MODE") == "null":
engine_options["poolclass"] = NullPool
else:
engine_options["pool_pre_ping"] = True
engine_options.update(
pool_size=_env_int("DB_POOL_SIZE", 20),
max_overflow=_env_int("DB_MAX_OVERFLOW", 20),
pool_timeout=_env_int("DB_POOL_TIMEOUT", 30),
pool_recycle=_env_int("DB_POOL_RECYCLE", 1800),
)
engine = create_engine(DATABASE_URL, **engine_options)
@@ -76,13 +85,22 @@ worker_engine_options = {
"echo": False,
}
if not DATABASE_URL.startswith("sqlite"):
worker_engine_options.update(
pool_pre_ping=True,
pool_size=_env_int("WORKER_DB_POOL_SIZE", 12),
max_overflow=_env_int("WORKER_DB_MAX_OVERFLOW", 4),
pool_timeout=_env_int("WORKER_DB_POOL_TIMEOUT", 30),
pool_recycle=_env_int("DB_POOL_RECYCLE", 1800),
)
if (
_pool_mode(
"WORKER_DB_POOL_MODE",
_pool_mode("DB_POOL_MODE"),
)
== "null"
):
worker_engine_options["poolclass"] = NullPool
else:
worker_engine_options["pool_pre_ping"] = True
worker_engine_options.update(
pool_size=_env_int("WORKER_DB_POOL_SIZE", 12),
max_overflow=_env_int("WORKER_DB_MAX_OVERFLOW", 4),
pool_timeout=_env_int("WORKER_DB_POOL_TIMEOUT", 30),
pool_recycle=_env_int("DB_POOL_RECYCLE", 1800),
)
worker_engine = create_engine(DATABASE_URL, **worker_engine_options)
@@ -140,6 +158,18 @@ WorkerSessionLocal = sessionmaker(
Base = declarative_base()
def dispose_engines() -> None:
"""关闭应用中所有持久连接。
`NullPool` 模式下通常没有空闲连接可关闭;保留这个统一出口是为了:
- queue 模式进程退出时主动释放 Sleep 连接;
- uvicorn reload / 测试重建应用时避免旧 engine 残留;
- 任务线程停止后再统一收口,顺序可预测。
"""
engine.dispose()
worker_engine.dispose()
class UTCDateTime(TypeDecorator):
"""时间列统一走这里:**库里存 naive UTC,Python 侧永远是 tz-aware UTC**。