v2.0.0-alpha.2 Latest Latest

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

Go to latest
Published: Jan 7, 2021 License: Apache-2.0 Imports: 9 Imported by: 0



Package ratelimit Tokenbucket based request rate limiter



View Source
const DefaultCapacity = 65536

DefaultCapacity default capacity

View Source
const UndefinedDelay = -1

UndefinedDelay default delay


This section is empty.


This section is empty.


type MaxRateError

type MaxRateError struct {
	Delay time.Duration

MaxRateError max rate error

func (*MaxRateError) Error

func (m *MaxRateError) Error() string

type RateErrHandler

type RateErrHandler struct{}

RateErrHandler error handler

func (*RateErrHandler) ServeHTTP

func (e *RateErrHandler) ServeHTTP(w http.ResponseWriter, req *http.Request, err error)

type RateExtractor

type RateExtractor interface {
	Extract(r *http.Request) (*RateSet, error)

RateExtractor rate extractor

type RateExtractorFunc

type RateExtractorFunc func(r *http.Request) (*RateSet, error)

RateExtractorFunc rate extractor function type

func (RateExtractorFunc) Extract

func (e RateExtractorFunc) Extract(r *http.Request) (*RateSet, error)

Extract extract from request

type RateSet

type RateSet struct {
	// contains filtered or unexported fields

RateSet maintains a set of rates. It can contain only one rate per period at a time.

func NewRateSet

func NewRateSet() *RateSet

NewRateSet crates an empty `RateSet` instance.

func (*RateSet) Add

func (rs *RateSet) Add(period time.Duration, average int64, burst int64) error

Add adds a rate to the set. If there is a rate with the same period in the set then the new rate overrides the old one.

func (*RateSet) String

func (rs *RateSet) String() string

type TokenBucketSet

type TokenBucketSet struct {
	// contains filtered or unexported fields

TokenBucketSet represents a set of TokenBucket covering different time periods.

func NewTokenBucketSet

func NewTokenBucketSet(rates *RateSet) *TokenBucketSet

NewTokenBucketSet creates a `TokenBucketSet` from the specified `rates`.

func (*TokenBucketSet) Consume

func (tbs *TokenBucketSet) Consume(tokens int64) (time.Duration, error)

Consume consume tokens

func (*TokenBucketSet) GetMaxPeriod

func (tbs *TokenBucketSet) GetMaxPeriod() time.Duration

GetMaxPeriod returns the max period

func (*TokenBucketSet) Update

func (tbs *TokenBucketSet) Update(rates *RateSet)

Update brings the buckets in the set in accordance with the provided `rates`.

type TokenLimiter

type TokenLimiter struct {
	// contains filtered or unexported fields

TokenLimiter implements rate limiting middleware.

func New

func New(next http.Handler, extract utils.SourceExtractor, defaultRates *RateSet, opts ...TokenLimiterOption) (*TokenLimiter, error)

New constructs a `TokenLimiter` middleware instance.

func (*TokenLimiter) ServeHTTP

func (tl *TokenLimiter) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*TokenLimiter) Wrap

func (tl *TokenLimiter) Wrap(next http.Handler)

Wrap sets the next handler to be called by token limiter handler.

type TokenLimiterOption

type TokenLimiterOption func(l *TokenLimiter) error

TokenLimiterOption token limiter option type

func Capacity

func Capacity(cap int) TokenLimiterOption

Capacity sets the capacity

func ErrorHandler

func ErrorHandler(h utils.ErrorHandler) TokenLimiterOption

ErrorHandler sets error handler of the server

func ExtractRates

func ExtractRates(e RateExtractor) TokenLimiterOption

ExtractRates sets the rate extractor

func Logger

Logger defines the logger the token limiter will use.

Jump to

Keyboard shortcuts

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