Documentation
¶
Overview ¶
Package ratelimit is a per-key token-bucket gin middleware for public routes. Services keep their own presets and keys next to their routes.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Allow ¶
Allow returns a check enforcing p, with its own bucket store: when the bucket is empty it answers 429 {"error":"rate_limited"} with Retry-After, aborts and returns false.
func IPOnly ¶
IPOnly keys a bucket per client IP (see TrustProxies for where it comes from); an IPv6 client is its /64, the block one host usually holds.
func New ¶
func New(p Preset) gin.HandlerFunc
New returns a middleware enforcing p (see Allow). Routes that share one returned handler share its buckets.
func TrustProxies ¶
TrustProxies makes r trust forwarded client addresses only from the proxies in TRUSTED_PROXIES (comma separated IPs or CIDRs). It sets ClientIP for the whole router, not only for rate limits. Unset or invalid, it trusts no proxy: gin's default trusts every sender, so any client could pick its own IP. Call it right after gin.New(): it adds the middleware that reads every X-Forwarded-For line.