diff --git a/cache.go b/cache.go index 7373250d..b6742b81 100644 --- a/cache.go +++ b/cache.go @@ -196,3 +196,21 @@ func (c *clientCache) ensureCapacityLocked(now time.Time) { return } + // Evict the oldest non-banned entry first. If all entries are banned, the + // oldest ban is evicted; the cap remains a hard upper bound either way. + var oldestIP string + var oldestTime time.Time + for ip, state := range c.entries { + candidate := state.lastSeen + if state.banUntil.After(now) { + candidate = state.banUntil + } + if oldestIP == "" || candidate.Before(oldestTime) { + oldestIP, oldestTime = ip, candidate + } + } + if oldestIP != "" { + delete(c.entries, oldestIP) + } +} +