limiter

package
v1.8.0-rc.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 8, 2021 License: Apache-2.0 Imports: 3 Imported by: 7

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.

func (*RateLimiter) Burst

func (l *RateLimiter) Burst(now time.Time, tenantID string) int

Burst returns the currently configured maximum burst size.

func (*RateLimiter) Limit

func (l *RateLimiter) Limit(now time.Time, tenantID string) float64

Limit returns the currently configured maximum overall tokens rate.

type RateLimiterStrategy

type RateLimiterStrategy interface {
	Limit(tenantID string) float64
	Burst(tenantID string) int
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL