limiter

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2019 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Type defines the type of the Limiter
	Type Type
	// Address defines the Redis server address
	Address string
	// RateLimit defines the rate limit in queries per Interval
	RateLimit float64
	// BurstLimit defines the burst limit or bucket size of the Limiter
	BurstLimit int
	// Interval defines the token refresh rate of RateLimit tokens per Interval
	Interval time.Duration
	// FailOpen determines if Allow should return true on Redis server errors
	FailOpen bool
}

Config defines a struct passed to New to configure a Limiter

type Limiter

type Limiter interface {
	// Allow returns true if an event may happen for the given ID
	Allow(id string) bool

	// AllowN returns true if the given number of events may happen for the
	// given ID
	AllowN(id string, n int) bool

	// AllowDynamic returns true if an event may happen for the given ID taking
	// into consideration the given rate and burst limits
	AllowDynamic(id string, rate float64, burst int) bool

	// AllowNDynamic returns true if the given number of events may happen for
	// the given ID taking into consideration the given rate and burst limits
	AllowNDynamic(id string, n int, rate float64, burst int) bool

	// Rate returns the default rate limit
	Rate() float64

	// Burst returns the default burst limit
	Burst() int
}

Limiter defines a rate limiter interface

func New

func New(config Config) Limiter

New creates a new redis limiter and returns an error if redis is not available at the configured redis address

type Type

type Type int
const (
	TypeRedis Type = iota << 1
	TypeInMemory
	TypeDisabled
)

Jump to

Keyboard shortcuts

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