feat: add AIMD adaptive concurrency pacing per source

P6 round 2 showed routing convergence turns account-level 429s into
the binding constraint (62% throttle rate at full concurrency). Each
source now carries a local AIMD limit: multiplicative cut on 429,
additive growth on success. Over-limit picks queue via the existing
quota-wait poll instead of burning retry budget or tripping the
circuit-open verdict.
This commit is contained in:
2026-07-21 10:38:48 -04:00
parent 910857fd13
commit 58061c7536
6 changed files with 157 additions and 2 deletions
+39
View File
@@ -82,6 +82,45 @@ class HealthAwareSelector:
return [head] + [s for s in ranked if s.name != head.name]
class AdaptivePacer:
"""AIMD 自适应并发(M2.5 设计 §3.35;Netflix concurrency-limits 损失型)。
429 是网关的"降速"信号: 乘性削减该源并发上限(×0.7),真实成功加性
增长(+1/limit),上限收敛到网关可持续水位;超限调用在 RetryMW 的
quota-wait 轮询里排队而非烧重试预算。进程本地,属 client 实例。
"""
_INITIAL = 8.0
_CUT = 0.7
_FLOOR = 1.0
def __init__(self, *, ceiling: float) -> None:
if ceiling < self._FLOOR:
raise ValueError("ceiling 不得小于下限 1")
self._ceiling = ceiling
self._limit: dict[str, float] = {}
self._inflight: dict[str, int] = {}
def limit(self, source_name: str) -> float:
return self._limit.get(source_name, min(self._INITIAL, self._ceiling))
def on_backpressure(self, source_name: str) -> None:
self._limit[source_name] = max(self._FLOOR, self.limit(source_name) * self._CUT)
def on_success(self, source_name: str) -> None:
cur = self.limit(source_name)
self._limit[source_name] = min(self._ceiling, cur + 1.0 / cur)
def admit(self, source_name: str) -> bool:
return self._inflight.get(source_name, 0) < self.limit(source_name)
def enter(self, source_name: str) -> None:
self._inflight[source_name] = self._inflight.get(source_name, 0) + 1
def leave(self, source_name: str) -> None:
self._inflight[source_name] = max(0, self._inflight.get(source_name, 0) - 1)
class SourceCooldownMemo:
"""进程本地的源冷却备忘(CHS governance.py:107 同款)。