From beaefa3cf6fbcc46664c4da91accf9816c39183b Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Thu, 26 Nov 2026 07:27:08 +0000 Subject: [PATCH] perf(shared-state): cache primitive key lists The breaker key list depends only on its ids, so the constructor builds it once. The in-flight key list depends on ids and the epoch, so it rebuilds only when the epoch changes. Under Yaegi, keys() drops from about 0.5 ms to 0.013 ms per refresh. Callers must not modify the returned slices. --- shared_state.go | 28 +++++++++++++++-------- shared_state_keys_test.go | 48 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 9 deletions(-) create mode 100644 shared_state_keys_test.go diff --git a/shared_state.go b/shared_state.go index baabda4..a4d9de2 100644 --- a/shared_state.go +++ b/shared_state.go @@ -376,6 +376,7 @@ type sharedBreaker struct { failAccAt map[string]time.Time kind string ids []string + keyList []string openBase time.Duration openMax time.Duration failWindow time.Duration @@ -390,6 +391,10 @@ func newSharedBreaker(loop *sharedStateLoop, kind string, ids []string, threshol 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), failAccAt: make(map[string]time.Time), } + b.keyList = make([]string, 0, sharedBreakerKeysPerID*len(ids)) + for _, id := range ids { + b.keyList = append(b.keyList, sharedBreakerKey(kind, id, "open"), sharedBreakerKey(kind, id, "fails"), sharedBreakerKey(kind, id, "opens")) + } if loop != nil { b.nowFn = loop.nowFn loop.register(b) @@ -398,13 +403,9 @@ func newSharedBreaker(loop *sharedStateLoop, kind string, ids []string, threshol return b } -// keys lists, per id, the open-until, fail-count and open-count keys. +// keys lists, per id, the open-until, fail-count and open-count keys. ids never change, so the shared list is built once; callers must not modify it. func (b *sharedBreaker) keys(_ time.Time) []string { - out := make([]string, 0, sharedBreakerKeysPerID*len(b.ids)) - for _, id := range b.ids { - out = append(out, sharedBreakerKey(b.kind, id, "open"), sharedBreakerKey(b.kind, id, "fails"), sharedBreakerKey(b.kind, id, "opens")) - } - return out + return b.keyList } func (b *sharedBreaker) apply(vals []int64) { @@ -650,8 +651,11 @@ type sharedInflight struct { limits map[string]int acc map[string]*infDelta ids []string - epochs int - mu sync.Mutex + // keyList is keys() for keyEpoch; keyEpoch is only valid once keyList is non-nil. + keyList []string + epochs int + keyEpoch int64 + mu sync.Mutex } // newSharedInflight returns the tracker over ids; loop may be nil for a no-op tracker. @@ -681,9 +685,14 @@ func (s *sharedInflight) epochKey(provider string, epoch int64, kind string) str return sharedInflightKey(provider, strconv.FormatInt(epoch, 10)+":"+kind) } -// keys lists, per provider, the started and finished keys of the last M epochs, then the learned limit. +// keys lists, per provider, the started and finished keys of the last M epochs, then the learned limit. The list is cached per epoch; callers must not modify it. func (s *sharedInflight) keys(now time.Time) []string { e := s.epochOf(now) + s.mu.Lock() + defer s.mu.Unlock() + if s.keyList != nil && s.keyEpoch == e { + return s.keyList + } out := make([]string, 0, len(s.ids)*(2*s.epochs+1)) for _, id := range s.ids { for i := 0; i < s.epochs; i++ { @@ -691,6 +700,7 @@ func (s *sharedInflight) keys(now time.Time) []string { } out = append(out, sharedInflightKey(id, "limit")) } + s.keyList, s.keyEpoch = out, e return out } diff --git a/shared_state_keys_test.go b/shared_state_keys_test.go new file mode 100644 index 0000000..e5b6f0c --- /dev/null +++ b/shared_state_keys_test.go @@ -0,0 +1,48 @@ +package traefikllmgateway + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func freshBreakerKeys(kind string, ids []string) []string { + var out []string + for _, id := range ids { + out = append(out, sharedBreakerKey(kind, id, "open"), sharedBreakerKey(kind, id, "fails"), sharedBreakerKey(kind, id, "opens")) + } + return out +} + +func freshInflightKeys(s *sharedInflight, now time.Time) []string { + e := s.epochOf(now) + var out []string + for _, id := range s.ids { + for i := 0; i < s.epochs; i++ { + out = append(out, s.epochKey(id, e-int64(i), "s"), s.epochKey(id, e-int64(i), "f")) + } + out = append(out, sharedInflightKey(id, "limit")) + } + return out +} + +func TestSharedBreakerKeys_MatchFreshBuild(t *testing.T) { + ids := []string{"a", "b", "c"} + b := newSharedBreaker(nil, sharedKindRequest, ids, 3, time.Second, time.Minute, time.Minute) + want := freshBreakerKeys(sharedKindRequest, ids) + assert.Equal(t, want, b.keys(time.Unix(0, 0))) + assert.Equal(t, want, b.keys(time.Unix(1e9, 0))) + assert.Equal(t, want, b.keys(time.Unix(0, 0))) +} + +func TestSharedInflightKeys_MatchFreshBuild(t *testing.T) { + s := newSharedInflight(nil, []string{"a", "b"}, 15*time.Minute) + t0 := time.Unix(1_700_000_000, 0) + sameEpoch := t0.Add(10 * time.Second) + assert.Equal(t, s.epochOf(t0), s.epochOf(sameEpoch)) + next := t0.Add(sharedInflightEpoch) + for _, now := range []time.Time{t0, sameEpoch, t0, next, next, t0, next.Add(time.Hour)} { + assert.Equal(t, freshInflightKeys(s, now), s.keys(now)) + } +}