From 40935a071e3059e1abbf213018d5dd7d7258cd8f Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sat, 6 Jul 2019 20:23:05 +0200 Subject: auto-decay flood counter --- internal/flood/flood.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) 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) } -- cgit v1.2.3