From a9f81d9735e1ba49ebcb1c9fa46b880b40a973b6 Mon Sep 17 00:00:00 2001 From: hoelee Date: Mon, 2 Mar 2026 10:22:00 +0800 Subject: [PATCH] feat(botfilter): add clientCache.cleanupLocked --- cache.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/cache.go b/cache.go index 3c6ee786..f88ffdf4 100644 --- a/cache.go +++ b/cache.go @@ -177,3 +177,17 @@ func (c *clientCache) pruneEventsLocked(state *clientState, now time.Time) { } } +func (c *clientCache) cleanupLocked(now time.Time) { + // Full cleanup every 256 requests amortises the map scan while retaining + // recently scored clients for the entire score window. + if c.operations%256 != 0 { + return + } + cutoff := now.Add(-c.scoreWindow) + for ip, state := range c.entries { + if !state.banUntil.After(now) && !state.lastSeen.After(cutoff) { + delete(c.entries, ip) + } + } +} +