Skip to content

Commit

Permalink
Fix Memory Leak In New Timecache Implementations (#528)
Browse files Browse the repository at this point in the history
* fix bug

* add for last seen cache
  • Loading branch information
nisdas authored Mar 15, 2023
1 parent 829f902 commit 340387e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
6 changes: 4 additions & 2 deletions timecache/first_seen_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ func TestFirstSeenCacheExpire(t *testing.T) {
}

time.Sleep(2 * time.Second)
if tc.Has(fmt.Sprint(0)) {
t.Fatal("should have dropped this from the cache already")
for i := 0; i < 10; i++ {
if tc.Has(fmt.Sprint(i)) {
t.Fatalf("should have dropped this key: %s from the cache already", fmt.Sprint(i))
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions timecache/last_seen_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ func TestLastSeenCacheExpire(t *testing.T) {
}

time.Sleep(2 * time.Second)
if tc.Has(fmt.Sprint(0)) {
t.Fatal("should have dropped this from the cache already")
for i := 0; i < 11; i++ {
if tc.Has(fmt.Sprint(i)) {
t.Fatalf("should have dropped this key: %s from the cache already", fmt.Sprint(i))
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion timecache/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
var backgroundSweepInterval = time.Minute

func background(ctx context.Context, lk sync.Locker, m map[string]time.Time) {
ticker := time.NewTimer(backgroundSweepInterval)
ticker := time.NewTicker(backgroundSweepInterval)
defer ticker.Stop()

for {
Expand Down

0 comments on commit 340387e

Please sign in to comment.