mirror of https://github.com/hashicorp/consul
lib/ttlcache: unexport key and additional godoc
parent
c17baadbf8
commit
0d4fa882b3
|
@ -26,9 +26,8 @@ import (
|
||||||
"github.com/armon/go-metrics"
|
"github.com/armon/go-metrics"
|
||||||
"golang.org/x/time/rate"
|
"golang.org/x/time/rate"
|
||||||
|
|
||||||
"github.com/hashicorp/consul/lib/ttlcache"
|
|
||||||
|
|
||||||
"github.com/hashicorp/consul/lib"
|
"github.com/hashicorp/consul/lib"
|
||||||
|
"github.com/hashicorp/consul/lib/ttlcache"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:generate mockery -all -inpkg
|
//go:generate mockery -all -inpkg
|
||||||
|
@ -758,12 +757,12 @@ func (c *Cache) runExpiryLoop() {
|
||||||
c.entriesLock.Lock()
|
c.entriesLock.Lock()
|
||||||
|
|
||||||
entry := timer.Entry
|
entry := timer.Entry
|
||||||
if closer, ok := c.entries[entry.Key].State.(io.Closer); ok {
|
if closer, ok := c.entries[entry.Key()].State.(io.Closer); ok {
|
||||||
closer.Close()
|
closer.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Entry expired! Remove it.
|
// Entry expired! Remove it.
|
||||||
delete(c.entries, entry.Key)
|
delete(c.entries, entry.Key())
|
||||||
c.entriesExpiryHeap.Remove(entry.Index())
|
c.entriesExpiryHeap.Remove(entry.Index())
|
||||||
|
|
||||||
// Set some metrics
|
// Set some metrics
|
||||||
|
|
|
@ -1,3 +1,8 @@
|
||||||
|
/*
|
||||||
|
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.
|
||||||
|
*/
|
||||||
package ttlcache
|
package ttlcache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -8,8 +13,7 @@ import (
|
||||||
// Entry in the ExpiryHeap, tracks the index and expiry time of an item in a
|
// Entry in the ExpiryHeap, tracks the index and expiry time of an item in a
|
||||||
// ttl cache.
|
// ttl cache.
|
||||||
type Entry struct {
|
type Entry struct {
|
||||||
// TODO: can Key be unexported?
|
key string
|
||||||
Key string
|
|
||||||
expiry time.Time
|
expiry time.Time
|
||||||
heapIndex int
|
heapIndex int
|
||||||
}
|
}
|
||||||
|
@ -18,18 +22,25 @@ type Entry struct {
|
||||||
// it is nil, or because it was removed.
|
// it is nil, or because it was removed.
|
||||||
const NotIndexed = -1
|
const NotIndexed = -1
|
||||||
|
|
||||||
func (c *Entry) Index() int {
|
// Index returns the index of this entry within the heap.
|
||||||
if c == nil {
|
func (e *Entry) Index() int {
|
||||||
|
if e == nil {
|
||||||
return NotIndexed
|
return NotIndexed
|
||||||
}
|
}
|
||||||
return c.heapIndex
|
return e.heapIndex
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExpiryHeap is a container/heap.Interface implementation that expires entries
|
// Key returns the key for the entry in the heap.
|
||||||
// in the cache when their expiration time is reached.
|
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.
|
||||||
//
|
//
|
||||||
// All operations on the heap and read/write of the heap contents require
|
// ExpiryHeap expects the caller to synchronize calls to most of its methods. This
|
||||||
// the proper entriesLock to be held on Cache.
|
// is necessary because the cache needs to ensure that updates to both its
|
||||||
|
// storage and the ExpiryHeap are synchronized.
|
||||||
type ExpiryHeap struct {
|
type ExpiryHeap struct {
|
||||||
entries []*Entry
|
entries []*Entry
|
||||||
|
|
||||||
|
@ -51,7 +62,7 @@ func NewExpiryHeap() *ExpiryHeap {
|
||||||
// Must be synchronized by the caller.
|
// Must be synchronized by the caller.
|
||||||
func (h *ExpiryHeap) Add(key string, expiry time.Duration) *Entry {
|
func (h *ExpiryHeap) Add(key string, expiry time.Duration) *Entry {
|
||||||
entry := &Entry{
|
entry := &Entry{
|
||||||
Key: key,
|
key: key,
|
||||||
expiry: time.Now().Add(expiry),
|
expiry: time.Now().Add(expiry),
|
||||||
// Set the initial heap index to the last index. If the entry is swapped it
|
// 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
|
// will have the correct index set, and if it remains at the end the last
|
||||||
|
@ -159,6 +170,12 @@ func (h *ExpiryHeap) Next() Timer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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.
|
||||||
type Timer struct {
|
type Timer struct {
|
||||||
timer *time.Timer
|
timer *time.Timer
|
||||||
Entry *Entry
|
Entry *Entry
|
||||||
|
|
Loading…
Reference in New Issue