Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter is a multi-tenant local rate limiter based on golang.org/x/time/rate. It requires a custom strategy in input, which is used to get the limit and burst settings for each tenant.
func NewRateLimiter ¶
func NewRateLimiter(strategy RateLimiterStrategy, recheckPeriod time.Duration) *RateLimiter
NewRateLimiter makes a new multi-tenant rate limiter. Each per-tenant limiter is configured using the input strategy and its limit/burst is rechecked (and reconfigured if changed) every recheckPeriod.
func (*RateLimiter) AllowN ¶
func (l *RateLimiter) AllowN(now time.Time, tenantID string, n int) (bool, Reservation)
AllowN reports whether n tokens may be consumed happen at time now. The reservation of tokens can be canceled using CancelAt on the returned object.
type RateLimiterStrategy ¶
RateLimiterStrategy defines the interface which a pluggable strategy should implement. The returned limit and burst can change over the time, and the local rate limiter will apply them every recheckPeriod.
type Reservation ¶
type Reservation interface { // CancelAt returns the reservation to the rate limiter for use by other // requests. Note that typically the reservation should be canceled with // the same timestamp it was requested with, or not all the tokens // consumed will be returned. CancelAt(now time.Time) }
Reservation is similar to rate.Reservation but excludes interfaces which do not make sense to expose, because we are following the semantics of AllowN, being an immediate reservation, i.e. not delayed into the future.