mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-02 09:20:22 +08:00
- 删除全部 Python 源码 (src/) 及 Alembic 迁移脚本,归档至 _deprecated_py_src/ - 重构 Rust gateway ai_pipeline: 拆分 planner/finalize 模块,新增 contracts/adaptation 层 - 重组 handlers 模块为 admin/public/proxy/internal/shared 子模块结构 - 新增 executor 模块,引入 Rust 原生数据库迁移 (aether-data/migrations) - 简化 CI/Docker 构建流程,移除 base image 二级构建,统一为单一 app image - 移除 Python 相关基础设施文件 (entrypoint.sh, gunicorn_conf.py, Dockerfile.base)
54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
"""
|
|
调度器工具函数
|
|
|
|
从 CacheAwareScheduler 提取的静态工具方法。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
|
def affinity_hash(affinity_key: str, identifier: str) -> int:
|
|
"""基于 affinity_key 和标识符的确定性哈希(用于同优先级内分散负载均衡)"""
|
|
return int(hashlib.sha256(f"{affinity_key}:{identifier}".encode()).hexdigest()[:16], 16)
|
|
|
|
|
|
def release_db_connection_before_await(db: Session) -> None:
|
|
"""
|
|
Best-effort: end a read-only transaction before awaiting async I/O.
|
|
|
|
This scheduler does a lot of async work (cache/Redis) mixed with sync SQLAlchemy reads.
|
|
If a SELECT has already started a transaction, the pooled connection can remain checked
|
|
out while we await, causing pool pressure under concurrency.
|
|
|
|
Safety:
|
|
- Only commits when the Session has no ORM pending changes.
|
|
- Temporarily disables expire_on_commit to keep already-loaded ORM objects usable.
|
|
"""
|
|
try:
|
|
if db is None:
|
|
return
|
|
has_pending_changes = bool(db.new) or bool(db.dirty) or bool(db.deleted)
|
|
if has_pending_changes:
|
|
return
|
|
if not db.in_transaction():
|
|
return
|
|
|
|
original_expire_on_commit = getattr(db, "expire_on_commit", True)
|
|
db.expire_on_commit = False
|
|
try:
|
|
db.commit()
|
|
except Exception:
|
|
try:
|
|
db.rollback()
|
|
except Exception:
|
|
pass
|
|
finally:
|
|
db.expire_on_commit = original_expire_on_commit
|
|
except Exception:
|
|
# Never let this optimization break scheduling
|
|
return
|