tokenbucket

package
v0.0.0-...-a5469ec Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2015 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Tokenbucket based request rate limiter

Index

Constants

View Source
const DefaultCapacity = 65536

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	Rate         Rate  // Average allowed rate
	Burst        int64 // Burst size
	Capacity     int   // Overall capacity (maximum sumultaneuously active tokens)
	Mapper       limit.MapperFn
	TimeProvider timetools.TimeProvider
}

type Rate

type Rate struct {
	Units  int64
	Period time.Duration
}

type TokenBucket

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

Implements token bucket rate limiting algorithm (http://en.wikipedia.org/wiki/Token_bucket) and is used by rate limiters to implement various rate limiting strategies

func NewTokenBucket

func NewTokenBucket(rate Rate, maxTokens int64, timeProvider timetools.TimeProvider) (*TokenBucket, error)

func (*TokenBucket) Consume

func (tb *TokenBucket) Consume(tokens int64) (time.Duration, error)

In case if there's enough tokens, consumes tokens and returns 0, nil In case if tokens to consume is larger than max burst returns -1, error In case if there's not enough tokens, returns time to wait till refill

type TokenLimiter

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

func NewTokenLimiter

func NewTokenLimiter(mapper limit.MapperFn, rate Rate) (*TokenLimiter, error)

func NewTokenLimiterWithOptions

func NewTokenLimiterWithOptions(mapper limit.MapperFn, rate Rate, o Options) (*TokenLimiter, error)

func (*TokenLimiter) GetBurst

func (tl *TokenLimiter) GetBurst() int64

func (*TokenLimiter) GetCapacity

func (tl *TokenLimiter) GetCapacity() int

func (*TokenLimiter) GetRate

func (tl *TokenLimiter) GetRate() Rate

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