diff --git a/pkg/infrared/infrared.go b/pkg/infrared/infrared.go index 0418c65..17cec2e 100644 --- a/pkg/infrared/infrared.go +++ b/pkg/infrared/infrared.go @@ -167,7 +167,7 @@ func (ir *Infrared) listenAndServe(srvReqChan chan<- ServerRequest) error { func (ir *Infrared) handleNewConn(c net.Conn, srvReqChan chan<- ServerRequest) { if err := ir.filter.Filter(c); err != nil { - //log.Printf("Filtered: %s", err) + // log.Printf("Filtered: %s", err) return } diff --git a/pkg/infrared/rate_limiter.go b/pkg/infrared/rate_limiter.go index 02eb7d8..78f331a 100644 --- a/pkg/infrared/rate_limiter.go +++ b/pkg/infrared/rate_limiter.go @@ -2,9 +2,9 @@ package infrared import ( "errors" - "fmt" "math" "net" + "strconv" "strings" "sync" "time" @@ -86,7 +86,8 @@ func canonicalizeIP(ip string) string { return ip } - return ipv6.Mask(net.CIDRMask(64, 128)).String() + ones, bits := 64, 128 + return ipv6.Mask(net.CIDRMask(ones, bits)).String() } func newRateLimiter(requestLimit int, windowLength time.Duration, options ...RateLimiterOption) *rateLimiter { @@ -207,15 +208,13 @@ func (c *localCounter) evict() { c.mu.Lock() defer c.mu.Unlock() - d := c.windowLength * 3 - - if time.Since(c.lastEvict) < d { + if time.Since(c.lastEvict) < c.windowLength { return } c.lastEvict = time.Now() for k, v := range c.counters { - if time.Since(v.updatedAt) >= d { + if time.Since(v.updatedAt) >= c.windowLength { delete(c.counters, k) } } @@ -224,6 +223,6 @@ func (c *localCounter) evict() { func limitCounterKey(key string, window time.Time) uint64 { h := xxhash.New() _, _ = h.WriteString(key) - _, _ = h.WriteString(fmt.Sprintf("%d", window.Unix())) + _, _ = h.WriteString(strconv.FormatInt(window.Unix(), 10)) return h.Sum64() }