staleness

package
v0.99.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NowFunc = time.Now

We override how Now() is returned, so we can have deterministic tests

Functions

This section is empty.

Types

type PriorityQueue

type PriorityQueue interface {
	// Update will add or update an entry, and reshuffle the queue internally as needed to keep it sorted
	Update(id identity.Stream, newPrio time.Time)
	// Peek will return the entry at the HEAD of the queue *without* removing it from the queue
	Peek() (identity.Stream, time.Time)
	// Pop will remove the entry at the HEAD of the queue and return it
	Pop() (identity.Stream, time.Time)
	// Len will return the number of entries in the queue
	Len() int
}

PriorityQueue represents a way to store entries sorted by their priority. Pop() will return the oldest entry of the set.

func NewPriorityQueue

func NewPriorityQueue() PriorityQueue

type Staleness

type Staleness[T any] struct {
	Max time.Duration
	// contains filtered or unexported fields
}

Staleness a a wrapper over a map that adds an additional "staleness" value to each entry. Users can call ExpireOldEntries() to automatically remove all entries from the map whole staleness value is older than the `max`

NOTE: Staleness methods are *not* thread-safe. If the user needs to use Staleness in a multi-threaded environment, then it is the user's responsibility to properly serialize calls to Staleness methods

func NewStaleness

func NewStaleness[T any](max time.Duration, items streams.Map[T]) *Staleness[T]

func (*Staleness[T]) Delete added in v0.97.0

func (s *Staleness[T]) Delete(id identity.Stream)

func (*Staleness[T]) Evict added in v0.97.0

func (s *Staleness[T]) Evict() identity.Stream

func (*Staleness[T]) ExpireOldEntries

func (s *Staleness[T]) ExpireOldEntries()

ExpireOldEntries will remove all entries whose staleness value is older than `now() - max` For example, if an entry has a staleness value of two hours ago, and max == 1 hour, then the entry would be removed. But if an entry had a stalness value of 30 minutes, then it *wouldn't* be removed.

func (*Staleness[T]) Items

func (s *Staleness[T]) Items() func(yield func(identity.Stream, T) bool) bool

Items returns an iterator function that in future go version can be used with range See: https://go.dev/wiki/RangefuncExperiment

func (*Staleness[T]) Len added in v0.97.0

func (s *Staleness[T]) Len() int

func (*Staleness[T]) Load

func (s *Staleness[T]) Load(id identity.Stream) (T, bool)

Load the value at key. If it does not exist, the boolean will be false and the value returned will be the zero value

func (*Staleness[T]) Next added in v0.97.0

func (s *Staleness[T]) Next() time.Time

func (*Staleness[T]) Store

func (s *Staleness[T]) Store(id identity.Stream, v T) error

Store the given key value pair in the map, and update the pair's staleness value to "now"

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL