From b9f1b8d746d5076838040e660bf265d5e340398c Mon Sep 17 00:00:00 2001 From: hoelee Date: Thu, 12 Feb 2026 09:58:00 +0800 Subject: [PATCH] feat(botfilter): cache.go part 4/5 --- cache.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) 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} +} +