From c17baadbf87ac9c601f59ad134648dadf832dd87 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Tue, 20 Oct 2020 19:00:24 -0400 Subject: [PATCH] lib/ttlcache: add a constant for NotIndexed --- agent/cache/cache.go | 2 +- lib/ttlcache/eviction.go | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/agent/cache/cache.go b/agent/cache/cache.go index c7b7bcfc1c..dd6d092345 100644 --- a/agent/cache/cache.go +++ b/agent/cache/cache.go @@ -683,7 +683,7 @@ func (c *Cache) fetch(key string, r getOptions, allowNew bool, attempt uint, ign // If this is a new entry (not in the heap yet), then setup the // initial expiry information and insert. If we're already in // the heap we do nothing since we're reusing the same entry. - if newEntry.Expiry == nil || newEntry.Expiry.Index() == -1 { + if newEntry.Expiry == nil || newEntry.Expiry.Index() == ttlcache.NotIndexed { newEntry.Expiry = c.entriesExpiryHeap.Add(key, tEntry.Opts.LastGetTTL) } diff --git a/lib/ttlcache/eviction.go b/lib/ttlcache/eviction.go index df9624bdcb..3c704edeb7 100644 --- a/lib/ttlcache/eviction.go +++ b/lib/ttlcache/eviction.go @@ -14,9 +14,13 @@ type Entry struct { heapIndex int } +// NotIndexed indicates that the entry does not exist in the heap. Either because +// it is nil, or because it was removed. +const NotIndexed = -1 + func (c *Entry) Index() int { if c == nil { - return -1 + return NotIndexed } return c.heapIndex } @@ -66,7 +70,7 @@ func (h *ExpiryHeap) Add(key string, expiry time.Duration) *Entry { // // Must be synchronized by the caller. func (h *ExpiryHeap) Update(idx int, expiry time.Duration) { - if idx < 0 { + if idx == NotIndexed { // the previous entry did not have a valid index, its not in the heap return } @@ -92,7 +96,7 @@ func (h *ExpiryHeap) Remove(idx int) { // entry. When it re-acquires the lock it needs to be informed that // the entry was expired while it was fetching. Setting heapIndex to -1 // indicates that the entry is no longer in the heap, and must be re-added. - entry.heapIndex = -1 + entry.heapIndex = NotIndexed if idx == 0 { h.notify()