rate

package
v0.55.1 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2025 License: AGPL-3.0 Imports: 2 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[T comparable] struct {
	// contains filtered or unexported fields
}

Limiter is a simple token bucket rate limiter. https://en.wikipedia.org/wiki/Token_bucket

func NewLimiter

func NewLimiter[T comparable](tokensPerSecond, burstSize float64) *Limiter[T]

NewLimiter creates a new Limiter. The tokensPerSecond is the rate of tokens to add to the bucket per second. The burstSize is the maximum number of tokens in the bucket. The T type is the type of the keys used to identify the buckets. Full buckets are removed over time to prevent memory leaks.

func (*Limiter[T]) TokenMap

func (l *Limiter[T]) TokenMap() map[T]float64

TokenMap returns a map of the current tokens in each bucket.

Method is thread-safe.

func (*Limiter[T]) TokenMapAt

func (l *Limiter[T]) TokenMapAt(at time.Time) map[T]float64

TokenMapAt returns a map of the current tokens in each bucket.

The at time must be greater or equal to the last event time of the bucket. Otherwise, the number of tokens in the bucket will be 0.

Do not use this method with time.Now(), use TokenMap instead. Otherwise, there is no guarantee that consecutive calls will be executed with increasing time in multi-threaded environments.

Method is thread-safe.

func (*Limiter[T]) Tokens

func (l *Limiter[T]) Tokens(id T) float64

Tokens returns the current number of tokens in the bucket with the given ID.

Method is thread-safe.

func (*Limiter[T]) TokensAt

func (l *Limiter[T]) TokensAt(id T, at time.Time) float64

TokensAt returns the current number of tokens in the bucket with the given ID.

The at time must be greater or equal to the last event time of the bucket. Otherwise, the function will return 0.

Do not use this method with time.Now(), use Tokens instead. Otherwise, there is no guarantee that consecutive calls will be executed with increasing time in multi-threaded environments.

Method is thread-safe.

func (*Limiter[T]) Use

func (l *Limiter[T]) Use(id T, n float64) bool

Use returns true if n tokens can be used from the bucket with the given ID and deducts n tokens from the bucket.

Method is thread-safe.

func (*Limiter[T]) UseAt

func (l *Limiter[T]) UseAt(id T, n float64, at time.Time) bool

UseAt returns true if n tokens can be used from the bucket with the given ID and deducts n tokens from the bucket.

The at time must be greater or equal to the last event time of the bucket. Otherwise, the function will return false.

Do not use this method with time.Now(), use Use instead. Otherwise, there is no guarantee that consecutive calls will be executed with increasing time in multi-threaded environments.

Method is thread-safe.

type LimiterSingle

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

LimiterSingle is a simple token bucket rate limiter for a single bucket. https://en.wikipedia.org/wiki/Token_bucket

func NewLimiterSingle

func NewLimiterSingle(tokensPerSecond, burstSize float64) *LimiterSingle

NewLimiterSingle creates a new LimiterSingle.

func (*LimiterSingle) Tokens

func (l *LimiterSingle) Tokens() float64

Tokens returns the current number of tokens in the bucket.

Method is thread-safe.

func (*LimiterSingle) TokensAt

func (l *LimiterSingle) TokensAt(at time.Time) float64

TokensAt returns the current number of tokens in the bucket.

The at time must be greater or equal to the last event time of the bucket. Otherwise, the function will return 0.

Do not use this method with time.Now(), use Tokens instead. Otherwise, there is no guarantee that consecutive calls will be executed with increasing time in multi-threaded environments.

Method is thread-safe.

func (*LimiterSingle) Use

func (l *LimiterSingle) Use(n float64) bool

Use returns true if n tokens can be used from the bucket and deducts n tokens from the bucket.

Method is thread-safe.

func (*LimiterSingle) UseAt

func (l *LimiterSingle) UseAt(n float64, at time.Time) bool

UseAt returns true if n tokens can be used from the bucket and deducts n tokens from the bucket.

The at time must be greater or equal to the last event time of the bucket. Otherwise, the function will return false.

Do not use this method with time.Now(), use Use instead. Otherwise, there is no guarantee that consecutive calls will be executed with increasing time in multi-threaded environments.

Method is thread-safe.

Jump to

Keyboard shortcuts

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