From ff4f88577f4d338f808eed1edc475bfdf3d57c0a Mon Sep 17 00:00:00 2001 From: Wouter de Vries Date: Mon, 22 May 2023 12:47:26 +0200 Subject: [PATCH] fix(TTL): don't add items into expiration map on update if item has no expiration If an item was already in the cache, adding it again will cause it to be added into the expiration map, even if the item has no expiration set. This effectively is a memory leak. There is no clean up of this "zero time" bucket, and it will keep growing. --- ttl.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ttl.go b/ttl.go index 78082ce1..8ccb1266 100644 --- a/ttl.go +++ b/ttl.go @@ -89,6 +89,11 @@ func (m *expirationMap[_]) update(key, conflict uint64, oldExpTime, newExpTime t delete(oldBucket, key) } + // Items that don't expire don't need to be in the expiration map. + if newExpTime.IsZero() { + return + } + newBucketNum := storageBucket(newExpTime) newBucket, ok := m.buckets[newBucketNum] if !ok { @@ -160,7 +165,7 @@ func (m *expirationMap[V]) cleanup(store store[V], policy policy[V], onEvict fun // clear clears the expirationMap, the caller is responsible for properly // evicting the referenced items -func (m *expirationMap) clear() { +func (m *expirationMap[V]) clear() { if m == nil { return }