ratelimit

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 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 backend 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 memory and redis limiter backends 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 memory and redis backends.

type Limiter added in v1.1.0

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

Limiter spends one request of key's budget in bucket. It has no error: a limiter that loses its store decides in process instead, so no failure lifts a budget.

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 memorylimiter is the in-process sliding-window rate limiter over ratelimit.Limit buckets: AuthKit's limiter without Redis, and the Redis limiter's fallback.
Package memorylimiter is the in-process sliding-window rate limiter over ratelimit.Limit buckets: AuthKit's limiter without Redis, and the Redis limiter's fallback.
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