Rate limits counted across the control planes with router.active_active
CI / test (push) Successful in 9m30s

Each router's per-counter request and token counts, this minute's and the
last, ride the standby's two-second exchange with the leader. The leader
answers each standby with everyone else's sum, its own included, and puts
the standbys' sum in force on its own router; each limiter adds the
others' counts to its sliding window.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-11-01 17:30:08 +01:00
1 parent 95fb5eeb6e
commit 540b518fde
17 files changed
+494 -28

No files matched your search

+51
View File
@@ -3,7 +3,9 @@ package control
import (
"log/slog"
"testing"
"time"
"gitea.mynodes.xyz/ilker/llmgw/internal/accounting"
"gitea.mynodes.xyz/ilker/llmgw/internal/control/catalog"
)
@@ -90,3 +92,52 @@ func TestTheCatalogRevisionMovesWithTheCatalog(t *testing.T) {
t.Errorf("the revision stayed %q", after)
}
}
// rateSink is a router whose counts can be shared: its own, and what it was
// last told the others counted.
type rateSink struct {
recordSink
own accounting.Rates
others *accounting.Rates
}
func (s *rateSink) Rates() accounting.Rates { return s.own }
func (s *rateSink) SetOtherRates(r accounting.Rates) {
s.others = &r
}
func counts(k string, n int64) accounting.Rates {
r := accounting.NewRates(time.Now())
r.Requests.Current = map[string]int64{k: n}
return r
}
// Each standby is given what the leader and the other standbys counted, never
// its own; the leader puts the standbys' counts in force on its own router.
func TestLimitsAreCountedAcrossTheControlPlanes(t *testing.T) {
sink := &rateSink{own: counts("key-1", 5)}
cat, _ := catalog.New(nil)
c := New(DefaultConfig(), catalog.NewLive(cat), noDispatch{}, sink, slog.New(slog.DiscardHandler))
b, d := counts("key-1", 3), counts("key-1", 7)
c.RecordActivity(Activity{From: "ctl-b", Rates: &b})
c.RecordActivity(Activity{From: "ctl-d", Rates: &d})
if got := c.RoutingFor("ctl-b").Rates.Requests.Current["key-1"]; got != 12 {
t.Errorf("ctl-b was told %d, want the leader's 5 and ctl-d's 7", got)
}
c.shareRates()
if sink.others == nil || sink.others.Requests.Current["key-1"] != 10 {
t.Errorf("the leader's router was told %+v, want the standbys' 10", sink.others)
}
// A standby that stopped reporting stops counting.
c.rates.mu.Lock()
r := c.rates.by["ctl-d"]
r.at = r.at.Add(-ratesFresh - time.Second)
c.rates.by["ctl-d"] = r
c.rates.mu.Unlock()
if got := c.RoutingFor("ctl-b").Rates.Requests.Current["key-1"]; got != 5 {
t.Errorf("a quiet standby still counts: %d", got)
}
}