slidingwindow

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2023 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Counter

type Counter[T comparable] struct {
	// contains filtered or unexported fields
}

Counter is a sliding window-based counter. The main interaction with Counter is through its Observe method, which records one observation of a value. Counter has a size, which limits how many observations are kept: if the size is 10, then the 11th observation will replace the 1st observation from the Counter.

Counter is not safe for concurrent use. If you want to share it across multiple goroutines, use LockedCounter instead. In this case, the cardinalityHint should be set to a sufficiently large value, to minimize time spent in Observe holding the mutex. See NewCounter for more details on cardinalityHint.

T may be any comparable type, but consider keeping its size small: ints, constant strings, and small structs at or under 16 bytes are fine. Dynamically allocated strings may be kept around for longer than desired, since the window will keep the string alive. Floats have strange equality properties (namely with NaN and epsilon), so consider using a fixed-point representation of the number instead. See https://github.com/golang/go/issues/56351.

Counter is not intended for use in calculating averages, although that can be implemented with the GetAll method.

func NewCounter

func NewCounter[T comparable](
	size int, cardinalityHint int, onEvict func(T),
) *Counter[T]

NewCounter creates a new sliding window-based counter with the given size. NewCounter is not safe for concurrent use.

If onEvict is not nil, then when the last occurrence of a previously observed value is evicted from the window, onEvict will be called with the evicted value. onEvict will run in the same goroutine that called Observe, so consider doing any long-running work in another goroutine.

The cardinalityHint is your guess of how many distinct values will ever be seen by Counter. If you are not sure, pass 0 and NewCounter will guess for you. This hint is used to size the internal map of values to counts. If cardinalityHint is too small, more time will be spent resizing the map and resolving hash collisions. If cardinalityHint is too large, more memory will be used for no performance gain.

func (*Counter[T]) Get

func (c *Counter[T]) Get(value T) int

Get returns the value's count, which may be 0, but never larger than the window size.

func (*Counter[T]) GetAll

func (c *Counter[T]) GetAll() map[T]int

GetAll returns a map of all observed values in the window to their counts.

func (*Counter[T]) Lifetime

func (c *Counter[T]) Lifetime() int

Lifetime returns the lifetime count of observations.

func (*Counter[T]) Observe

func (c *Counter[T]) Observe(value T)

Observe makes an observation of a value.

func (*Counter[T]) String

func (c *Counter[T]) String() string

type LockedCounter

type LockedCounter[T comparable] struct {
	// contains filtered or unexported fields
}

LockedCounter is a wrapper for Counter that takes a lock before any method is called.

func NewLocked

func NewLocked[T comparable](
	counter *Counter[T],
) *LockedCounter[T]

NewLocked wraps a Counter, making it thread-safe. Do not retain the *Counter that is passed in.

func (*LockedCounter[T]) Get

func (lc *LockedCounter[T]) Get(value T) int

func (*LockedCounter[T]) GetAll

func (lc *LockedCounter[T]) GetAll() map[T]int

func (*LockedCounter[T]) Lifetime

func (lc *LockedCounter[T]) Lifetime() int

func (*LockedCounter[T]) Observe

func (lc *LockedCounter[T]) Observe(value T)

func (*LockedCounter[T]) String

func (lc *LockedCounter[T]) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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