feat(botfilter): cache.go part 4/5

This commit is contained in:
hoelee committed 2026-02-12 09:58:00 +08:00
1 parent c0b2da8553
commit b9f1b8d746
1 file changed
+20
+20
View File
@@ -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}
}