carlos / platform Public

edge: probe-burst limiter reads the clock under its lock

A call that read the time and then waited for the mutex could record an
earlier time after a later one, and the ring then tripped on 60 notes no
60 s window held. note and apply now sample time under the lock. Adds a
reordered-calls regression and makes the expired-counter test exercise
real expired counters.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev fdc0a6c7ddc861eb82dcf2823955c38b7ceeaffa parent e0b13a3
2 files changed, +95 −12
  • internal/edge/burst.go +7 −4
  • internal/edge/burst_test.go +88 −8
diff --git a/internal/edge/burst.go b/internal/edge/burst.go
index 644ce12f1..7786fc03b 100644
--- a/internal/edge/burst.go
+++ b/internal/edge/burst.go
@@ -93,10 +93,13 @@ func newBurstLimiter(now func() time.Time) *burstLimiter {
// note records one not-found for app and client and reports whether it was
// the one that tripped a cool-down. A client already cooling is not counted.
func (b *burstLimiter) note(app string, client netip.Prefix) (tripped bool, until time.Time) {
- now := b.now()
- n := now.UnixNano()
+ // The clock is read under the lock: a counter's ring assumes timestamps
+ // arrive in time order, and a call that read the time and then waited for
+ // the lock could otherwise record an earlier time after a later one.
b.mu.Lock()
defer b.mu.Unlock()
+ now := b.now()
+ n := now.UnixNano()
if _, ok := b.liveCooldown(client, now); ok {
return false, time.Time{}
}
@@ -137,12 +140,12 @@ func (b *burstLimiter) cooling(client netip.Prefix) (until time.Time, from strin
// whether until was within bounds, and never reports a trip: the edge that
// tripped has already told the others, and they must not tell it back.
func (b *burstLimiter) apply(client netip.Prefix, until time.Time) bool {
+ b.mu.Lock()
+ defer b.mu.Unlock()
now := b.now()
if !until.After(now) || until.After(now.Add(burstCooldown)) {
return false
}
- b.mu.Lock()
- defer b.mu.Unlock()
b.dropClient(client)
if cd, ok := b.liveCooldown(client, now); ok {
if until.After(cd.until) {
diff --git a/internal/edge/burst_test.go b/internal/edge/burst_test.go
index a710f1717..23b8c2aa0 100644
--- a/internal/edge/burst_test.go
+++ b/internal/edge/burst_test.go
@@ -3,6 +3,7 @@ package edge
import (
"net/netip"
"sync"
+ "sync/atomic"
"testing"
"time"
)
@@ -284,20 +285,99 @@ func TestBurstBoundsEvictOldest(t *testing.T) {
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))
+ // Fill both caps with entries that then expire. The counters and the
+ // cool-downs use different clients: apply drops a client's counters.
+ counterClients := []netip.Prefix{
+ netip.MustParsePrefix("198.51.100.1/32"),
+ netip.MustParsePrefix("198.51.100.2/32"),
+ }
+ coolClients := []netip.Prefix{
+ netip.MustParsePrefix("198.51.100.3/32"),
+ netip.MustParsePrefix("198.51.100.4/32"),
+ }
+ for _, cl := range counterClients {
+ b.note("bab/shop", cl)
+ }
+ for _, cl := range coolClients {
+ if !b.apply(cl, c.now().Add(time.Minute)) {
+ t.Fatal("apply refused")
+ }
+ }
+ if b.counterCount() != 2 || b.cooldownCount() != 2 {
+ t.Fatalf("setup: %d counters, %d cool-downs, want 2 and 2", b.counterCount(), b.cooldownCount())
+ }
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))
+ // A new counter and a new cool-down fit only because the expired ones go.
+ b.note("bab/shop", burstClientA)
+ if !b.apply(burstClientB, c.now().Add(time.Minute)) {
+ t.Fatal("apply refused")
+ }
+ if n := b.counterCount(); n != 1 {
+ t.Fatalf("counters = %d, want the 2 expired gone and 1 new", n)
+ }
+ if n := b.cooldownCount(); n != 1 {
+ t.Fatalf("cool-downs = %d, want the 2 ended gone and 1 new", n)
+ }
if s := b.stats(); s.CountersEvicted != 0 || s.CooldownsEvicted != 0 {
t.Fatalf("expired entries were counted as evictions: %+v", s)
}
}
+// Time is read under the lock, so calls are recorded in the order their times
+// were read. Before that, a call could read 59.99 s, stall, and record after
+// another had recorded 60.01 s; the ring then held 60 notes that no window of
+// 60 s contained, and tripped. The clock below stalls the first call after
+// the arm point until the second call has finished, or 200 ms pass: with the
+// clock read outside the lock the second call finishes at once and the first
+// trips falsely; under the lock the second call waits its turn.
+func TestBurstReorderedCallsNeverTripFalsely(t *testing.T) {
+ base := newBurstClock()
+ var (
+ armed atomic.Bool
+ calls atomic.Int32
+ sampled = make(chan struct{})
+ secondEnd = make(chan struct{})
+ )
+ b := newBurstLimiter(func() time.Time {
+ if !armed.Load() {
+ return base.now()
+ }
+ switch calls.Add(1) {
+ case 1:
+ close(sampled)
+ select {
+ case <-secondEnd:
+ case <-time.After(200 * time.Millisecond):
+ }
+ return base.now().Add(59*time.Second + 990*time.Millisecond)
+ default:
+ return base.now().Add(60*time.Second + 10*time.Millisecond)
+ }
+ })
+ for i := 0; i < 58; i++ {
+ b.note("bab/shop", burstClientA)
+ }
+ armed.Store(true)
+
+ var first, second bool
+ var wg sync.WaitGroup
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ first, _ = b.note("bab/shop", burstClientA)
+ }()
+ <-sampled
+ second, _ = b.note("bab/shop", burstClientA)
+ close(secondEnd)
+ wg.Wait()
+ if first || second {
+ t.Fatalf("tripped (first %v, second %v) though the 58 earlier notes are more than 60 s from the last", first, second)
+ }
+ if _, _, ok := b.cooling(burstClientA); ok {
+ t.Fatal("client is cooling though no 60 s window held 60 notes")
+ }
+}
+
func TestBurstSingleKeyFloodBounded(t *testing.T) {
b, c := burstTestLimiter()
// A flood that never lets 60 land inside a window: one note per 2 s.