Documentation ¶
Overview ¶
Package rate provides a rate limiter.
Index ¶
Constants ¶
const ( // Inf is the infinite rate limit; it allows all events (even if burst is zero). Inf = Limit(math.MaxFloat64) // InfDuration is the duration returned by Delay when a Reservation is not OK. // InfDuration - это продолжительность, возвращаемая с задержкой, когда Reservation не в порядке. InfDuration = time.Duration(1<<63 - 1) )
Variables ¶
var TestBucketsFactory = func() irates.IBuckets { return Provide(coreutils.TestTimeFunc) }
Functions ¶
func BucketStateIsZero ¶
func BucketStateIsZero(state *irates.BucketState) bool
является ли состояние bucket'а неопределенным (нулевым) is the bucket's state undefined (zero)
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 three main methods, Allow, Reserve, and Wait. Most callers should use Wait.
Each of the three methods consumes a single token. They differ in their behavior when no token is available. If no token is available, Allow returns false. If no token is available, Reserve returns a reservation for a future token and the amount of time the caller must wait before using it. If no token is available, Wait blocks until one can be obtained or its associated context.Context is canceled.
The methods AllowN, ReserveN, and WaitN consume n tokens.
type Reservation ¶
type Reservation struct {
// contains filtered or unexported fields
}
A Reservation holds information about events that are permitted by a Limiter to happen after a delay. A Reservation may be canceled, which may enable the Limiter to permit additional events. Reservation содержит информацию о событиях, которые разрешены Limiter после задержки. Reservation может быть отменено, что может позволить Limiter разрешить дополнительные мероприятия