feat(gateway): worker 池主备分组派发,主全挂才用备机

- pool.py: WorkerState 增加 primary 标记;acquire_worker 分级派发——
  主组有在线只在主组内挑空闲(全忙排队,不退备组),主组全部下线才用备机
- config.py: 新增可选 primary_workers 配置(须为 workers 子集,自动过滤+告警);
  不配置/空列表 = 全部视为主 worker,兼容旧行为
- config.json(不入 git): 3 台 GPU worker 设为主,max_global_concurrency 1→3
- app.py: /health 增加 workers_primary_total / workers_primary_healthy
- tests: 新增 4 条主备派发单测(主优先/主全挂用备/主全忙不走备/无配置兼容)
- docs(实现说明): 补充 primary_workers 配置说明,改配置需重启网关生效
This commit is contained in:
xsl
2026-09-22 23:48:21 +08:00
parent 6b24bf0b0d
commit f1b8b7bd82
6 changed files with 125 additions and 8 deletions
+10
View File
@@ -15,6 +15,7 @@ CONFIG_PATH = Path(__file__).resolve().parent / "config.json"
DEFAULTS = {
"public_base_url": "https://hair.xiangsilian.com",
"static_dir": "static/annotations",
"primary_workers": [], # 主 worker 地址列表(须为 workers 的子集);空 = 全部视为主 worker
"health_check": {
"path": "/health",
"interval_seconds": 8,
@@ -83,6 +84,15 @@ def load_config() -> dict:
if not isinstance(w, str) or not w:
raise ValueError(f"配置错误: workers[{i}] 必须是非空字符串")
primary_workers: List[str] = cfg.get("primary_workers", [])
for i, w in enumerate(primary_workers):
if not isinstance(w, str) or not w:
raise ValueError(f"配置错误: primary_workers[{i}] 必须是非空字符串")
for w in primary_workers:
if w not in workers:
logger.warning("⚠ primary_workers 中的 %s 不在 workers 列表里,将被忽略", w)
cfg["primary_workers"] = [w for w in primary_workers if w in workers]
password: str = cfg.get("shared_password", "")
if not password or password == "REPLACE_ME_ROTATE_PERIODICALLY":
logger.warning(