feat(botfilter): cache.go part 4/5
This commit is contained in:
1 parent
e133bb46c3
commit
70f6339cee
1 file changed
+20
@@ -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}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user