ratelimit

package
v0.9.91 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2017 License: Apache-2.0, MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Header is
	Header = "X-Rate-Limit"
	// LocalRatelimitName is the name of the LocalRatelimit filter, which will be shown in log
	LocalRatelimitName = "localRatelimit"
	// DisableRatelimitName is the name of the DisableRatelimit, which will be shown in log
	DisableRatelimitName = "disableRatelimit"
)
View Source
const (
	DefaultMaxhits       = 20
	DefaultTimeWindow    = 1 * time.Second
	DefaultCleanInterval = 60 * time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthLookuper

type AuthLookuper struct{}

func NewAuthLookuper

func NewAuthLookuper() AuthLookuper

func (AuthLookuper) Lookup

func (_ AuthLookuper) Lookup(req *http.Request) string

type Lookuper

type Lookuper interface {
	// Lookup is used to get the string which is used to define
	// how the bucket of a ratelimiter looks like, which is used
	// to decide to ratelimit or not. For example you can use the
	// X-Forwarded-For Header if you want to rate limit based on
	// source ip behind a proxy/loadbalancer or the Authorization
	// Header for request per token or user.
	Lookup(*http.Request) string
}

Lookuper makes it possible to be more flexible for ratelimiting.

type Ratelimit

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

Ratelimit is a proxy objects that delegates to implemetations and stores settings for the ratelimiter

func (*Ratelimit) Allow

func (l *Ratelimit) Allow(s string) bool

Allow returns true if the s is not ratelimited, false if it is ratelimited

func (*Ratelimit) Close

func (l *Ratelimit) Close()

Close will stop a cleanup goroutines in underlying implementation.

type Registry

type Registry struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Registry objects hold the active ratelimiters, ensure synchronized access to them, apply default settings and recycle the idle ratelimiters.

func NewRegistry

func NewRegistry(settings ...Settings) *Registry

NewRegistry initializes a registry with the provided default settings.

func (*Registry) Check

func (r *Registry) Check(req *http.Request) (Settings, bool)

Check returns Settings used and false in case of request is ratelimitted and false otherwise.

func (*Registry) Get

func (r *Registry) Get(s Settings) *Ratelimit

Get returns a Ratelimit instance for provided Settings

type Settings

type Settings struct {
	Type          Type
	Lookuper      Lookuper
	Host          string
	MaxHits       int
	TimeWindow    time.Duration
	CleanInterval time.Duration
}

Settings configures the chosen rate limiter

func (Settings) Empty

func (s Settings) Empty() bool

func (Settings) String

func (s Settings) String() string

type Type

type Type int

Type defines the type of the used breaker: consecutive, rate or disabled.

const (
	// NoRatelimit is not used
	NoRatelimit Type = iota
	// LocalRatelimit is used to have a simple local rate limit,
	// which is calculated and measured within each instance
	LocalRatelimit
	// DisableRatelimit is used to disable rate limit
	DisableRatelimit
)

type XForwardedForLookuper

type XForwardedForLookuper struct{}

func NewXForwardedForLookuper

func NewXForwardedForLookuper() XForwardedForLookuper

func (XForwardedForLookuper) Lookup

func (_ XForwardedForLookuper) Lookup(req *http.Request) string

Jump to

Keyboard shortcuts

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