tokenbucket

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2014 License: MIT Imports: 13 Imported by: 0

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 ConfigMapperFn

type ConfigMapperFn func(r request.Request) (*RateSet, error)

ConfigMapperFn is a mapper function that is used by the `TokenLimiter` middleware to retrieve `RateSet` from HTTP requests.

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 NewLimiter

func NewLimiter(defaultRates *RateSet, capacity int, mapper limit.MapperFn, configMapper ConfigMapperFn, clock timetools.TimeProvider) (*TokenLimiter, error)

NewLimiter constructs a `TokenLimiter` middleware instance.

func (*TokenLimiter) DefaultRates

func (tl *TokenLimiter) DefaultRates() *RateSet

DefaultRates returns the default rate set of the limiter. The only reason to Provide this method is to facilitate testing.

func (*TokenLimiter) ProcessRequest

func (tl *TokenLimiter) ProcessRequest(r request.Request) (*http.Response, error)

func (*TokenLimiter) ProcessResponse

func (tl *TokenLimiter) ProcessResponse(r request.Request, a request.Attempt)

Jump to

Keyboard shortcuts

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