ratelimit

package
v0.23.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package ratelimit provides explicit fixed-window request throttling with a replaceable persistence contract. The in-memory store is bounded and suited to one-process applications; distributed deployments can replace Store.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidConfiguration = errors.New("rate limiter configuration is invalid")
View Source
var ErrInvalidForwardedChain = errors.New("rate limit: invalid forwarded address chain")

Functions

func Key

func Key(parts ...string) string

Key hashes namespaced key parts so stores do not retain raw email addresses, IP addresses, or other identifying inputs.

func Middleware

func Middleware(limiter *Limiter, key KeyFunc) httpx.Middleware

Middleware enforces a limiter before invoking the next handler.

func RemoteAddress

func RemoteAddress(request *http.Request) string

RemoteAddress returns the direct peer address without trusting spoofable forwarding headers. Applications behind trusted proxies can supply their own KeyFunc after validating proxy configuration.

Types

type Decision

type Decision struct {
	Allowed    bool
	RetryAfter time.Duration
}

type KeyFunc

type KeyFunc func(*httpx.Context) string

type Limiter

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

func New

func New(store Store, policy Policy) (*Limiter, error)

func (*Limiter) Enforce

func (limiter *Limiter) Enforce(ctx *httpx.Context, key string) error

Enforce consumes one attempt and returns a generic HTTP 429 error when the policy is exhausted. The response discloses neither the key nor counters.

func (*Limiter) Reset

func (limiter *Limiter) Reset(ctx context.Context, key string) error

func (*Limiter) Take

func (limiter *Limiter) Take(ctx context.Context, key string) (Decision, error)

type MemoryStore

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

MemoryStore is a bounded, concurrency-safe fixed-window store. When full it removes expired entries; if capacity is still exhausted, unseen keys are denied until the earliest active window resets instead of evicting a live key and allowing a key-spray bypass.

func NewMemoryStore

func NewMemoryStore(capacity int) *MemoryStore

func (*MemoryStore) Reset

func (store *MemoryStore) Reset(ctx context.Context, key string) error

func (*MemoryStore) Take

func (store *MemoryStore) Take(ctx context.Context, key string, policy Policy, now time.Time) (Decision, error)

type Policy

type Policy struct {
	Limit  int
	Window time.Duration
}

type Store

type Store interface {
	Take(context.Context, string, Policy, time.Time) (Decision, error)
	Reset(context.Context, string) error
}

Store atomically consumes one attempt for key. Reset removes accumulated attempts, normally after successful authentication.

type TrustedProxies

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

TrustedProxies is an explicit allowlist of network peers whose forwarding metadata may participate in client address resolution. Its zero value trusts no peer. Prefixes are immutable after construction and safe for concurrent use.

func ParseTrustedProxies

func ParseTrustedProxies(values ...string) (TrustedProxies, error)

ParseTrustedProxies validates a list of CIDR prefixes. Each argument may contain a comma-separated list, which makes the function convenient for an environment setting. An empty list is valid and trusts no peer. Bare IP addresses are rejected so the configured trust boundary remains conspicuous.

func (TrustedProxies) ClientAddress

func (trusted TrustedProxies) ClientAddress(request *http.Request) string

ClientAddress resolves the effective client without ever trusting forwarding metadata automatically. Forwarded or X-Forwarded-For is considered only when the socket peer belongs to trusted. A malformed or ambiguous chain safely falls back to RemoteAddress.

func (TrustedProxies) Contains

func (trusted TrustedProxies) Contains(address netip.Addr) bool

Contains reports whether address belongs to an explicitly configured proxy prefix. IPv4-mapped IPv6 addresses are normalized before comparison.

func (TrustedProxies) ResolveClientAddress

func (trusted TrustedProxies) ResolveClientAddress(request *http.Request) (string, error)

ResolveClientAddress is ClientAddress with a diagnostic error for malformed forwarding data. It still returns the direct peer as its address on error so callers can log the problem without accidentally trusting attacker input.

Directories

Path Synopsis
Package postgres provides an inspectable durable PostgreSQL rate-limit store.
Package postgres provides an inspectable durable PostgreSQL rate-limit store.

Jump to

Keyboard shortcuts

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