Documentation
¶
Overview ¶
Package rate provides a rate limiter.
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
}
A Limiter controls how frequently events are allowed to happen. It implements a "token bucket" of size b, initially full and refilled at rate r tokens per second.
Informally, in any large enough time interval, the Limiter limits the rate to r tokens per second, with a maximum burst size of b events.
Limiter is thread-safe.
func NewLimiter ¶
NewLimiter returns a new Limiter that allows events up to rate r and permits bursts of at most b tokens.
func NewLimiterWithCustomTime ¶
func NewLimiterWithCustomTime( r float64, b float64, nowFn func() time.Time, sleepFn func(d time.Duration), ) *Limiter
NewLimiterWithCustomTime returns a new Limiter that allows events up to rate r and permits bursts of at most b tokens. The limiter uses the given functions to retrieve the current time and to sleep (useful for testing).