| new file mode 100644 |
| index 000000000..a710f1717 |
| --- /dev/null |
| +++ b/internal/edge/burst_test.go |
| @@ -0,0 +1,389 @@ |
| +package edge |
| + |
| +import ( |
| + "net/netip" |
| + "sync" |
| + "testing" |
| + "time" |
| +) |
| + |
| +// burstClock is a hand-driven clock so the window and cool-down are exact. |
| +type burstClock struct { |
| + mu sync.Mutex |
| + t time.Time |
| +} |
| + |
| +func newBurstClock() *burstClock { |
| + return &burstClock{t: time.Date(2026, 10, 31, 12, 0, 0, 0, time.UTC)} |
| +} |
| + |
| +func (c *burstClock) now() time.Time { |
| + c.mu.Lock() |
| + defer c.mu.Unlock() |
| + return c.t |
| +} |
| + |
| +func (c *burstClock) advance(d time.Duration) { |
| + c.mu.Lock() |
| + c.t = c.t.Add(d) |
| + c.mu.Unlock() |
| +} |
| + |
| +func burstTestLimiter() (*burstLimiter, *burstClock) { |
| + c := newBurstClock() |
| + return newBurstLimiter(c.now), c |
| +} |
| + |
| +var ( |
| + burstClientA = netip.MustParsePrefix("203.0.113.7/32") |
| + burstClientB = netip.MustParsePrefix("2001:db8:1:2::/64") |
| +) |
| + |
| +// lowerBurstCaps shrinks the bounds for one test and restores them after. |
| +func lowerBurstCaps(t *testing.T, counters, cooldowns int) { |
| + t.Helper() |
| + oc, od := burstMaxCounters, burstMaxCooldowns |
| + burstMaxCounters, burstMaxCooldowns = counters, cooldowns |
| + t.Cleanup(func() { burstMaxCounters, burstMaxCooldowns = oc, od }) |
| +} |
| + |
| +func TestBurstTripsAt60InAWindow(t *testing.T) { |
| + b, c := burstTestLimiter() |
| + for i := 1; i <= 59; i++ { |
| + if tripped, _ := b.note("bab/shop", burstClientA); tripped { |
| + t.Fatalf("note %d tripped; want the 60th", i) |
| + } |
| + c.advance(500 * time.Millisecond) |
| + } |
| + tripped, until := b.note("bab/shop", burstClientA) |
| + if !tripped { |
| + t.Fatal("60th note did not trip") |
| + } |
| + if want := c.now().Add(burstCooldown); !until.Equal(want) { |
| + t.Fatalf("until = %v, want %v", until, want) |
| + } |
| + if tripped, _ := b.note("bab/shop", burstClientA); tripped { |
| + t.Fatal("61st note tripped a second time") |
| + } |
| +} |
| + |
| +func TestBurstWindowSlides(t *testing.T) { |
| + b, c := burstTestLimiter() |
| + // 60 notes whose first and last are 61 s apart: never 60 inside 60 s. |
| + step := 61 * time.Second / 59 |
| + for i := 0; i < 60; i++ { |
| + if tripped, _ := b.note("bab/shop", burstClientA); tripped { |
| + t.Fatalf("note %d tripped", i+1) |
| + } |
| + c.advance(step) |
| + } |
| + // And a steady pace slower than 60 per 60 s never trips, however long it |
| + // lasts: 60 notes at this spacing span 59 x 1.02 s, more than the window. |
| + b, c = burstTestLimiter() |
| + for i := 0; i < 600; i++ { |
| + if tripped, _ := b.note("bab/shop", burstClientA); tripped { |
| + t.Fatalf("note %d tripped at a sustained slow pace", i+1) |
| + } |
| + c.advance(1020 * time.Millisecond) |
| + } |
| +} |
| + |
| +func TestBurstCountsPerApp(t *testing.T) { |
| + b, _ := burstTestLimiter() |
| + for i := 0; i < 30; i++ { |
| + if tripped, _ := b.note("bab/one", burstClientA); tripped { |
| + t.Fatal("app one tripped") |
| + } |
| + if tripped, _ := b.note("bdk/two", burstClientA); tripped { |
| + t.Fatal("app two tripped") |
| + } |
| + } |
| + if _, _, ok := b.cooling(burstClientA); ok { |
| + t.Fatal("30 + 30 across two apps put the client in a cool-down") |
| + } |
| +} |
| + |
| +func TestBurstCountsPerClient(t *testing.T) { |
| + b, _ := burstTestLimiter() |
| + for i := 0; i < 59; i++ { |
| + b.note("bab/shop", burstClientA) |
| + b.note("bab/shop", burstClientB) |
| + } |
| + if _, _, ok := b.cooling(burstClientA); ok { |
| + t.Fatal("client A cooling after 59") |
| + } |
| + if tripped, _ := b.note("bab/shop", burstClientA); !tripped { |
| + t.Fatal("client A did not trip on its 60th") |
| + } |
| + if _, _, ok := b.cooling(burstClientB); ok { |
| + t.Fatal("client B cooling because client A tripped") |
| + } |
| +} |
| + |
| +func TestBurstCooldownIsBoxWide(t *testing.T) { |
| + b, _ := burstTestLimiter() |
| + for i := 0; i < 60; i++ { |
| + b.note("bab/shop", burstClientA) |
| + } |
| + until, from, ok := b.cooling(burstClientA) |
| + if !ok || from != "bab/shop" || until.IsZero() { |
| + t.Fatalf("cooling = (%v, %q, %v), want a cool-down from bab/shop", until, from, ok) |
| + } |
| + // The cool-down belongs to the client, not the app it tripped on. |
| + // Notes on another app during the cool-down are no-ops and leave it be. |
| + if tripped, _ := b.note("bdk/other", burstClientA); tripped { |
| + t.Fatal("a note on another app tripped again") |
| + } |
| + if _, f, ok := b.cooling(burstClientA); !ok || f != "bab/shop" { |
| + t.Fatalf("cooling after another app's note: from %q ok %v", f, ok) |
| + } |
| + if _, _, ok := b.cooling(burstClientB); ok { |
| + t.Fatal("another client is cooling") |
| + } |
| +} |
| + |
| +func TestBurstTripClearsTheClientsCounters(t *testing.T) { |
| + b, c := burstTestLimiter() |
| + for i := 0; i < 20; i++ { |
| + b.note("bdk/other", burstClientA) |
| + } |
| + for i := 0; i < 60; i++ { |
| + b.note("bab/shop", burstClientA) |
| + } |
| + if n := b.counterCount(); n != 0 { |
| + t.Fatalf("counters after a trip = %d, want 0", n) |
| + } |
| + // After the cool-down the client starts from nothing. |
| + c.advance(burstCooldown + time.Second) |
| + for i := 0; i < 59; i++ { |
| + if tripped, _ := b.note("bab/shop", burstClientA); tripped { |
| + t.Fatalf("note %d tripped after the cool-down", i+1) |
| + } |
| + } |
| +} |
| + |
| +func TestBurstCooldownExpires(t *testing.T) { |
| + b, c := burstTestLimiter() |
| + for i := 0; i < 60; i++ { |
| + b.note("bab/shop", burstClientA) |
| + } |
| + c.advance(burstCooldown - time.Second) |
| + if _, _, ok := b.cooling(burstClientA); !ok { |
| + t.Fatal("cool-down ended early") |
| + } |
| + c.advance(time.Second) |
| + if _, _, ok := b.cooling(burstClientA); ok { |
| + t.Fatal("cool-down did not end at 10 minutes") |
| + } |
| + if n := b.cooldownCount(); n != 0 { |
| + t.Fatalf("expired cool-down still held: %d", n) |
| + } |
| +} |
| + |
| +func TestBurstApplyBounds(t *testing.T) { |
| + b, c := burstTestLimiter() |
| + now := c.now() |
| + if b.apply(burstClientA, now) { |
| + t.Fatal("until == now accepted") |
| + } |
| + if b.apply(burstClientA, now.Add(-time.Second)) { |
| + t.Fatal("until in the past accepted") |
| + } |
| + if b.apply(burstClientA, now.Add(burstCooldown+time.Second)) { |
| + t.Fatal("until 10 min + 1 s ahead accepted") |
| + } |
| + if _, _, ok := b.cooling(burstClientA); ok { |
| + t.Fatal("a refused notice started a cool-down") |
| + } |
| + if !b.apply(burstClientA, now.Add(burstCooldown)) { |
| + t.Fatal("until exactly 10 min ahead refused") |
| + } |
| + until, from, ok := b.cooling(burstClientA) |
| + if !ok || from != "" || !until.Equal(now.Add(burstCooldown)) { |
| + t.Fatalf("cooling = (%v, %q, %v)", until, from, ok) |
| + } |
| + |
| + // A shorter until never shortens; a longer one extends. |
| + b.apply(burstClientA, now.Add(time.Minute)) |
| + if u, _, _ := b.cooling(burstClientA); !u.Equal(now.Add(burstCooldown)) { |
| + t.Fatalf("a shorter notice moved until to %v", u) |
| + } |
| + c.advance(5 * time.Minute) |
| + b.apply(burstClientA, c.now().Add(burstCooldown)) |
| + if u, _, _ := b.cooling(burstClientA); !u.Equal(c.now().Add(burstCooldown)) { |
| + t.Fatalf("a later notice did not extend: %v", u) |
| + } |
| +} |
| + |
| +func TestBurstApplyNeverReportsATripAndNoteIsNoOpWhileCooling(t *testing.T) { |
| + b, _ := burstTestLimiter() |
| + b.apply(burstClientA, b.now().Add(time.Minute)) |
| + for i := 0; i < 200; i++ { |
| + if tripped, _ := b.note("bab/shop", burstClientA); tripped { |
| + t.Fatal("note tripped for a client already cooling from a notice") |
| + } |
| + } |
| + if n := b.counterCount(); n != 0 { |
| + t.Fatalf("notes during a cool-down left %d counters", n) |
| + } |
| + if _, from, _ := b.cooling(burstClientA); from != "" { |
| + t.Fatalf("from = %q, want the notice's empty source", from) |
| + } |
| +} |
| + |
| +func TestBurstBoundsEvictOldest(t *testing.T) { |
| + lowerBurstCaps(t, 3, 2) |
| + b, _ := burstTestLimiter() |
| + clients := []netip.Prefix{ |
| + netip.MustParsePrefix("198.51.100.1/32"), |
| + netip.MustParsePrefix("198.51.100.2/32"), |
| + netip.MustParsePrefix("198.51.100.3/32"), |
| + netip.MustParsePrefix("198.51.100.4/32"), |
| + } |
| + for _, cl := range clients { |
| + b.note("bab/shop", cl) |
| + } |
| + if n := b.counterCount(); n != 3 { |
| + t.Fatalf("counters = %d, want the cap of 3", n) |
| + } |
| + if got := b.stats().CountersEvicted; got != 1 { |
| + t.Fatalf("CountersEvicted = %d, want 1", got) |
| + } |
| + // The oldest (client 1) went: 59 more notes from it do not trip, |
| + // because its first note is gone and it starts again from one. |
| + for i := 0; i < 59; i++ { |
| + if tripped, _ := b.note("bab/shop", clients[0]); tripped { |
| + t.Fatalf("evicted client tripped at %d", i) |
| + } |
| + } |
| + |
| + // Cool-downs: the third evicts the first. |
| + b, c := burstTestLimiter() |
| + until := c.now().Add(time.Minute) |
| + for _, cl := range clients[:3] { |
| + if !b.apply(cl, until) { |
| + t.Fatal("apply refused") |
| + } |
| + } |
| + if n := b.cooldownCount(); n != 2 { |
| + t.Fatalf("cool-downs = %d, want the cap of 2", n) |
| + } |
| + if got := b.stats().CooldownsEvicted; got != 1 { |
| + t.Fatalf("CooldownsEvicted = %d, want 1", got) |
| + } |
| + if _, _, ok := b.cooling(clients[0]); ok { |
| + t.Fatal("the oldest cool-down survived") |
| + } |
| + for _, cl := range clients[1:3] { |
| + if _, _, ok := b.cooling(cl); !ok { |
| + t.Fatalf("%v lost its cool-down", cl) |
| + } |
| + } |
| +} |
| + |
| +func TestBurstExpiredEntriesAreDroppedBeforeEvicting(t *testing.T) { |
| + lowerBurstCaps(t, 2, 2) |
| + b, c := burstTestLimiter() |
| + b.note("bab/shop", burstClientA) |
| + b.apply(burstClientA, c.now().Add(time.Minute)) |
| + c.advance(burstCooldown) |
| + // Both held entries are long expired: a new one needs no eviction. |
| + other := netip.MustParsePrefix("198.51.100.9/32") |
| + b.note("bab/shop", other) |
| + b.note("bab/shop", burstClientB) |
| + b.apply(other, c.now().Add(time.Minute)) |
| + b.apply(burstClientB, c.now().Add(time.Minute)) |
| + if s := b.stats(); s.CountersEvicted != 0 || s.CooldownsEvicted != 0 { |
| + t.Fatalf("expired entries were counted as evictions: %+v", s) |
| + } |
| +} |
| + |
| +func TestBurstSingleKeyFloodBounded(t *testing.T) { |
| + b, c := burstTestLimiter() |
| + // A flood that never lets 60 land inside a window: one note per 2 s. |
| + allocs := testing.AllocsPerRun(1, func() { |
| + for i := 0; i < 1_000_000; i++ { |
| + b.note("bab/shop", burstClientB) |
| + c.advance(2 * time.Second) |
| + } |
| + }) |
| + if n := b.counterCount(); n != 1 { |
| + t.Fatalf("counters = %d, want 1", n) |
| + } |
| + // AllocsPerRun runs the function once to warm up and once measured; the |
| + // ring is made in the first, so the measured pass allocates nothing per |
| + // note. Allow a handful for the runtime. |
| + if allocs > 10 { |
| + t.Fatalf("1,000,000 notes allocated %v times", allocs) |
| + } |
| + if s := b.stats(); s.CountersEvicted != 0 { |
| + t.Fatalf("evictions = %d", s.CountersEvicted) |
| + } |
| + |
| + // A flood inside a cool-down is a no-op: no counters, no allocation. |
| + b2, _ := burstTestLimiter() |
| + b2.apply(burstClientA, b2.now().Add(time.Minute)) |
| + allocs = testing.AllocsPerRun(1, func() { |
| + for i := 0; i < 1_000_000; i++ { |
| + b2.note("bab/shop", burstClientA) |
| + } |
| + }) |
| + if allocs > 10 || b2.counterCount() != 0 { |
| + t.Fatalf("notes during a cool-down: %v allocations, %d counters", allocs, b2.counterCount()) |
| + } |
| +} |
| + |
| +func TestBurstRingHoldsAtMost60Timestamps(t *testing.T) { |
| + b, c := burstTestLimiter() |
| + for i := 0; i < 5000; i++ { |
| + b.note("bab/shop", burstClientA) |
| + c.advance(2 * time.Second) |
| + } |
| + if got := b.ringLen("bab/shop", burstClientA); got != burstThreshold { |
| + t.Fatalf("ring holds %d timestamps, want %d", got, burstThreshold) |
| + } |
| +} |
| + |
| +func TestBurstConcurrent(t *testing.T) { |
| + b := newBurstLimiter(time.Now) |
| + var wg sync.WaitGroup |
| + for g := 0; g < 8; g++ { |
| + wg.Add(1) |
| + go func(g int) { |
| + defer wg.Done() |
| + cl := netip.PrefixFrom(netip.AddrFrom4([4]byte{10, 0, 0, byte(g)}), 32) |
| + for i := 0; i < 2000; i++ { |
| + b.note("bab/shop", cl) |
| + b.cooling(cl) |
| + if i%500 == 0 { |
| + b.apply(cl, time.Now().Add(time.Minute)) |
| + b.stats() |
| + } |
| + } |
| + }(g) |
| + } |
| + wg.Wait() |
| +} |
| + |
| +// The tests look inside under the lock; the serving path never needs these. |
| + |
| +func (b *burstLimiter) counterCount() int { |
| + b.mu.Lock() |
| + defer b.mu.Unlock() |
| + return b.counterLRU.Len() |
| +} |
| + |
| +func (b *burstLimiter) cooldownCount() int { |
| + b.mu.Lock() |
| + defer b.mu.Unlock() |
| + return b.cooldownList.Len() |
| +} |
| + |
| +func (b *burstLimiter) ringLen(app string, client netip.Prefix) int { |
| + b.mu.Lock() |
| + defer b.mu.Unlock() |
| + if el, ok := b.counters[client][app]; ok { |
| + return len(el.Value.(*burstCounter).ts) |
| + } |
| + return 0 |
| +} |