summaryrefslogtreecommitdiffhomepage
path: root/ratelimiter/ratelimiter.go
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-05-21 03:18:56 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2018-05-21 03:20:18 +0200
commite28d70f5b2a00af75aa9f283e31c7da31a2913df (patch)
tree643f86329523f1c6ee07fed8e4568d9bb88c2167 /ratelimiter/ratelimiter.go
parent84c5357cf31b9b04e794f474ad3ab47cc55f61a1 (diff)
ratelimiter: do not run GC with nothing to do
Diffstat (limited to 'ratelimiter/ratelimiter.go')
-rw-r--r--ratelimiter/ratelimiter.go31
1 files changed, 23 insertions, 8 deletions
diff --git a/ratelimiter/ratelimiter.go b/ratelimiter/ratelimiter.go
index 95b547e..5a844da 100644
--- a/ratelimiter/ratelimiter.go
+++ b/ratelimiter/ratelimiter.go
@@ -27,7 +27,7 @@ type RatelimiterEntry struct {
type Ratelimiter struct {
mutex sync.RWMutex
- stop chan struct{}
+ stopReset chan struct{}
tableIPv4 map[[net.IPv4len]byte]*RatelimiterEntry
tableIPv6 map[[net.IPv6len]byte]*RatelimiterEntry
}
@@ -36,8 +36,8 @@ func (rate *Ratelimiter) Close() {
rate.mutex.Lock()
defer rate.mutex.Unlock()
- if rate.stop != nil {
- close(rate.stop)
+ if rate.stopReset != nil {
+ close(rate.stopReset)
}
}
@@ -47,11 +47,11 @@ func (rate *Ratelimiter) Init() {
// stop any ongoing garbage collection routine
- if rate.stop != nil {
- close(rate.stop)
+ if rate.stopReset != nil {
+ close(rate.stopReset)
}
- rate.stop = make(chan struct{})
+ rate.stopReset = make(chan struct{})
rate.tableIPv4 = make(map[[net.IPv4len]byte]*RatelimiterEntry)
rate.tableIPv6 = make(map[[net.IPv6len]byte]*RatelimiterEntry)
@@ -59,11 +59,16 @@ func (rate *Ratelimiter) Init() {
go func() {
ticker := time.NewTicker(time.Second)
+ ticker.Stop()
for {
select {
- case <-rate.stop:
+ case _, ok := <-rate.stopReset:
ticker.Stop()
- return
+ if ok {
+ ticker = time.NewTicker(time.Second)
+ } else {
+ return
+ }
case <-ticker.C:
func() {
rate.mutex.Lock()
@@ -84,6 +89,10 @@ func (rate *Ratelimiter) Init() {
}
entry.mutex.Unlock()
}
+
+ if len(rate.tableIPv4) == 0 && len(rate.tableIPv6) == 0 {
+ ticker.Stop()
+ }
}()
}
}
@@ -121,8 +130,14 @@ func (rate *Ratelimiter) Allow(ip net.IP) bool {
rate.mutex.Lock()
if IPv4 != nil {
rate.tableIPv4[keyIPv4] = entry
+ if len(rate.tableIPv4) == 1 && len(rate.tableIPv6) == 0 {
+ rate.stopReset <- struct{}{}
+ }
} else {
rate.tableIPv6[keyIPv6] = entry
+ if len(rate.tableIPv6) == 1 && len(rate.tableIPv4) == 0 {
+ rate.stopReset <- struct{}{}
+ }
}
rate.mutex.Unlock()
return true