Documentation
¶
Overview ¶
Package singleflight provides a duplicate function call suppression mechanism.
Index ¶
- Constants
- Variables
- func RoundUpPowerOf2(v uint32) uint32
- type Buffer
- type CountMinSketch
- type DataBlock
- type Entry
- func (e *Entry[K, V]) Next(listType uint8) *Entry[K, V]
- func (e *Entry[K, V]) NextExpire() *Entry[K, V]
- func (e *Entry[K, V]) NextPolicy() *Entry[K, V]
- func (e *Entry[K, V]) PolicyWeight() int64
- func (e *Entry[K, V]) Position() string
- func (e *Entry[K, V]) Prev(listType uint8) *Entry[K, V]
- func (e *Entry[K, V]) PrevExpire() *Entry[K, V]
- func (e *Entry[K, V]) PrevPolicy() *Entry[K, V]
- func (e *Entry[K, V]) Weight() int64
- type Flag
- func (f *Flag) IsDeleted() bool
- func (f *Flag) IsFromNVM() bool
- func (f *Flag) IsProbation() bool
- func (f *Flag) IsProtected() bool
- func (f *Flag) IsRemoved() bool
- func (f *Flag) IsRoot() bool
- func (f *Flag) IsWindow() bool
- func (f *Flag) SetDeleted(isDeleted bool)
- func (f *Flag) SetFromNVM(isFromNVM bool)
- func (f *Flag) SetProbation(isProbation bool)
- func (f *Flag) SetProtected(isProtected bool)
- func (f *Flag) SetRemoved(isRemoved bool)
- func (f *Flag) SetRoot(isRoot bool)
- func (f *Flag) SetWindow(isWindow bool)
- type Group
- type Hasher
- type List
- func (l *List[K, V]) Back() *Entry[K, V]
- func (l *List[K, V]) Contains(entry *Entry[K, V]) bool
- func (l *List[K, V]) Front() *Entry[K, V]
- func (l *List[K, V]) Len() int
- func (l *List[K, V]) MoveAfter(e, mark *Entry[K, V])
- func (l *List[K, V]) MoveBefore(e, mark *Entry[K, V])
- func (l *List[K, V]) MoveToBack(e *Entry[K, V])
- func (l *List[K, V]) MoveToFront(e *Entry[K, V])
- func (l *List[K, V]) Persist(writer io.Writer, blockEncoder *gob.Encoder, tp uint8) error
- func (l *List[K, V]) PopTail() *Entry[K, V]
- func (l *List[K, V]) PushBack(e *Entry[K, V])
- func (l *List[K, V]) PushFront(e *Entry[K, V])
- func (l *List[K, V]) Remove(e *Entry[K, V])
- func (l *List[K, V]) Reset()
- type Loaded
- type LoadingStore
- type MetaData
- type NotFound
- type Pentry
- type PolicyBuffers
- type RBMutex
- type RToken
- type ReadBufItem
- type RemoveReason
- type Result
- type SecondaryCache
- type SecondaryCacheItem
- type Serializer
- type Shard
- type SimpleMapSecondary
- func (s *SimpleMapSecondary[K, V]) Delete(key K) error
- func (s *SimpleMapSecondary[K, V]) Get(key K) (value V, cost int64, expire int64, ok bool, err error)
- func (s *SimpleMapSecondary[K, V]) HandleAsyncError(err error)
- func (s *SimpleMapSecondary[K, V]) Set(key K, value V, cost int64, expire int64) error
- func (s *SimpleMapSecondary[K, V]) SetClock(clock *clock.Clock)
- type Slru
- type Stats
- type Store
- func (s *Store[K, V]) Close()
- func (s *Store[K, V]) DebugInfo() debugInfo
- func (s *Store[K, V]) Delete(key K)
- func (s *Store[K, V]) DeleteWithSecondary(key K) error
- func (s *Store[K, V]) EstimatedSize() int
- func (s *Store[K, V]) Get(key K) (V, bool)
- func (s *Store[K, V]) GetWithSecodary(key K) (value V, ok bool, err error)
- func (s *Store[K, V]) Len() int
- func (s *Store[K, V]) Persist(version uint64, writer io.Writer) error
- func (s *Store[K, V]) Range(f func(key K, value V) bool)
- func (s *Store[K, V]) RangeEntry(f func(entry *Entry[K, V]))
- func (s *Store[K, V]) Recover(version uint64, reader io.Reader) error
- func (s *Store[K, V]) Set(key K, value V, cost int64, ttl time.Duration) bool
- func (s *Store[K, V]) Stats() Stats
- func (s *Store[K, V]) Wait()
- type StoreMeta
- type StringKey
- type TimerWheel
- type TinyLfu
- type UnsignedCounter
- type WriteBufItem
Constants ¶
const ( NEW int8 = iota REMOVE UPDATE EVICTE WAIT )
const ( LIST_PROBATION uint8 = 1 LIST_PROTECTED uint8 = 2 WHEEL_LIST uint8 = 3 LIST_WINDOW uint8 = 4 )
const ( ADMIT_HASHDOS_THRESHOLD = 6 HILL_CLIMBER_STEP_DECAY_RATE = 0.98 HILL_CLIMBER_STEP_PERCENT = 0.0625 )
const BlockBufferSize = 4 * 1024 * 1024
Variables ¶
Functions ¶
func RoundUpPowerOf2 ¶ added in v0.4.0
RoundUpPowerOf2 is based on https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2.
Types ¶
type Buffer ¶ added in v0.4.0
type Buffer[K comparable, V any] struct { // contains filtered or unexported fields }
Buffer is a circular ring buffer stores the elements being transferred by the producers to the consumer. The monotonically increasing count of reads and writes allow indexing sequentially to the next element location based upon a power-of-two sizing.
The producers race to read the counts, check if there is available capacity, and if so then try once to CAS to the next write count. If the increment is successful then the producer lazily publishes the element. The producer does not retry or block when unsuccessful due to a failed CAS or the buffer being full.
The consumer reads the counts and takes the available elements. The clearing of the elements and the next read count are lazily set.
This implementation is striped to further increase concurrency.
func NewBuffer ¶ added in v0.4.0
func NewBuffer[K comparable, V any]() *Buffer[K, V]
New creates a new lossy Buffer.
func (*Buffer[K, V]) Add ¶ added in v0.4.0
func (b *Buffer[K, V]) Add(n ReadBufItem[K, V]) *PolicyBuffers[K, V]
Add lazily publishes the item to the consumer.
item may be lost due to contention.
type CountMinSketch ¶
func NewCountMinSketch ¶
func NewCountMinSketch() *CountMinSketch
func (*CountMinSketch) Add ¶
func (s *CountMinSketch) Add(h uint64) bool
func (*CountMinSketch) Addn ¶ added in v0.5.0
func (s *CountMinSketch) Addn(h uint64, n int)
used in test
func (*CountMinSketch) EnsureCapacity ¶ added in v0.3.0
func (s *CountMinSketch) EnsureCapacity(size uint)
func (*CountMinSketch) Estimate ¶
func (s *CountMinSketch) Estimate(h uint64) uint
type DataBlock ¶ added in v0.2.6
type DataBlock[V any] struct { Type uint8 // 1: meta&timerwheel, 2: window, 3: probation, 4: protected SecondaryType uint8 CheckSum uint64 Index uint64 // helper filed, usage depends on Type/SecondaryType Data []byte // contains filtered or unexported fields }
type Entry ¶
type Entry[K comparable, V any] struct { // contains filtered or unexported fields }
func NewEntry ¶
func NewEntry[K comparable, V any](key K, value V, cost int64, expire int64) *Entry[K, V]
used in test only
func (*Entry[K, V]) NextExpire ¶ added in v0.6.0
func (*Entry[K, V]) NextPolicy ¶ added in v0.6.0
func (*Entry[K, V]) PolicyWeight ¶ added in v0.5.0
func (*Entry[K, V]) PrevExpire ¶ added in v0.6.0
func (*Entry[K, V]) PrevPolicy ¶ added in v0.6.0
type Flag ¶ added in v0.5.0
type Flag struct {
Flags int8
}
Flag struct uses 8 bits, with each bit representing a boolean value. Currently, 5 bits are used. All bits are read/write in policy only(with policy mutex), so safe to put them together. Bit 1: Indicates if this entry is a root of linked list. Bit 2: Indicates if this entry is on probation. Bit 3: Indicates if this entry is protected. Bit 4: Indicates if this entry is removed from main(SLRU). Bit 5: Indicates if this entry is from NVM. Bit 6: Indicates if this entry is deleted by API. Bit 7: Indicates if this entry is window.
func (*Flag) IsProbation ¶ added in v0.5.0
func (*Flag) IsProtected ¶ added in v0.5.0
func (*Flag) SetDeleted ¶ added in v0.5.0
func (*Flag) SetFromNVM ¶ added in v0.5.0
func (*Flag) SetProbation ¶ added in v0.5.0
func (*Flag) SetProtected ¶ added in v0.5.0
func (*Flag) SetRemoved ¶ added in v0.5.0
type Group ¶ added in v0.2.0
type Group[K comparable, V any] struct { // contains filtered or unexported fields }
Group represents a class of work and forms a namespace in which units of work can be executed with duplicate suppression.
func NewGroup ¶ added in v0.2.6
func NewGroup[K comparable, V any]() *Group[K, V]
func (*Group[K, V]) Do ¶ added in v0.2.0
Do executes and returns the results of the given function, making sure that only one execution is in-flight for a given key at a time. If a duplicate comes in, the duplicate caller waits for the original to complete and receives the same results. The return value shared indicates whether v was given to multiple callers.
type Hasher ¶
type Hasher[K comparable] struct { // contains filtered or unexported fields }
func NewHasher ¶
func NewHasher[K comparable](stringKeyFunc func(K) string) *Hasher[K]
type List ¶
type List[K comparable, V any] struct { // contains filtered or unexported fields }
List represents a doubly linked list. The zero value for List is an empty list ready to use.
func NewList ¶
func NewList[K comparable, V any](size uint, listType uint8) *List[K, V]
New returns an initialized list.
func (*List[K, V]) MoveAfter ¶
MoveAfter moves element e to its new position after mark. If e or mark is not an element of l, or e == mark, the list is not modified. The element and mark must not be nil.
func (*List[K, V]) MoveBefore ¶
MoveBefore moves element e to its new position before mark. If e or mark is not an element of l, or e == mark, the list is not modified. The element and mark must not be nil.
func (*List[K, V]) MoveToBack ¶
MoveToBack moves element e to the back of list l. If e is not an element of l, the list is not modified. The element must not be nil.
func (*List[K, V]) MoveToFront ¶
MoveToFront moves element e to the front of list l. If e is not an element of l, the list is not modified. The element must not be nil.
type LoadingStore ¶ added in v0.2.0
type LoadingStore[K comparable, V any] struct { *Store[K, V] // contains filtered or unexported fields }
func NewLoadingStore ¶ added in v0.2.0
func NewLoadingStore[K comparable, V any](store *Store[K, V]) *LoadingStore[K, V]
type MetaData ¶
type MetaData[K comparable, V any] struct { // contains filtered or unexported fields }
type Pentry ¶ added in v0.2.6
type Pentry[K comparable, V any] struct { Key K Value V Weight int64 PolicyWeight int64 Expire int64 Frequency int32 Flag Flag }
entry for persistence
type PolicyBuffers ¶ added in v0.4.0
type PolicyBuffers[K comparable, V any] struct { Returned []ReadBufItem[K, V] }
PolicyBuffers is the set of buffers returned by the lossy buffer.
type RBMutex ¶ added in v0.4.0
type RBMutex struct {
// contains filtered or unexported fields
}
A RBMutex is a reader biased reader/writer mutual exclusion lock. The lock can be held by an many readers or a single writer. The zero value for a RBMutex is an unlocked mutex.
A RBMutex must not be copied after first use.
RBMutex is based on a modified version of BRAVO (Biased Locking for Reader-Writer Locks) algorithm: https://arxiv.org/pdf/1810.01553.pdf
RBMutex is a specialized mutex for scenarios, such as caches, where the vast majority of locks are acquired by readers and write lock acquire attempts are infrequent. In such scenarios, RBMutex performs better than sync.RWMutex on large multicore machines.
RBMutex extends sync.RWMutex internally and uses it as the "reader bias disabled" fallback, so the same semantics apply. The only noticeable difference is in reader tokens returned from the RLock/RUnlock methods.
func NewRBMutex ¶ added in v0.4.0
func NewRBMutex() *RBMutex
NewRBMutex creates a new RBMutex instance.
func (*RBMutex) Lock ¶ added in v0.4.0
func (mu *RBMutex) Lock()
Lock locks m for writing. If the lock is already locked for reading or writing, Lock blocks until the lock is available.
func (*RBMutex) RLock ¶ added in v0.4.0
RLock locks m for reading and returns a reader token. The token must be used in the later RUnlock call.
Should not be used for recursive read locking; a blocked Lock call excludes new readers from acquiring the lock.
func (*RBMutex) RUnlock ¶ added in v0.4.0
RUnlock undoes a single RLock call. A reader token obtained from the RLock call must be provided. RUnlock does not affect other simultaneous readers. A panic is raised if m is not locked for reading on entry to RUnlock.
func (*RBMutex) TryRLock ¶ added in v0.4.0
TryRLock tries to lock m for reading without blocking. When TryRLock succeeds, it returns true and a reader token. In case of a failure, a false is returned.
func (*RBMutex) Unlock ¶ added in v0.4.0
func (mu *RBMutex) Unlock()
Unlock unlocks m for writing. A panic is raised if m is not locked for writing on entry to Unlock.
As with RWMutex, a locked RBMutex is not associated with a particular goroutine. One goroutine may RLock (Lock) a RBMutex and then arrange for another goroutine to RUnlock (Unlock) it.
type RToken ¶ added in v0.4.0
type RToken struct {
// contains filtered or unexported fields
}
RToken is a reader lock token.
type ReadBufItem ¶
type ReadBufItem[K comparable, V any] struct { // contains filtered or unexported fields }
type RemoveReason ¶ added in v0.1.2
type RemoveReason uint8
const ( REMOVED RemoveReason = iota EVICTED EXPIRED )
type Result ¶ added in v0.2.0
type Result struct {
Val interface{}
Err error
}
Result holds the results of Do, so they can be passed on a channel.
type SecondaryCache ¶ added in v0.3.0
type SecondaryCacheItem ¶ added in v0.3.0
type SecondaryCacheItem[K comparable, V any] struct { // contains filtered or unexported fields }
type Serializer ¶ added in v0.3.0
type Shard ¶
type Shard[K comparable, V any] struct { // contains filtered or unexported fields }
type SimpleMapSecondary ¶ added in v0.5.0
type SimpleMapSecondary[K comparable, V any] struct { ErrCounter atomic.Uint64 ErrMode bool // contains filtered or unexported fields }
used in test only
func NewSimpleMapSecondary ¶ added in v0.5.0
func NewSimpleMapSecondary[K comparable, V any]() *SimpleMapSecondary[K, V]
func (*SimpleMapSecondary[K, V]) Delete ¶ added in v0.5.0
func (s *SimpleMapSecondary[K, V]) Delete(key K) error
func (*SimpleMapSecondary[K, V]) Get ¶ added in v0.5.0
func (s *SimpleMapSecondary[K, V]) Get(key K) (value V, cost int64, expire int64, ok bool, err error)
func (*SimpleMapSecondary[K, V]) HandleAsyncError ¶ added in v0.5.0
func (s *SimpleMapSecondary[K, V]) HandleAsyncError(err error)
func (*SimpleMapSecondary[K, V]) Set ¶ added in v0.5.0
func (s *SimpleMapSecondary[K, V]) Set(key K, value V, cost int64, expire int64) error
func (*SimpleMapSecondary[K, V]) SetClock ¶ added in v0.5.0
func (s *SimpleMapSecondary[K, V]) SetClock(clock *clock.Clock)
type Slru ¶
type Slru[K comparable, V any] struct { // contains filtered or unexported fields }
type Store ¶
type Store[K comparable, V any] struct { // contains filtered or unexported fields }
func NewStore ¶
func NewStore[K comparable, V any]( maxsize int64, doorkeeper bool, entryPool bool, listener func(key K, value V, reason RemoveReason), cost func(v V) int64, secondaryCache SecondaryCache[K, V], workers int, probability float32, stringKeyFunc func(k K) string, ) *Store[K, V]
New returns a new data struct with the specified capacity
func (*Store[K, V]) DebugInfo ¶ added in v0.5.0
func (s *Store[K, V]) DebugInfo() debugInfo
used for test, only
func (*Store[K, V]) DeleteWithSecondary ¶ added in v0.3.0
func (*Store[K, V]) EstimatedSize ¶ added in v0.4.1
func (*Store[K, V]) GetWithSecodary ¶ added in v0.3.0
func (*Store[K, V]) RangeEntry ¶ added in v0.5.0
used in test
type StoreMeta ¶ added in v0.2.6
type StoreMeta struct {
Version uint64
StartNano int64
Sketch *CountMinSketch
}
type TimerWheel ¶
type TimerWheel[K comparable, V any] struct { // contains filtered or unexported fields }
func NewTimerWheel ¶
func NewTimerWheel[K comparable, V any](size uint) *TimerWheel[K, V]
type TinyLfu ¶
type TinyLfu[K comparable, V any] struct { // contains filtered or unexported fields }
func NewTinyLfu ¶
func NewTinyLfu[K comparable, V any](size uint, hasher *Hasher[K]) *TinyLfu[K, V]
func (*TinyLfu[K, V]) Access ¶
func (t *TinyLfu[K, V]) Access(item ReadBufItem[K, V])
func (*TinyLfu[K, V]) EvictEntries ¶
func (t *TinyLfu[K, V]) EvictEntries()
func (*TinyLfu[K, V]) UpdateCost ¶
type UnsignedCounter ¶ added in v0.4.1
type UnsignedCounter struct {
// contains filtered or unexported fields
}
A UnsignedCounter is a unsigned striped int64 counter.
Should be preferred over a single atomically updated int64 counter in high contention scenarios.
A Counter must not be copied after first use.
func NewUnsignedCounter ¶ added in v0.4.1
func NewUnsignedCounter() *UnsignedCounter
UnsignedCounter creates a new UnsignedCounter instance.
func (*UnsignedCounter) Add ¶ added in v0.4.1
func (c *UnsignedCounter) Add(delta uint64)
Add adds the delta to the counter.
func (*UnsignedCounter) Inc ¶ added in v0.4.1
func (c *UnsignedCounter) Inc()
Inc increments the counter by 1.
func (*UnsignedCounter) Reset ¶ added in v0.4.1
func (c *UnsignedCounter) Reset()
Reset resets the counter to zero. This method should only be used when it is known that there are no concurrent modifications of the counter.
func (*UnsignedCounter) Value ¶ added in v0.4.1
func (c *UnsignedCounter) Value() uint64
Value returns the current counter value. The returned value may not include all of the latest operations in presence of concurrent modifications of the counter.
type WriteBufItem ¶
type WriteBufItem[K comparable, V any] struct { // contains filtered or unexported fields }