Documentation
¶
Overview ¶
Package ratelimit is a rate limiter for Fiber based on the token bucket algorithm, with partitioned in-memory storage, dynamic per-request limits, variable cost, and bounded memory against key-flooding. See README.md.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct {
// Max is the number of requests allowed per Expiration.
// Default: 100. Ignored if you set LimitFor.
Max int
// Expiration is the refill window (linear refill, not a hard
// reset). Default: 1 minute. Ignored if you set LimitFor.
Expiration time.Duration
// Burst is the maximum allowed burst (bucket capacity). If it's
// 0, Burst = Max, which is the classic behavior. Setting it lower
// than Max smooths out spikes: e.g. Max=60/min with Burst=10 allows an
// average of 1 req/sec but never more than 10 at once. Ignored with LimitFor.
Burst int
// LimitFor computes the limit dynamically per request. Enables
// limits per plan/role/route with a single middleware and a single store:
//
// LimitFor: func(c *fiber.Ctx) core.Limit {
// if isPro(c) { return core.PerWindow(1000, time.Minute) }
// return core.PerWindow(100, time.Minute)
// }
//
// If nil, the fixed Max/Expiration/Burst are used.
LimitFor func(c fiber.Ctx) core.Limit
// Cost is how many tokens each request consumes. Default: fixed 1.
// Useful for charging heavy endpoints more:
//
// Cost: func(c *fiber.Ctx) float64 {
// if strings.HasPrefix(c.Path(), "/api/export") { return 10 }
// return 1
// }
Cost func(c fiber.Ctx) float64
// Next allows skipping the limiter for certain requests (health
// checks, internal IPs, admins). If it returns true, no limit is
// applied. Standard convention among Fiber middlewares.
Next func(c fiber.Ctx) bool
// KeyGenerator identifies the client. Default: c.IP().
//
// SECURITY: if your app is behind a proxy/load balancer (Railway
// is), c.IP() returns the proxy's IP unless you configure Fiber
// with ProxyHeader and EnableTrustedProxyCheck + TrustedProxies. Without that,
// reading X-Forwarded-For by hand lets any client spoof
// its identity by sending the header itself and evade the limit (or worse,
// exhaust a victim's limit). See README, "IPs behind
// a proxy" section.
KeyGenerator func(c fiber.Ctx) string
// MaxKeyLength bounds the key size. Longer keys are
// replaced by their FNV-64 hash in hex (17 bytes), preserving
// practical uniqueness. Prevents an attacker from inflating memory by sending
// giant API keys/headers when the KeyGenerator reads client
// input. Default: 128. Negative = no limit.
MaxKeyLength int
// MaxKeys bounds the total number of in-memory buckets; once exceeded, the
// most idle one is evicted. This is the defense against key-flooding (millions
// of fake identities to exhaust RAM). Default: 65536.
MaxKeys int
// LimitReached runs when the limit is exceeded. Default: 429 JSON.
LimitReached fiber.Handler
// DisableHeaders disables X-RateLimit-* and Retry-After. Exposing
// these headers is friendly to legitimate clients, but it also
// gives an attacker information about your thresholds; on
// sensitive endpoints (login, password reset) it may be worth turning them off.
DisableHeaders bool
// SkipSuccessfulRequests refunds the token if the response was
// < 400 (only errors/abuse count against the limit).
SkipSuccessfulRequests bool
// SkipFailedRequests refunds the token if the response was >= 400
// or the handler returned an error (only successful traffic counts).
SkipFailedRequests bool
// Shards of the in-memory store. Default: 64.
Shards int
// CleanupInterval defines how often idle buckets are purged.
// Default: 5 minutes. 0 uses the default; negative disables it.
CleanupInterval time.Duration
// Store allows injecting a custom backend (e.g. Redis for
// multi-instance). If nil, the in-memory store is used.
Store core.Store
}
Config defines the rate limiter's behavior.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package core contains the rate limiter engine: token bucket with sharded in-memory storage.
|
Package core contains the rate limiter engine: token bucket with sharded in-memory storage. |
|
Package redisstore implements core.Store over Redis, to deploy the rate limiter across multiple instances sharing a single count.
|
Package redisstore implements core.Store over Redis, to deploy the rate limiter across multiple instances sharing a single count. |
Click to show internal directories.
Click to hide internal directories.