Documentation
¶
Overview ¶
Package ratelimit provides token-bucket rate limiting for the switchAILocal proxy. It supports both a global system-wide limiter and per-API-key limiters.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Middleware ¶
func Middleware(cfg config.RateLimiterConfig) gin.HandlerFunc
Middleware returns a Gin middleware that enforces rate limits. It checks the global limit first (cheapest), then per-key limit. Rate-limited requests receive HTTP 429 with a Retry-After header.
Types ¶
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter manages global and per-key rate limiting using token bucket algorithm. The global limiter protects the system from total overload. Per-key limiters prevent any single client from monopolizing resources.
func New ¶
func New(cfg config.RateLimiterConfig) *Limiter
New creates a new rate limiter from the given configuration. Starts a background goroutine to evict stale per-key entries every 5 minutes.