Documentation
¶
Overview ¶
Package rate implements a rate limiter with support for global, per-IP, and CIDR-based rate limits with periodic hot-reload from file, Redis, or HTTP sources.
Index ¶
- Constants
- Variables
- func NewLimiter(r float64, b int) limiter.Limiter
- func NewRateLimiter(opts ...Option) limiter.RateLimiter
- type Option
- func FileLoaderOption(fileLoader loader.Loader) Option
- func HTTPLoaderOption(httpLoader loader.Loader) Option
- func LimitsOption(limits ...string) Option
- func LoggerOption(logger logger.Logger) Option
- func RedisLoaderOption(redisLoader loader.Loader) Option
- func ReloadPeriodOption(period time.Duration) Option
- type RateLimitGenerator
Constants ¶
const ( // GlobalLimitKey is the key used to configure a global rate limit that applies to all traffic. GlobalLimitKey = "$" // IPLimitKey is the key used to configure a per-IP rate limit where each unique IP // gets its own independent rate limiter. IPLimitKey = "$$" )
Variables ¶
var ErrRateLimit = errors.New("rate limit")
ErrRateLimit is returned when a rate limit is exceeded.
Functions ¶
func NewLimiter ¶
NewLimiter creates a limiter.Limiter with the specified rate r (events per second) and burst b.
func NewRateLimiter ¶
func NewRateLimiter(opts ...Option) limiter.RateLimiter
NewRateLimiter creates a limiter.RateLimiter with the given options. It starts a background goroutine for periodic reload and returns an implementation that supports global, per-IP, and CIDR-based rate limits.
Types ¶
type Option ¶
type Option func(opts *options)
Option configures a [rateLimiter].
func FileLoaderOption ¶
FileLoaderOption sets the file-based loader.Loader for rate limit rules.
func HTTPLoaderOption ¶
HTTPLoaderOption sets the HTTP-based loader.Loader for rate limit rules.
func LimitsOption ¶
LimitsOption sets the static rate limit rules as strings in "key limit" format (e.g., "$ 100", "192.168.1.1 50", "10.0.0.0/8 30").
func LoggerOption ¶
LoggerOption sets the logger.Logger for the rate limiter. If not set, a no-op logger is used.
func RedisLoaderOption ¶
RedisLoaderOption sets the Redis-based loader.Loader for rate limit rules.
func ReloadPeriodOption ¶
ReloadPeriodOption sets the period for periodic reload of rate limit rules from external loaders. If zero or negative, periodic reload is disabled. Values less than one second are clamped to one second.
type RateLimitGenerator ¶
RateLimitGenerator is an interface for generating limiter.Limiter instances.
func NewRateLimitGenerator ¶
func NewRateLimitGenerator(r float64) RateLimitGenerator
NewRateLimitGenerator creates a RateLimitGenerator that produces a new limiter.Limiter for each call to Limiter(). Useful for per-IP rate limiting where each IP should have its own independent rate limiter.
func NewRateLimitSingleGenerator ¶
func NewRateLimitSingleGenerator(r float64) RateLimitGenerator
NewRateLimitSingleGenerator creates a RateLimitGenerator that returns the same limiter.Limiter for every call to Limiter(). Useful for shared global rate limits where all callers share the same token bucket.