diff --git a/cache.go b/cache.go index 1e17d0fe..a5116faa 100644 --- a/cache.go +++ b/cache.go @@ -120,3 +120,23 @@ func (c *clientCache) observeResponse(ip string, status int, points int, now tim return cacheResult{} } + c.mu.Lock() + defer c.mu.Unlock() + state := c.entries[ip] + if state == nil || state.banUntil.After(now) { + return cacheResult{} + } + c.pruneEventsLocked(state, now) + state.lastSeen = now + state.events = append(state.events, scoreEvent{at: now, points: points}) + if len(state.events) > c.maxEvents { + state.events = append([]scoreEvent(nil), state.events[len(state.events)-c.maxEvents:]...) + } + score := sumScore(state.events) + if score >= c.threshold { + state.banUntil = now.Add(c.banDuration) + return cacheResult{banned: true, banUntil: state.banUntil, score: score} + } + return cacheResult{score: score} +} +