From 340387ef3ff652a8244e9d7cf95c940fffee0de7 Mon Sep 17 00:00:00 2001 From: Nishant Das Date: Wed, 15 Mar 2023 19:52:01 +0800 Subject: [PATCH] Fix Memory Leak In New Timecache Implementations (#528) * fix bug * add for last seen cache --- timecache/first_seen_cache_test.go | 6 ++++-- timecache/last_seen_cache_test.go | 6 ++++-- timecache/util.go | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/timecache/first_seen_cache_test.go b/timecache/first_seen_cache_test.go index 858a4617..59d2a59e 100644 --- a/timecache/first_seen_cache_test.go +++ b/timecache/first_seen_cache_test.go @@ -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)) + } } } diff --git a/timecache/last_seen_cache_test.go b/timecache/last_seen_cache_test.go index 4005df58..45220267 100644 --- a/timecache/last_seen_cache_test.go +++ b/timecache/last_seen_cache_test.go @@ -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)) + } } } diff --git a/timecache/util.go b/timecache/util.go index aaf35c35..eaf92b34 100644 --- a/timecache/util.go +++ b/timecache/util.go @@ -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 {