2020-10-20 23:09:51 +00:00
|
|
|
/*
|
|
|
|
Package ttlcache provides an ExpiryHeap that can be used by a cache to track the
|
|
|
|
expiration time of its entries. When an expiry is reached the Timer will fire
|
|
|
|
and the entry can be removed.
|
|
|
|
*/
|
2020-10-20 22:34:42 +00:00
|
|
|
package ttlcache
|
2020-10-20 17:19:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"container/heap"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-10-20 22:34:42 +00:00
|
|
|
// Entry in the ExpiryHeap, tracks the index and expiry time of an item in a
|
|
|
|
// ttl cache.
|
|
|
|
type Entry struct {
|
2020-10-20 23:09:51 +00:00
|
|
|
key string
|
2020-10-20 22:34:42 +00:00
|
|
|
expiry time.Time
|
|
|
|
heapIndex int
|
|
|
|
}
|
|
|
|
|
2020-10-20 23:00:24 +00:00
|
|
|
// NotIndexed indicates that the entry does not exist in the heap. Either because
|
|
|
|
// it is nil, or because it was removed.
|
|
|
|
const NotIndexed = -1
|
|
|
|
|
2020-10-20 23:09:51 +00:00
|
|
|
// Index returns the index of this entry within the heap.
|
|
|
|
func (e *Entry) Index() int {
|
|
|
|
if e == nil {
|
2020-10-20 23:00:24 +00:00
|
|
|
return NotIndexed
|
2020-10-20 22:34:42 +00:00
|
|
|
}
|
2020-10-20 23:09:51 +00:00
|
|
|
return e.heapIndex
|
2020-10-20 17:19:21 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 23:09:51 +00:00
|
|
|
// Key returns the key for the entry in the heap.
|
|
|
|
func (e *Entry) Key() string {
|
|
|
|
return e.key
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExpiryHeap is a heap that is ordered by the expiry time of entries. It may
|
|
|
|
// be used by a cache or storage to expiry items after a TTL.
|
2020-10-20 17:19:21 +00:00
|
|
|
//
|
2020-10-20 23:09:51 +00:00
|
|
|
// ExpiryHeap expects the caller to synchronize calls to most of its methods. This
|
|
|
|
// is necessary because the cache needs to ensure that updates to both its
|
|
|
|
// storage and the ExpiryHeap are synchronized.
|
2020-10-20 21:38:12 +00:00
|
|
|
type ExpiryHeap struct {
|
2020-10-20 22:34:42 +00:00
|
|
|
entries []*Entry
|
2020-10-20 17:19:21 +00:00
|
|
|
|
|
|
|
// NotifyCh is sent a value whenever the 0 index value of the heap
|
|
|
|
// changes. This can be used to detect when the earliest value
|
|
|
|
// changes.
|
|
|
|
NotifyCh chan struct{}
|
|
|
|
}
|
|
|
|
|
2020-10-20 22:34:42 +00:00
|
|
|
// NewExpiryHeap creates and returns a new ExpiryHeap.
|
2020-10-20 21:38:12 +00:00
|
|
|
func NewExpiryHeap() *ExpiryHeap {
|
|
|
|
h := &ExpiryHeap{NotifyCh: make(chan struct{}, 1)}
|
|
|
|
heap.Init((*entryHeap)(h))
|
2020-10-20 17:59:53 +00:00
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2020-10-20 18:57:22 +00:00
|
|
|
// Add an entry to the heap.
|
|
|
|
//
|
2020-10-20 17:59:53 +00:00
|
|
|
// Must be synchronized by the caller.
|
2020-10-20 22:34:42 +00:00
|
|
|
func (h *ExpiryHeap) Add(key string, expiry time.Duration) *Entry {
|
|
|
|
entry := &Entry{
|
2020-10-20 23:09:51 +00:00
|
|
|
key: key,
|
2020-10-20 22:34:42 +00:00
|
|
|
expiry: time.Now().Add(expiry),
|
2020-10-20 18:57:22 +00:00
|
|
|
// Set the initial heap index to the last index. If the entry is swapped it
|
|
|
|
// will have the correct index set, and if it remains at the end the last
|
|
|
|
// index will be correct.
|
2020-10-20 22:34:42 +00:00
|
|
|
heapIndex: len(h.entries),
|
2020-10-20 18:57:22 +00:00
|
|
|
}
|
2020-10-20 21:38:12 +00:00
|
|
|
heap.Push((*entryHeap)(h), entry)
|
2020-10-20 22:34:42 +00:00
|
|
|
if entry.heapIndex == 0 {
|
2020-10-20 18:57:22 +00:00
|
|
|
h.notify()
|
|
|
|
}
|
2020-10-20 17:19:21 +00:00
|
|
|
return entry
|
|
|
|
}
|
|
|
|
|
2021-02-23 17:13:41 +00:00
|
|
|
// Update the entry that is currently at idx with the new expiry time, if the new
|
|
|
|
// expiry time is further into the future. The heap will be rebalanced after the
|
|
|
|
// entry is updated. If the new expiry time is earlier than the existing expiry
|
|
|
|
// time than the expiry is not modified.
|
2020-10-20 17:19:21 +00:00
|
|
|
//
|
|
|
|
// Must be synchronized by the caller.
|
2020-10-20 21:38:12 +00:00
|
|
|
func (h *ExpiryHeap) Update(idx int, expiry time.Duration) {
|
2020-10-20 23:00:24 +00:00
|
|
|
if idx == NotIndexed {
|
2020-10-20 22:58:30 +00:00
|
|
|
// the previous entry did not have a valid index, its not in the heap
|
|
|
|
return
|
|
|
|
}
|
2020-10-20 21:38:12 +00:00
|
|
|
entry := h.entries[idx]
|
2021-02-23 17:13:41 +00:00
|
|
|
newExpiry := time.Now().Add(expiry)
|
|
|
|
|
|
|
|
// Ignore the new expiry if the time is earlier than the existing expiry.
|
|
|
|
if entry.expiry.After(newExpiry) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
entry.expiry = newExpiry
|
2020-10-20 21:38:12 +00:00
|
|
|
heap.Fix((*entryHeap)(h), idx)
|
2020-10-20 17:19:21 +00:00
|
|
|
|
2020-10-20 22:34:42 +00:00
|
|
|
if idx == 0 || entry.heapIndex == 0 {
|
2020-10-20 17:19:21 +00:00
|
|
|
h.notify()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 22:34:42 +00:00
|
|
|
// Remove the entry at idx from the heap.
|
|
|
|
//
|
2020-10-20 17:59:53 +00:00
|
|
|
// Must be synchronized by the caller.
|
2020-10-20 21:38:12 +00:00
|
|
|
func (h *ExpiryHeap) Remove(idx int) {
|
|
|
|
entry := h.entries[idx]
|
|
|
|
heap.Remove((*entryHeap)(h), idx)
|
2020-10-20 17:59:53 +00:00
|
|
|
|
|
|
|
// A goroutine which is fetching a new value will have a reference to this
|
|
|
|
// entry. When it re-acquires the lock it needs to be informed that
|
2020-10-20 22:34:42 +00:00
|
|
|
// the entry was expired while it was fetching. Setting heapIndex to -1
|
2020-10-20 17:59:53 +00:00
|
|
|
// indicates that the entry is no longer in the heap, and must be re-added.
|
2020-10-20 23:00:24 +00:00
|
|
|
entry.heapIndex = NotIndexed
|
2020-10-20 18:57:22 +00:00
|
|
|
|
|
|
|
if idx == 0 {
|
|
|
|
h.notify()
|
|
|
|
}
|
2020-10-20 17:59:53 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 21:38:12 +00:00
|
|
|
type entryHeap ExpiryHeap
|
2020-10-20 17:19:21 +00:00
|
|
|
|
2020-10-20 21:38:12 +00:00
|
|
|
func (h *entryHeap) Len() int { return len(h.entries) }
|
|
|
|
|
|
|
|
func (h *entryHeap) Swap(i, j int) {
|
|
|
|
h.entries[i], h.entries[j] = h.entries[j], h.entries[i]
|
2020-10-20 22:34:42 +00:00
|
|
|
h.entries[i].heapIndex = i
|
|
|
|
h.entries[j].heapIndex = j
|
2020-10-20 17:19:21 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 21:38:12 +00:00
|
|
|
func (h *entryHeap) Less(i, j int) bool {
|
2020-10-20 17:19:21 +00:00
|
|
|
// The usage of Before here is important (despite being obvious):
|
|
|
|
// this function uses the monotonic time that should be available
|
|
|
|
// on the time.Time value so the heap is immune to wall clock changes.
|
2020-10-20 22:34:42 +00:00
|
|
|
return h.entries[i].expiry.Before(h.entries[j].expiry)
|
2020-10-20 17:19:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// heap.Interface, this isn't expected to be called directly.
|
2020-10-20 21:38:12 +00:00
|
|
|
func (h *entryHeap) Push(x interface{}) {
|
2020-10-20 22:34:42 +00:00
|
|
|
h.entries = append(h.entries, x.(*Entry))
|
2020-10-20 17:19:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// heap.Interface, this isn't expected to be called directly.
|
2020-10-20 21:38:12 +00:00
|
|
|
func (h *entryHeap) Pop() interface{} {
|
|
|
|
n := len(h.entries)
|
|
|
|
entries := h.entries
|
2020-10-20 17:19:21 +00:00
|
|
|
last := entries[n-1]
|
2020-10-20 21:38:12 +00:00
|
|
|
h.entries = entries[0 : n-1]
|
2020-10-20 17:19:21 +00:00
|
|
|
return last
|
|
|
|
}
|
|
|
|
|
2020-10-20 18:57:22 +00:00
|
|
|
// notify the timer that the head value has changed, so the expiry time has
|
|
|
|
// also likely changed.
|
2020-10-20 21:38:12 +00:00
|
|
|
func (h *ExpiryHeap) notify() {
|
2020-10-20 18:57:22 +00:00
|
|
|
// Send to channel without blocking. Skips sending if there is already
|
|
|
|
// an item in the buffered channel.
|
2020-10-20 17:19:21 +00:00
|
|
|
select {
|
|
|
|
case h.NotifyCh <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
2020-10-20 17:59:53 +00:00
|
|
|
|
2020-10-20 22:34:42 +00:00
|
|
|
// Next returns a Timer that waits until the first entry in the heap expires.
|
|
|
|
//
|
2020-10-20 17:59:53 +00:00
|
|
|
// Must be synchronized by the caller.
|
2020-10-20 21:38:12 +00:00
|
|
|
func (h *ExpiryHeap) Next() Timer {
|
|
|
|
if len(h.entries) == 0 {
|
|
|
|
return Timer{}
|
2020-10-20 17:59:53 +00:00
|
|
|
}
|
2020-10-20 21:38:12 +00:00
|
|
|
entry := h.entries[0]
|
|
|
|
return Timer{
|
2020-10-20 22:34:42 +00:00
|
|
|
timer: time.NewTimer(time.Until(entry.expiry)),
|
2020-10-20 17:59:53 +00:00
|
|
|
Entry: entry,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 23:09:51 +00:00
|
|
|
// Timer provides a channel to block on. When the Wait channel receives an
|
|
|
|
// item the Timer.Entry has expired. The caller is expected to call
|
|
|
|
// ExpiryHeap.Remove with the Entry.Index().
|
|
|
|
//
|
|
|
|
// The caller is responsible for calling Stop to stop the timer if the timer has
|
|
|
|
// not fired.
|
2020-10-20 21:38:12 +00:00
|
|
|
type Timer struct {
|
2020-10-20 17:59:53 +00:00
|
|
|
timer *time.Timer
|
2020-10-20 22:34:42 +00:00
|
|
|
Entry *Entry
|
2020-10-20 17:59:53 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 21:38:12 +00:00
|
|
|
func (t *Timer) Wait() <-chan time.Time {
|
2020-10-20 17:59:53 +00:00
|
|
|
if t.timer == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return t.timer.C
|
|
|
|
}
|
|
|
|
|
2020-10-20 21:38:12 +00:00
|
|
|
func (t *Timer) Stop() {
|
2020-10-20 17:59:53 +00:00
|
|
|
if t.timer != nil {
|
|
|
|
t.timer.Stop()
|
|
|
|
}
|
|
|
|
}
|