Skip to content

Commit

Permalink
fix: linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
haveachin committed Jan 31, 2024
1 parent f8ebc0d commit 97a098c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/infrared/infrared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
13 changes: 6 additions & 7 deletions pkg/infrared/rate_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package infrared

import (
"errors"
"fmt"
"math"
"net"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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()
}

0 comments on commit 97a098c

Please sign in to comment.