ratelimit: fix bucket collection eviction
This commit is contained in:
parent
915e5feba0
commit
87e6317929
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue