fix(shared-state): rate-limit the loop's store error logs

A Redis outage logged one line per refresh and per failed write. The
loop now logs at most one store error per 30 seconds, the interval the
limiter already uses.
This commit is contained in:
2026-11-26 04:11:17 +00:00
parent a9468f5d51
commit 383d477778
2 changed files with 70 additions and 16 deletions
+34 -16
View File
@@ -16,6 +16,8 @@ const (
sharedStateMaxFailures = 3 sharedStateMaxFailures = 3
// sharedStateChunk bounds the keys of one getMulti. // sharedStateChunk bounds the keys of one getMulti.
sharedStateChunk = 500 sharedStateChunk = 500
// sharedStateErrorLogEvery bounds how often the loop logs a store error, as the limiter does.
sharedStateErrorLogEvery = storeErrorLogEvery
// Breaker kinds, the {kind} part of a breaker key. // Breaker kinds, the {kind} part of a breaker key.
sharedKindRequest = "req" sharedKindRequest = "req"
@@ -55,18 +57,20 @@ type sharedStateLoop struct {
nowFn func() time.Time nowFn func() time.Time
logf func(string, ...any) logf func(string, ...any)
// lossy runs best-effort writes and may drop them under pressure. // lossy runs best-effort writes and may drop them under pressure.
lossy func(func()) lossy func(func())
lastUse time.Time lastUse time.Time
stop chan struct{} // lastErrLog is guarded by mu.
done chan struct{} lastErrLog time.Time
prims []sharedPrimitive stop chan struct{}
flushers []sharedFlusher done chan struct{}
every time.Duration prims []sharedPrimitive
failures int flushers []sharedFlusher
mu sync.Mutex every time.Duration
flushMu sync.Mutex failures int
running bool mu sync.Mutex
closed bool flushMu sync.Mutex
running bool
closed bool
} }
// newSharedStateLoop returns a loop over store; lossy is the droppable write spawn (the limiter's). // newSharedStateLoop returns a loop over store; lossy is the droppable write spawn (the limiter's).
@@ -95,6 +99,20 @@ func (l *sharedStateLoop) log(format string, args ...any) {
l.logf(format, args...) l.logf(format, args...)
} }
// logErr is log limited to one line per sharedStateErrorLogEvery, so a Redis outage cannot flood the log.
func (l *sharedStateLoop) logErr(format string, args ...any) {
now := l.nowFn()
l.mu.Lock()
ok := now.Sub(l.lastErrLog) >= sharedStateErrorLogEvery
if ok {
l.lastErrLog = now
}
l.mu.Unlock()
if ok {
l.log(format, args...)
}
}
func (l *sharedStateLoop) registerFlusher(f sharedFlusher) { func (l *sharedStateLoop) registerFlusher(f sharedFlusher) {
l.mu.Lock() l.mu.Lock()
l.flushers = append(l.flushers, f) l.flushers = append(l.flushers, f)
@@ -113,7 +131,7 @@ func (l *sharedStateLoop) flush() {
now := l.nowFn() now := l.nowFn()
for _, f := range fl { for _, f := range fl {
if err := f.flush(l, now); err != nil { if err := f.flush(l, now); err != nil {
l.log("shared state flush failed: %v", err) l.logErr("shared state flush failed: %v", err)
} }
} }
} }
@@ -266,7 +284,7 @@ func (l *sharedStateLoop) refreshFailed(prims []sharedPrimitive, err error) {
absent := l.failures >= sharedStateMaxFailures absent := l.failures >= sharedStateMaxFailures
n := l.failures n := l.failures
l.mu.Unlock() l.mu.Unlock()
l.log("shared state refresh failed (%d consecutive): %v", n, err) l.logErr("shared state refresh failed (%d consecutive): %v", n, err)
if absent { if absent {
var none []int64 var none []int64
for _, p := range prims { for _, p := range prims {
@@ -278,7 +296,7 @@ func (l *sharedStateLoop) refreshFailed(prims []sharedPrimitive, err error) {
func (l *sharedStateLoop) incrAndGet(e counterIncr) (int64, bool) { func (l *sharedStateLoop) incrAndGet(e counterIncr) (int64, bool) {
got, _, err := l.store.incrAndGetMulti([]counterIncr{e}, nil) got, _, err := l.store.incrAndGetMulti([]counterIncr{e}, nil)
if err != nil || len(got) != 1 { if err != nil || len(got) != 1 {
l.log("shared state write %q failed: %v", e.key, err) l.logErr("shared state write %q failed: %v", e.key, err)
return 0, false return 0, false
} }
return got[0], true return got[0], true
@@ -286,7 +304,7 @@ func (l *sharedStateLoop) incrAndGet(e counterIncr) (int64, bool) {
func (l *sharedStateLoop) write(entries []counterIncr) { func (l *sharedStateLoop) write(entries []counterIncr) {
if _, err := l.store.incrMulti(entries); err != nil { if _, err := l.store.incrMulti(entries); err != nil {
l.log("shared state write of %d keys failed: %v", len(entries), err) l.logErr("shared state write of %d keys failed: %v", len(entries), err)
} }
} }
+36
View File
@@ -324,3 +324,39 @@ func TestSharedStateIdleStopsDuringOutageAfterFailWindow(t *testing.T) {
waitLoop(t, l, false) waitLoop(t, l, false)
l.close() l.close()
} }
func TestSharedStateErrorLogsAreRateLimited(t *testing.T) {
store, clk := sharedTestEnv()
var mu sync.Mutex
var lines []string
l := newSharedStateLoop(store, nil, func(f string, a ...any) {
mu.Lock()
lines = append(lines, f)
mu.Unlock()
})
l.nowFn = clk.now
l.closed = true
l.lossy = func(f func()) { f() }
b := testBreaker(l)
store.setFail(errors.New("redis down"))
store.setFailIncr(errors.New("redis down"))
count := func() int {
mu.Lock()
defer mu.Unlock()
return len(lines)
}
for i := 0; i < 50; i++ {
b.recordFailure("p")
l.refresh()
l.write([]counterIncr{{key: "k", delta: 1, ttl: time.Minute}})
}
if got := count(); got != 1 {
t.Fatalf("%d error lines inside one interval, want 1", got)
}
clk.advance(sharedStateErrorLogEvery + time.Second)
b.recordFailure("p")
l.refresh()
if got := count(); got != 2 {
t.Errorf("%d error lines after the interval, want 2", got)
}
}