Documentation ¶
Overview ¶
Package ratelimit Tokenbucket based request rate limiter
Index ¶
Constants ¶
const DefaultCapacity = 65536
DefaultCapacity default capacity
const UndefinedDelay = -1
UndefinedDelay default delay
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MaxRateError ¶
type MaxRateError struct {
// contains filtered or unexported fields
}
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 ¶
RateExtractor rate extractor
type RateExtractorFunc ¶
RateExtractorFunc rate extractor function type
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.
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, clock timetools.TimeProvider) *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 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 ¶
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.