ratelimit: fix bucket collection eviction

This commit is contained in:
Adam Bozanich 2016-12-21 19:02:41 -08:00
parent 915e5feba0
commit 87e6317929
1 changed files with 6 additions and 4 deletions

View File

@ -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)
}
}