ratelimit

package
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package ratelimit implements a Redis-backed, per-client, per-route-tier request rate limiter. Counters live in Redis (INCR + EXPIRE) rather than per-pod memory, so the limit is consistent across catalog-api's replicas instead of being N times looser depending on how many pods happen to be up.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ping

func Ping(ctx context.Context, pool *redis.Pool) error

Ping verifies the Redis backend is reachable, for use in startup/readiness checks.

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

func New(pool *redis.Pool, tiers map[string]Tier) *Limiter

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.

func (*Limiter) Allow

func (l *Limiter) Allow(ctx context.Context, clientKey, tierName string) (bool, error)

Allow increments the request counter for clientKey within the named tier and reports whether the request is within that tier's limit. A non-nil error means the Redis backend was unreachable - callers must treat that as fail-closed (reject the request), not fail-open.

type Tier

type Tier struct {
	Limit  int
	Window int // seconds
}

Tier describes a rate-limit budget: at most Limit requests per Window.

Jump to

Keyboard shortcuts

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