Documentation
¶
Index ¶
- type Limiter
- func (l *Limiter[T]) TokenMap() map[T]float64
- func (l *Limiter[T]) TokenMapAt(at time.Time) map[T]float64
- func (l *Limiter[T]) Tokens(id T) float64
- func (l *Limiter[T]) TokensAt(id T, at time.Time) float64
- func (l *Limiter[T]) Use(id T, n float64) bool
- func (l *Limiter[T]) UseAt(id T, n float64, at time.Time) bool
- type LimiterSingle
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 ¶
TokenMap returns a map of the current tokens in each bucket.
Method is thread-safe.
func (*Limiter[T]) TokenMapAt ¶
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 ¶
Tokens returns the current number of tokens in the bucket with the given ID.
Method is thread-safe.
func (*Limiter[T]) TokensAt ¶
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 ¶
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 ¶
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.