summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2019-07-06 20:23:05 +0200
committerDimitri Sokolyuk <demon@dim13.org>2019-07-06 20:23:05 +0200
commit40935a071e3059e1abbf213018d5dd7d7258cd8f (patch)
treeac627fb51a3e0904c56dba6de8f34dba2a018b9e
parenta0c782f83d7349cb5abda92647545e9a78b41502 (diff)
auto-decay flood counter
-rw-r--r--internal/flood/flood.go19
1 files changed, 18 insertions, 1 deletions
diff --git a/internal/flood/flood.go b/internal/flood/flood.go
index c737e8f..0a26edb 100644
--- a/internal/flood/flood.go
+++ b/internal/flood/flood.go
@@ -4,6 +4,7 @@ import (
"math"
"sort"
"strings"
+ "time"
"unicode/utf8"
lru "github.com/hashicorp/golang-lru"
@@ -27,7 +28,9 @@ type Check struct {
func New(k Kicker) *Check {
cache, _ := lru.New(100)
- return &Check{k: k, cache: cache}
+ c := &Check{k: k, cache: cache}
+ go c.decay(time.Hour)
+ return c
}
func (c Check) get(nick string) (int, bool) {
@@ -41,6 +44,20 @@ func (c Check) del(nick string) {
c.cache.Remove(nick)
}
+func (c Check) decay(t time.Duration) {
+ for range time.Tick(t) {
+ for _, k := range c.cache.Keys() {
+ nick := k.(string)
+ n, _ := c.get(nick)
+ if n > 1 {
+ c.set(nick, n-1)
+ } else {
+ c.del(nick)
+ }
+ }
+ }
+}
+
func (c Check) set(nick string, n int) {
c.cache.Add(nick, n)
}