Skip to content

Commit 87e6317

Browse files
committed
ratelimit: fix bucket collection eviction
1 parent 915e5fe commit 87e6317

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

ratelimiter/memory.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import (
55
"time"
66
)
77

8-
const GC_SIZE int = 100
8+
const (
9+
GC_SIZE int = 100
10+
GC_PERIOD time.Duration = 60 * time.Second
11+
)
912

1013
type Memory struct {
1114
store map[string]LeakyBucket
@@ -44,11 +47,10 @@ func (m *Memory) GarbageCollect() {
4447
now := time.Now()
4548

4649
// rate limit GC to once per minute
47-
if now.Add(60*time.Second).Unix() > m.lastGCCollected.Unix() {
48-
50+
if now.Unix() >= m.lastGCCollected.Add(GC_PERIOD).Unix() {
4951
for key, bucket := range m.store {
5052
// if the bucket is drained, then GC
51-
if bucket.DrainedAt().Unix() > now.Unix() {
53+
if bucket.DrainedAt().Unix() < now.Unix() {
5254
delete(m.store, key)
5355
}
5456
}

0 commit comments

Comments
 (0)