From a9468f5d512ba9bdc01733b57d7ea3f574ce772a Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Thu, 26 Nov 2026 04:10:15 +0000 Subject: [PATCH] fix(shared-state): drop breaker deltas older than the fail window During a Redis outage the queued failure deltas kept a replaced gateway's loop alive for ever. A queued failure older than the fail window cannot change the fleet count, so the breaker now drops it. --- shared_state.go | 24 ++++++++++++++++++++++-- shared_state_review_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/shared_state.go b/shared_state.go index fa72ae9..4e99638 100644 --- a/shared_state.go +++ b/shared_state.go @@ -334,6 +334,7 @@ type sharedBreaker struct { opensSeen map[string]int64 failsSeen map[string]int64 failAcc map[string]int64 + failAccAt map[string]time.Time kind string ids []string openBase time.Duration @@ -348,7 +349,7 @@ func newSharedBreaker(loop *sharedStateLoop, kind string, ids []string, threshol b := &sharedBreaker{ loop: loop, nowFn: time.Now, kind: kind, ids: ids, threshold: threshold, openBase: openBase, openMax: openMax, failWindow: failWindow, - local: make(map[string]*breakerLocal), openUntil: make(map[string]int64), opensSeen: make(map[string]int64), failsSeen: make(map[string]int64), failAcc: make(map[string]int64), + local: make(map[string]*breakerLocal), openUntil: make(map[string]int64), opensSeen: make(map[string]int64), failsSeen: make(map[string]int64), failAcc: make(map[string]int64), failAccAt: make(map[string]time.Time), } if loop != nil { b.nowFn = loop.nowFn @@ -425,6 +426,9 @@ func (b *sharedBreaker) recordFailure(id string) { b.mu.Lock() b.recordFailureLocal(b.localFor(id), b.nowFn()) if b.loop != nil { + if b.failAcc[id] == 0 { + b.failAccAt[id] = b.nowFn() + } b.failAcc[id]++ } b.mu.Unlock() @@ -459,13 +463,26 @@ func (b *sharedBreaker) recordFailureLocal(st *breakerLocal, now time.Time) { func (b *sharedBreaker) pending() bool { b.mu.Lock() defer b.mu.Unlock() + b.pruneStaleLocked(b.nowFn()) return len(b.failAcc) > 0 } +// pruneStaleLocked drops queued failures older than the fail window: the fleet count they would join has expired, +// and keeping them would stop an idle loop during a Redis outage. +func (b *sharedBreaker) pruneStaleLocked(now time.Time) { + for id, at := range b.failAccAt { + if now.Sub(at) > b.failWindow { + delete(b.failAcc, id) + delete(b.failAccAt, id) + } + } +} + // flush adds the queued failures to the fleet counts. An instance whose flush leaves the count at or above the // threshold opens the breaker unless its last refresh already shows it open. -func (b *sharedBreaker) flush(l *sharedStateLoop, _ time.Time) error { +func (b *sharedBreaker) flush(l *sharedStateLoop, now time.Time) error { b.mu.Lock() + b.pruneStaleLocked(now) ids := make([]string, 0, len(b.failAcc)) deltas := make([]int64, 0, len(b.failAcc)) for id, n := range b.failAcc { @@ -491,8 +508,10 @@ func (b *sharedBreaker) flush(l *sharedStateLoop, _ time.Time) error { for i, id := range ids { if left := b.failAcc[id] - deltas[i]; left > 0 { b.failAcc[id] = left + b.failAccAt[id] = now } else { delete(b.failAcc, id) + delete(b.failAccAt, id) } } b.mu.Unlock() @@ -545,6 +564,7 @@ func (b *sharedBreaker) recordSuccess(id string) { wasOpen := b.openUntil[id] > b.nowFn().UnixMilli() opensSeen := b.opensSeen[id] > 0 delete(b.failAcc, id) + delete(b.failAccAt, id) st.open = false st.consecutiveFailures = 0 st.backoff = 0 diff --git a/shared_state_review_test.go b/shared_state_review_test.go index 91129c9..b710f44 100644 --- a/shared_state_review_test.go +++ b/shared_state_review_test.go @@ -294,3 +294,33 @@ func TestSharedInflightEndRestartsIdleStoppedLoop(t *testing.T) { } l.close() } + +func TestSharedBreakerDropsDeltasPastFailWindow(t *testing.T) { + store, clk := sharedTestEnv() + l := sharedTestLoop(store, clk) + b := testBreaker(l) + store.setFailIncr(errors.New("redis down")) + b.recordFailure("p") + l.refresh() + if !b.pending() { + t.Fatal("delta dropped inside the fail window") + } + clk.advance(testBrkWindow + time.Second) + if b.pending() { + t.Error("failure delta older than the fail window still pending") + } +} + +func TestSharedStateIdleStopsDuringOutageAfterFailWindow(t *testing.T) { + store, clk := sharedTestEnv() + l := newSharedStateLoop(store, nil, nil) + l.nowFn = clk.now + l.every = 5 * time.Millisecond + b := testBreaker(l) + store.setFailIncr(errors.New("redis down")) + b.recordFailure("p") + waitLoop(t, l, true) + clk.advance(sharedStateIdleStop + time.Second) + waitLoop(t, l, false) + l.close() +}