From f865dc5e774cd61f38c3de89c11f903a235138e2 Mon Sep 17 00:00:00 2001 From: fylorn <249551762+fylorn@users.noreply.github.com> Date: Tue, 15 Sep 2026 17:50:36 +0800 Subject: [PATCH] fix: the breaker's recovery could go unannounced MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every failure past the threshold pushes the cooldown out to now — and while a breaker is open, failures keep arriving: with all candidates open the gateway fails open and tries them anyway. So the one-shot timer armed when the breaker tripped would wake up, see a cooldown that had been pushed past it, say nothing, and exit. Nothing was left to announce the moment the breaker actually became ready to probe. The visible result is worse than a missing event: the pushed view and the polled view disagree. `/overview` computes the state fresh, so it reports `ok` as soon as the cooldown elapses, while a client following the event stream still shows the provider as tripped — until some request happens to hit it. The timer now re-arms instead of giving up. Health::cooldown_left tells it which of the three cases it woke into: no longer tripped (a success already announced the recovery), cooldown elapsed (announce it), or pushed back (sleep the remainder and look again). Co-Authored-By: Claude Opus 5 --- crates/tw-gateway/src/health.rs | 39 +++++++++++++++++++++------------ crates/tw-gateway/src/server.rs | 18 ++++++++++++--- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/crates/tw-gateway/src/health.rs b/crates/tw-gateway/src/health.rs index 66529179..1611c8ca 100644 --- a/crates/tw-gateway/src/health.rs +++ b/crates/tw-gateway/src/health.rs @@ -101,16 +101,18 @@ impl Health { (before != after).then_some(after) } - /// 这家的冷却刚好走完了吗。 + /// 这家还要等多久才轮到探测。 /// - /// **专门给「报一条恢复」用的**,所以判据比 `state()` 严:要求那次 - /// 熔断**还在**(`opened_at` 没被成功清掉,也没有被一次新的失败 - /// 顶成更晚的时刻)。中途成功过的话 `record_success` 已经报过了, - /// 再报一条就是同一件事说两遍;重新熔断的话,那次自己带着定时器。 - pub fn just_cooled_down(&self, name: &str) -> bool { + /// `None` = 根本不在熔断态(成功过,或者从来没熔断过)。 + /// `Some(0)` = 冷却走完了,下一个请求就是那次探测。 + /// + /// **专门给「报一条恢复」的定时器用。**它必须能分清「到点了」和 + /// 「中途又失败、冷却被顶到更晚了」—— 后者返回的是新的剩余时间, + /// 定时器据此接着等,而不是现在就宣布恢复。 + pub fn cooldown_left(&self, name: &str) -> Option { let g = self.inner.lock().unwrap(); - g.get(name) - .is_some_and(|e| e.opened_at.is_some_and(|t| t.elapsed() >= COOLDOWN)) + let t = g.get(name)?.opened_at?; + Some(COOLDOWN.saturating_sub(t.elapsed())) } /// 从候选里挑出还能用的,**并说明是不是 fail-open**。 @@ -171,18 +173,27 @@ mod tests { assert_eq!(h.record_success("a"), None); } - /// 刚熔断的那一刻冷却还没走完 —— 这条守的是「别把恢复报两遍」: - /// 开的时候排下的那个定时器到点会拿它再确认一次。 + /// 这条守着「报恢复」那个定时器的两个判断。 #[test] - fn a_fresh_open_has_not_cooled_down_yet() { + fn the_cooldown_clock_says_which_case_it_is() { let h = Health::new(); + assert_eq!(h.cooldown_left("a"), None, "没熔断过,没有什么可等的"); for _ in 0..3 { h.record_failure("a"); } - assert!(!h.just_cooled_down("a")); - // 成功之后那次熔断就不存在了,定时器到点也不该再报一条 + let left = h.cooldown_left("a").expect("熔断了就该在等"); + assert!(!left.is_zero(), "刚熔断,冷却还没走完"); + + // **中途又失败会把冷却顶到更晚。**定时器到点时看到的是一段新的 + // 剩余时间,它该接着等 —— 现在宣布恢复的话,界面会说一家正在 + // 连续失败的上游已经好了。 + h.record_failure("a"); + assert!(!h.cooldown_left("a").expect("还在熔断").is_zero()); + + // 成功之后那次熔断就不存在了:`record_success` 已经报过恢复, + // 定时器到点看到 None,闭嘴走人。 h.record_success("a"); - assert!(!h.just_cooled_down("a")); + assert_eq!(h.cooldown_left("a"), None); } #[test] diff --git a/crates/tw-gateway/src/server.rs b/crates/tw-gateway/src/server.rs index 7ec6a262..a6a8b602 100644 --- a/crates/tw-gateway/src/server.rs +++ b/crates/tw-gateway/src/server.rs @@ -776,6 +776,11 @@ async fn passthrough( /// 开的时候顺手排一个定时器:**冷却到点本身就是一次状态变化**,而它 /// 不由任何调用触发。不报的话,界面会一直显示「熔断中」,直到碰巧又有 /// 一个请求打到这家为止。 +/// +/// 定时器会**自己续期**,因为熔断期间的每一次失败都会把冷却顶到更晚 +/// (全都熔断时我们是放行的,所以那些失败照样打得到这家)。只睡一次 +/// 就下结论的话,它醒来时看到的是一段还没走完的冷却 —— 然后闭嘴,而 +/// 真正到点的那一刻再也没有人报。 fn note_health( bus: &tw_observe::EventBus, health: &Arc, @@ -799,9 +804,16 @@ fn note_health( } let (bus, health, name) = (bus.clone(), health.clone(), provider.to_string()); tokio::spawn(async move { - tokio::time::sleep(crate::health::COOLDOWN).await; - if health.just_cooled_down(&name) { - say(&bus, name, false); + let mut left = crate::health::COOLDOWN; + loop { + tokio::time::sleep(left).await; + match health.cooldown_left(&name) { + // 中途成功过了 —— `record_success` 已经报过恢复 + None => return, + Some(d) if d.is_zero() => return say(&bus, name, false), + // 又失败了一次,冷却被顶后。接着等,别现在就说它好了 + Some(d) => left = d, + } } }); }