slidingwindow

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidInterval       = errors.New("interval must be non-zero positive duration value")
	ErrInvalidSegmentsTooLow = errors.New("segments value is too low")
	ErrInvalidLimitTooLow    = errors.New("maxEvents must be non-zero positive integer value")
)

Functions

func NewAlgorithm

func NewAlgorithm(maxEvents uint32, interval time.Duration, segments uint32, options ...Option) (rate.LimiterAlgorithm, error)

NewAlgorithm returns rate.LimiterAlgorithm with Sliding Window Counter implementation that's based on Fixed Window and Sliding Window algorithms to make a perfect balance between memory consumption and accuracy. The higher value of segments means better accuracy but also more memory consumption. The minimum value of segments is 2 and there is no check for maximum, however too big value can lead to inaccurate results so test the values for yourself to find the best accuracy to your usecase since it can vary depending on interval value. Segments are fixed-length time slots of value `interval/segments`. Each has an internal counter. The last segment is the current one which is incrementing, after segment duration, it's shifted to the back of 1 segment slot so if the segment duration passes, the last has 0 value, and the oldest is replaced by a counter at 1 index e.g [1, 2, 3, 4, 5] => [2, 3, 4, 5, 0]. It's recommended to use for frequent events in relatively small interval like web API rate limitation.

Types

type Option

type Option func(o *swOptions)

func WithClock

func WithClock(c rate.Clock) Option

WithClock returns an Option to modify clock used in time retrieval.

Jump to

Keyboard shortcuts

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