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

ratelimit

package
v1.21.2 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package ratelimit grants a limited number of permits per key and period. The count lives in Redis, so every server instance draws on the same quota. It throttles sending verification codes, registrations per client IP and ticket creation.

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.Client, keyPrefix string,
	opts ...PeriodOption) *PeriodLimit

NewPeriodLimit returns a PeriodLimit with given parameters.

func (*PeriodLimit) ParsePermitState

func (h *PeriodLimit) ParsePermitState(code int) bool

ParsePermitState returns the permit state by the code.

func (*PeriodLimit) Take

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

Take requests a permit for key and returns the permit state; ctx bounds the Redis call.

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.

Jump to

Keyboard shortcuts

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