fix(douyu): 修复斗鱼链路 P0/P1 隐患并增强 P2 运维队列可观测性

This commit is contained in:
yml
2026-11-29 14:10:24 +08:00
parent be7146a9ef
commit d12d9e02b3
51 changed files with 2510 additions and 561 deletions
+18
View File
@@ -246,12 +246,30 @@ class UTCDateTime(TypeDecorator):
def get_db():
"""FastAPI 依赖:提供数据库会话,请求结束自动关闭。"""
db = SessionLocal()
db.info["managed_request_session"] = True
try:
yield db
finally:
db.close()
def release_idle_session(db) -> bool:
"""在执行阻塞 HTTP / sleep 前释放干净托管会话的事务与连接池连接。
仅对通过 ``get_db()`` 或 worker 托管标记创建的会话生效,且会话内不能有
未提交的新增/修改/删除对象,避免误伤单元测试中的未提交夹具或脏写状态。
"""
info = getattr(db, "info", None)
if not isinstance(info, dict) or not (
info.get("managed_request_session") or info.get("managed_worker_session")
):
return False
if getattr(db, "new", None) or getattr(db, "dirty", None) or getattr(db, "deleted", None):
return False
db.rollback()
return True
def init_db():
"""执行数据库迁移 + 写入初始数据。"""
run_migrations()