ratelimit

package
v0.0.0-...-7b8bf0e Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2015 License: Apache-2.0 Imports: 9 Imported by: 1

Documentation

Overview

Tokenbucket based request rate limiter

Index

Constants

View Source
const DefaultCapacity = 65536
View Source
const UndefinedDelay = -1

Variables

This section is empty.

Functions

This section is empty.

Types

type MaxRateError

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

func (*MaxRateError) Error

func (m *MaxRateError) Error() string

type RateErrHandler

type RateErrHandler struct {
}

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)
}

type RateExtractorFunc

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

func (RateExtractorFunc) Extract

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

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 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)

type TokenLimiterOption

type TokenLimiterOption func(l *TokenLimiter) error

func Capacity

func Capacity(cap int) TokenLimiterOption

func ErrorHandler

func ErrorHandler(h utils.ErrorHandler) TokenLimiterOption

ErrorHandler sets error handler of the server

func ExtractRates

func ExtractRates(e RateExtractor) TokenLimiterOption

func Logger

Logger sets the logger that will be used by this middleware.

Jump to

Keyboard shortcuts

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