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) + } + } +} +