Skip to content

Commit

Permalink
fix(TTL): don't add items into expiration map on update if item has n…
Browse files Browse the repository at this point in the history
…o 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.
  • Loading branch information
Woutifier committed Sep 29, 2023
1 parent c888d21 commit ff4f885
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion ttl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit ff4f885

Please sign in to comment.