limit

package
v1.10.32 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2021 License: MIT Imports: 9 Imported by: 2

Documentation

Index

Constants

View Source
const (
	Unknown = iota
	Allowed
	HitQuota
	OverQuota
)

Variables

View Source
var ErrUnknownCode = errors.New("unknown status code")

Functions

This section is empty.

Types

type LimitOption

type LimitOption func(l *PeriodLimit)

func Align

func Align() LimitOption

type PeriodLimit

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

func NewPeriodLimit

func NewPeriodLimit(period, quota int, limitStore *redis.Redis, keyPrefix string,
	opts ...LimitOption) *PeriodLimit

func (*PeriodLimit) Take

func (h *PeriodLimit) Take(key string) (int, error)

type TokenLimiter

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

A TokenLimiter controls how frequently events are allowed to happen with in one second.

func NewTokenLimiter

func NewTokenLimiter(rate, burst int, store *redis.Redis, key string) *TokenLimiter

NewTokenLimiter returns a new TokenLimiter that allows events up to rate and permits bursts of at most burst tokens.

func (*TokenLimiter) Allow

func (lim *TokenLimiter) Allow() bool

Allow is shorthand for AllowN(time.Now(), 1).

func (*TokenLimiter) AllowN

func (lim *TokenLimiter) AllowN(now time.Time, n int) bool

AllowN reports whether n events may happen at time now. Use this method if you intend to drop / skip events that exceed the rate rate. Otherwise use Reserve or Wait.

Jump to

Keyboard shortcuts

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