oxy: github.com/vulcand/oxy/ratelimit Index | Files

package ratelimit

import "github.com/vulcand/oxy/ratelimit"

Package ratelimit Tokenbucket based request rate limiter

Index

Package Files

bucket.go bucketset.go tokenlimiter.go

Constants

const DefaultCapacity = 65536

DefaultCapacity default capacity

const UndefinedDelay = -1

UndefinedDelay default delay

type MaxRateError Uses

type MaxRateError struct {
    // contains filtered or unexported fields
}

MaxRateError max rate error

func (*MaxRateError) Error Uses

func (m *MaxRateError) Error() string

type RateErrHandler Uses

type RateErrHandler struct{}

RateErrHandler error handler

func (*RateErrHandler) ServeHTTP Uses

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

type RateExtractor Uses

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

RateExtractor rate extractor

type RateExtractorFunc Uses

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

RateExtractorFunc rate extractor function type

func (RateExtractorFunc) Extract Uses

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

Extract extract from request

type RateSet Uses

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 Uses

func NewRateSet() *RateSet

NewRateSet crates an empty `RateSet` instance.

func (*RateSet) Add Uses

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 Uses

func (rs *RateSet) String() string

type TokenBucketSet Uses

type TokenBucketSet struct {
    // contains filtered or unexported fields
}

TokenBucketSet represents a set of TokenBucket covering different time periods.

func NewTokenBucketSet Uses

func NewTokenBucketSet(rates *RateSet, clock timetools.TimeProvider) *TokenBucketSet

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

func (*TokenBucketSet) Consume Uses

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

Consume consume tokens

func (*TokenBucketSet) GetMaxPeriod Uses

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

GetMaxPeriod returns the max period

func (*TokenBucketSet) Update Uses

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

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

type TokenLimiter Uses

type TokenLimiter struct {
    // contains filtered or unexported fields
}

TokenLimiter implements rate limiting middleware.

func New Uses

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

New constructs a `TokenLimiter` middleware instance.

func (*TokenLimiter) ServeHTTP Uses

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

func (*TokenLimiter) Wrap Uses

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

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

type TokenLimiterOption Uses

type TokenLimiterOption func(l *TokenLimiter) error

TokenLimiterOption token limiter option type

func Capacity Uses

func Capacity(cap int) TokenLimiterOption

Capacity sets the capacity

func Clock Uses

func Clock(clock timetools.TimeProvider) TokenLimiterOption

Clock sets the clock

func ErrorHandler Uses

func ErrorHandler(h utils.ErrorHandler) TokenLimiterOption

ErrorHandler sets error handler of the server

func ExtractRates Uses

func ExtractRates(e RateExtractor) TokenLimiterOption

ExtractRates sets the rate extractor

func Logger Uses

func Logger(l *log.Logger) TokenLimiterOption

Logger defines the logger the token limiter will use.

It defaults to logrus.StandardLogger(), the global logger used by logrus.

Package ratelimit imports 10 packages (graph) and is imported by 28 packages. Updated 2018-07-09. Refresh now. Tools for package owners.