fix(dispatcher): 熔断器接回 failover —— 挂掉的主模型跳过而非每次白试(T4.E 收官)

- failoverModel 加每模型熔断器(阈值3/冷却20s,比编排层更紧):
  主模型持续失败达阈值 → 熔断 → 后续请求直接跳过主、直连备用(省掉每次白试主的失败往返);
  冷却到点半开放行探测打回主,成功即自动恢复走主(靠熔断器半开机制,无需外部通知)
- 全部模型都熔断时强制试主兜底(编排层 o.breaker 兜"全挂")
- WithTools 重包共享同一批 breakers(状态不清零)——否则每次 rewrap 熔断失效,关键坑
- harness 加 NewCircuitBreakerWith(threshold,cooldown,halfOpenMax) 参数化构造
- Generate/Stream 用泛型 runFailover 共用选路循环(去重)
- 3 新单测:熔断跳过主/WithTools 共享熔断状态/冷却后半开恢复(全三态)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Blizzard
2026-07-02 10:49:54 +08:00
parent 65e939889e
commit 7c211719d2
4 changed files with 173 additions and 44 deletions
@@ -50,11 +50,26 @@ type CircuitBreaker struct {
}
func NewCircuitBreaker() *CircuitBreaker {
return NewCircuitBreakerWith(defaultThreshold, defaultCooldown, defaultHalfOpenMax)
}
// NewCircuitBreakerWith 用自定义参数建熔断器(非法入参回退默认)。
// 供模型层 failover 用更紧的阈值/冷却(比编排层更快跳过挂掉的主模型);也便于测试注短冷却。
func NewCircuitBreakerWith(threshold int, cooldown time.Duration, halfOpenMax int) *CircuitBreaker {
if threshold <= 0 {
threshold = defaultThreshold
}
if cooldown <= 0 {
cooldown = defaultCooldown
}
if halfOpenMax <= 0 {
halfOpenMax = defaultHalfOpenMax
}
return &CircuitBreaker{
state: Closed,
threshold: defaultThreshold,
cooldown: defaultCooldown,
halfOpenMax: defaultHalfOpenMax,
threshold: threshold,
cooldown: cooldown,
halfOpenMax: halfOpenMax,
now: time.Now,
}
}