fix(06-07): make limiter admission atomic

- Replace split store checks with one mutex-guarded Attempt operation
- Use domainless trusted-client keys for anonymous inline throttles
- Preserve fixed-window headers, expiry, stacking, and principal isolation
This commit is contained in:
Jakub Zych
2026-09-20 17:02:53 +02:00
parent 5bcd7ba011
commit 6852a8f8c3
4 changed files with 56 additions and 83 deletions

View File

@@ -129,39 +129,37 @@ func TestFixedWindowLimiterMemoryStoreWindow(t *testing.T) {
s := NewMemoryStore(0)
decay := 80 * time.Millisecond
if c := s.Hit("k", decay); c != 1 {
t.Fatalf("hit 1 = %d", c)
if allowed, attempts, _ := s.Attempt("k", 3, decay); !allowed || attempts != 1 {
t.Fatalf("attempt 1 = allowed %v, attempts %d", allowed, attempts)
}
if c := s.Hit("k", decay); c != 2 {
t.Fatalf("hit 2 = %d", c)
if allowed, attempts, _ := s.Attempt("k", 3, decay); !allowed || attempts != 2 {
t.Fatalf("attempt 2 = allowed %v, attempts %d", allowed, attempts)
}
if s.TooManyAttempts("k", 3) {
t.Fatal("TooManyAttempts at count==max-1")
if allowed, attempts, _ := s.Attempt("k", 3, decay); !allowed || attempts != 3 {
t.Fatalf("attempt 3 = allowed %v, attempts %d", allowed, attempts)
}
if c := s.Hit("k", decay); c != 3 {
t.Fatalf("hit 3 = %d", c)
}
if !s.TooManyAttempts("k", 3) {
t.Fatal("TooManyAttempts at count==max")
if allowed, attempts, _ := s.Attempt("k", 3, decay); allowed || attempts != 3 {
t.Fatalf("denied attempt = allowed %v, attempts %d", allowed, attempts)
}
time.Sleep(decay + 20*time.Millisecond)
if s.TooManyAttempts("k", 3) {
t.Fatal("TooManyAttempts after window elapsed")
}
if c := s.Hit("k", decay); c != 1 {
t.Fatalf("fresh window hit = %d", c)
if allowed, attempts, _ := s.Attempt("k", 3, decay); !allowed || attempts != 1 {
t.Fatalf("fresh-window attempt = allowed %v, attempts %d", allowed, attempts)
}
}
func TestFixedWindowLimiterMemoryStoreFirstHitWins(t *testing.T) {
s := NewMemoryStore(0)
decay := 200 * time.Millisecond
s.Hit("k", decay)
if allowed, _, _ := s.Attempt("k", 2, decay); !allowed {
t.Fatal("first attempt denied")
}
time.Sleep(120 * time.Millisecond)
s.Hit("k", decay) // must not extend the original window
if allowed, _, _ := s.Attempt("k", 2, decay); !allowed {
t.Fatal("second attempt denied")
}
time.Sleep(100 * time.Millisecond)
if s.TooManyAttempts("k", 1) {
if allowed, attempts, _ := s.Attempt("k", 1, decay); !allowed || attempts != 1 {
t.Fatal("window was extended by a later hit")
}
}