slidingwindow

package
v0.0.0-...-4973d3c Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2024 License: AGPL-3.0 Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limiter

type Limiter struct {
	// contains filtered or unexported fields
}

LocalWindow represents a window that ignores sync behavior entirely and only stores counters in memory.

func NewLimiter

func NewLimiter(interval time.Duration, max int64) Limiter

NewLimiter returns a new Limiter that limits events to max events per interval duration.

func (*Limiter) Allow

func (lim *Limiter) Allow() (bool, time.Duration)

Allow checks if the rate limit would be exceeded by calling add. If the limit would be exceeded, Allow returns false and the remaining duration until the next event can happen. If the event can happen immediately, Allow calls add to increment the count and returns true.

func (*Limiter) AllowForce

func (lim *Limiter) AllowForce() bool

AllowForce unconditionally increments the rate limiter count and returns true, without checking if the rate limit would be exceeded. This allows forcing an event through even if the rate limit has been reached.

func (*Limiter) Check

func (lim *Limiter) Check() (bool, time.Duration)

Check returns whether the rate limit would be exceeded if an event is added now. It returns a bool indicating if the limit would be exceeded, and a time.Duration for the remaining time until the next event can happen without exceeding the rate limit.

func (*Limiter) Interval

func (lim *Limiter) Interval() time.Duration

Interval returns the interval duration configured for the rate limiter.

func (*Limiter) WaitTill

func (lim *Limiter) WaitTill(now time.Time)

WaitTill sets the last time to the given time. This overrides the rate limiting and forces the last time to be the given time.

Jump to

Keyboard shortcuts

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