ratelimit

package
v1.16.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ReasonCooldown      = "cooldown"
	ReasonLimitExceeded = "limit_exceeded"
)

Variables

This section is empty.

Functions

func Remaining

func Remaining(limit int, used int64) int

Remaining is the non-negative budget left after used requests. used is int64 because the Redis limiter reads it straight from a Lua reply; it is clamped before any narrowing so an out-of-range count cannot wrap.

func ValidateLimits

func ValidateLimits(limits map[string]Limit) error

ValidateLimits rejects policies that the millisecond-based backends cannot enforce identically. Disabling a limiter is an explicit HTTP configuration.

Types

type Limit

type Limit struct {
	Limit    int
	Window   time.Duration
	Cooldown time.Duration
}

Limit configures a named rate-limit bucket: at most Limit requests per Window, with an optional Cooldown between accepted requests. It is the single shared limit type consumed by the PostgreSQL and Redis limiters and the HTTP layer.

func LookupLimit

func LookupLimit(limits map[string]Limit, bucket string) (Limit, bool)

LookupLimit resolves the Limit for a bucket from a limits map: the bucket's own entry, else the "default" entry, else a conservative built-in fallback (found is false only for that fallback). Shared by the PostgreSQL and Redis limiters.

type Limiter added in v1.1.0

type Limiter interface {
	Allow(ctx context.Context, bucket, key string) (Result, error)
}

Limiter spends one request of key's budget in bucket, in a store every replica shares. An error means no store could decide, and the request is refused: no failure lifts a budget or counts it per process.

type Result

type Result struct {
	Allowed    bool
	RetryAfter time.Duration
	Reason     string
	Limit      int
	Remaining  int
	Window     time.Duration
	Cooldown   time.Duration
}

Result is one decision. A refusal always carries a positive RetryAfter.

Directories

Path Synopsis
Package pglimiter is the sliding-window rate limiter over ratelimit.Limit buckets in PostgreSQL (rate_limits), shared by every replica: AuthKit's limiter of record, and the Redis limiter's while Redis fails.
Package pglimiter is the sliding-window rate limiter over ratelimit.Limit buckets in PostgreSQL (rate_limits), shared by every replica: AuthKit's limiter of record, and the Redis limiter's while Redis fails.
Package redislimiter is the Redis-backed sliding-window rate limiter over ratelimit.Limit buckets, shared by every replica.
Package redislimiter is the Redis-backed sliding-window rate limiter over ratelimit.Limit buckets, shared by every replica.

Jump to

Keyboard shortcuts

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