Documentation
¶
Overview ¶
Package ratelimit implements the platform-wide, Redis-backed, per-client, per-route-tier request rate limiter for sweetrpg Go HTTP services. Counters live in Redis (INCR + EXPIRE) rather than per-pod memory, so a client's limit is consistent across a service's replicas instead of being N times looser depending on how many pods are up.
If the counter store is unreachable, standard-tier routes fail closed (503) rather than being served as if unlimited, while /status/* routes fail open so a cache fault cannot fail the liveness and readiness probes that call them (PADR-0036). Every store error is logged at ERROR and counted in ratelimit_backend_errors_total{tier}. Every 429 carries a Retry-After header.
Index ¶
Constants ¶
const ( TierCheap = "cheap" TierStandard = "standard" )
Tier names. Routes under /status/ use the looser cheap tier; everything else uses standard.
Variables ¶
This section is empty.
Functions ¶
func ClientKey ¶
ClientKey identifies the caller for per-client limiting: the API key if the client sent an X-API-Key header, otherwise the client IP. It does not introduce an auth mechanism - it keys off whatever identity already exists on the request. The key:/ip: prefixes keep a client that sends an API key and one that does not from colliding on the same source address.
func Middleware ¶
func Middleware(pool *redis.Pool, opts Options) gin.HandlerFunc
Middleware returns a gin handler that enforces per-client, per-tier limits backed by the given Redis pool. It aborts with 429 (ErrorRateLimited, plus Retry-After) when a client is over its tier limit. When the pool is nil or the store errors it logs, counts, and then either lets the request through (/status/*, so probes survive a cache fault) or aborts with 503 (ErrorRateLimitUnavailable) for every other route.
Types ¶
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter enforces per-client request limits per tier, backed by a Redis connection pool.
func New ¶
New builds a Limiter using the given Redis pool and tier definitions. Callers should pass at least a "standard" and "cheap" tier; Allow falls back to "standard" for an unknown tier name.
type Options ¶
Options configures the middleware's tier budgets.
func DefaultOptions ¶
func DefaultOptions() Options
DefaultOptions reads the tier budgets from the standard env vars (RATE_LIMIT_CHEAP / RATE_LIMIT_CHEAP_WINDOW_SECONDS / RATE_LIMIT_STANDARD / RATE_LIMIT_STANDARD_WINDOW_SECONDS), falling back to the catalog-api-validated defaults.