From 87e6317929c4b7ce85908ba56818b49f858cf372 Mon Sep 17 00:00:00 2001 From: Adam Bozanich Date: Wed, 21 Dec 2016 19:02:41 -0800 Subject: [PATCH] ratelimit: fix bucket collection eviction --- ratelimiter/memory.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ratelimiter/memory.go b/ratelimiter/memory.go index 8f6a578..bf3c213 100644 --- a/ratelimiter/memory.go +++ b/ratelimiter/memory.go @@ -5,7 +5,10 @@ import ( "time" ) -const GC_SIZE int = 100 +const ( + GC_SIZE int = 100 + GC_PERIOD time.Duration = 60 * time.Second +) type Memory struct { store map[string]LeakyBucket @@ -44,11 +47,10 @@ func (m *Memory) GarbageCollect() { now := time.Now() // 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 { // if the bucket is drained, then GC - if bucket.DrainedAt().Unix() > now.Unix() { + if bucket.DrainedAt().Unix() < now.Unix() { delete(m.store, key) } }