Help us shape the future of Go! Take the 2026 Go Developer Survey

limit

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Unknown means not initialized state.
	Unknown = iota
	// Allowed means allowed state.
	Allowed
	// HitQuota means this request exactly hit the quota.
	HitQuota
	// OverQuota means passed the quota.
	OverQuota
)

Variables

View Source
var (
	// ErrUnknownCode is an error that represents unknown status code.
	ErrUnknownCode = errors.New("unknown status code")
)

Functions

This section is empty.

Types

type PeriodLimit

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

A PeriodLimit is used to limit requests during a period of time.

func NewPeriodLimit

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

NewPeriodLimit returns a PeriodLimit with given parameters.

func (*PeriodLimit) Take

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

Take requests a permit, it returns the permit state.

func (*PeriodLimit) TakeCtx

func (h *PeriodLimit) TakeCtx(ctx context.Context, key string) (int, error)

TakeCtx requests a permit with context, it returns the permit state.

type PeriodOption

type PeriodOption func(l *PeriodLimit)

PeriodOption defines the method to customize a PeriodLimit.

func Align

func Align() PeriodOption

Align returns a func to customize a PeriodLimit with alignment. For example, if we want to limit end users with 5 sms verification messages every day, we need to align with the local timezone and the start of the day.

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) AllowCtx

func (lim *TokenLimiter) AllowCtx(ctx context.Context) bool

AllowCtx is shorthand for AllowNCtx(ctx,time.Now(), 1) with incoming context.

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. Otherwise, use Reserve or Wait.

func (*TokenLimiter) AllowNCtx

func (lim *TokenLimiter) AllowNCtx(ctx context.Context, now time.Time, n int) bool

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

Jump to

Keyboard shortcuts

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