fix(api): 清理投稿限流器的过期 IP 记录 (#44)

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
fu500f
2026-10-31 00:52:56 +00:00
co-authored by Devin AI
parent 36f9c95fa7
commit 31e1a8977b
2 changed files with 57 additions and 8 deletions
@@ -0,0 +1,31 @@
package adminhandler
import (
"testing"
"time"
)
func TestRateLimiterPrunesExpiredKeysAndPreservesLiveLimits(t *testing.T) {
window := time.Minute
now := time.Now()
limiter := newRateLimiter(2, window)
limiter.hits["expired"] = []time.Time{now.Add(-2 * window)}
limiter.hits["live"] = []time.Time{now.Add(-time.Second)}
limiter.lastPrune = now.Add(-2 * window)
if !limiter.allow("live") {
t.Fatal("allow rejected a live key below its limit")
}
if _, ok := limiter.hits["expired"]; ok {
t.Fatal("expired key was not pruned")
}
if got := len(limiter.hits["live"]); got != 2 {
t.Fatalf("live key hit count = %d, want 2", got)
}
if limiter.allow("live") {
t.Fatal("allow accepted a live key at its limit")
}
if got := len(limiter.hits["live"]); got != 2 {
t.Fatalf("live key hit count after rejection = %d, want 2", got)
}
}