2026-04-04 10:29:02 +00:00
|
|
|
package proxy
|
|
|
|
|
|
2026-04-04 18:49:03 +00:00
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
2026-04-04 10:29:02 +00:00
|
|
|
|
|
|
|
|
func TestRateLimiter_Allow(t *testing.T) {
|
|
|
|
|
rl := NewRateLimiter(RateLimit{MaxConnectionsPerMinute: 1, BanDurationSeconds: 1})
|
|
|
|
|
if !rl.Allow("1.2.3.4:1234") {
|
|
|
|
|
t.Fatalf("expected first call to pass")
|
|
|
|
|
}
|
|
|
|
|
if rl.Allow("1.2.3.4:1234") {
|
|
|
|
|
t.Fatalf("expected second call to fail")
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-04 18:49:03 +00:00
|
|
|
|
|
|
|
|
func TestRateLimiter_Allow_ReplenishesHighLimits(t *testing.T) {
|
|
|
|
|
rl := NewRateLimiter(RateLimit{MaxConnectionsPerMinute: 120, BanDurationSeconds: 1})
|
|
|
|
|
rl.mu.Lock()
|
2026-04-05 03:34:07 +00:00
|
|
|
rl.bucketByHost["1.2.3.4"] = &tokenBucket{
|
2026-04-04 18:49:03 +00:00
|
|
|
tokens: 0,
|
|
|
|
|
lastRefill: time.Now().Add(-30 * time.Second),
|
|
|
|
|
}
|
|
|
|
|
rl.mu.Unlock()
|
|
|
|
|
|
|
|
|
|
if !rl.Allow("1.2.3.4:1234") {
|
|
|
|
|
t.Fatalf("expected bucket to replenish at 120/min")
|
|
|
|
|
}
|
|
|
|
|
}
|