Merge pull request #105 from boz/fix-rl-gc-eviction

ratelimit: fix bucket collection eviction
This commit is contained in:
Nino Kodabande 2017-07-07 12:15:47 -07:00 committed by GitHub
commit 2b56f8711f
1 changed files with 6 additions and 4 deletions

View File

@ -5,7 +5,10 @@ import (
"time" "time"
) )
const GC_SIZE int = 100 const (
GC_SIZE int = 100
GC_PERIOD time.Duration = 60 * time.Second
)
type Memory struct { type Memory struct {
store map[string]LeakyBucket store map[string]LeakyBucket
@ -44,11 +47,10 @@ func (m *Memory) GarbageCollect() {
now := time.Now() now := time.Now()
// rate limit GC to once per minute // rate limit GC to once per minute
if now.Add(60*time.Second).Unix() > m.lastGCCollected.Unix() { if now.Unix() >= m.lastGCCollected.Add(GC_PERIOD).Unix() {
for key, bucket := range m.store { for key, bucket := range m.store {
// if the bucket is drained, then GC // if the bucket is drained, then GC
if bucket.DrainedAt().Unix() > now.Unix() { if bucket.DrainedAt().Unix() < now.Unix() {
delete(m.store, key) delete(m.store, key)
} }
} }