Documentation
¶
Overview ¶
Package rate provides a rate limiter.
Index ¶
Constants ¶
const Inf = Limit(math.MaxFloat64)
Inf is the infinite rate limit; it allows all events (even if burst is zero).
const InfDuration = time.Duration(1<<63 - 1)
InfDuration is the duration returned by Delay when a Reservation is not OK.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Limit ¶
type Limit float64
Limit defines the maximum frequency of some events. Limit is represented as number of events per second. A zero Limit allows no events.
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. As a special case, if r == Inf (the infinite rate), b is ignored. See https://en.wikipedia.org/wiki/Token_bucket for more about token buckets.
The zero value is a valid Limiter, but it will reject all events. Use NewLimiter to create non-zero Limiters.
Limiter has one main methods, Allow. Allow consumes a single token. If no token is available, Allow returns false.
func NewLimiter ¶
NewLimiter returns a new Limiter that allows events up to rate r and permits bursts of at most b tokens. NOTE: the maximum burst we can represent is a little over 100k tokens; bursts greater than that will be truncated.