ratelimit

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0

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

func Allow(p Preset) func(c *gin.Context) bool

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

func IPOnly(c *gin.Context) string

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

func TrustProxies(r *gin.Engine)

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.

Types

type Preset

type Preset struct {
	PerMinute float64 // sustained refill, tokens per minute
	Burst     int
	Key       func(c *gin.Context) string
}

Preset describes one route class: a sustained rate, a burst and the bucket key.

Jump to

Keyboard shortcuts

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