rate

package
v0.28.1 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 16 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

View Source
var ErrRateLimit = errors.New("rate limit")

ErrRateLimit is returned when a rate limit is exceeded.

Functions

func NewLimiter

func NewLimiter(r float64, b int) limiter.Limiter

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

func FileLoaderOption(fileLoader loader.Loader) Option

FileLoaderOption sets the file-based loader.Loader for rate limit rules.

func HTTPLoaderOption

func HTTPLoaderOption(httpLoader loader.Loader) Option

HTTPLoaderOption sets the HTTP-based loader.Loader for rate limit rules.

func LimitsOption

func LimitsOption(limits ...string) Option

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

func LoggerOption(logger logger.Logger) Option

LoggerOption sets the logger.Logger for the rate limiter. If not set, a no-op logger is used.

func RedisLoaderOption

func RedisLoaderOption(redisLoader loader.Loader) Option

RedisLoaderOption sets the Redis-based loader.Loader for rate limit rules.

func ReloadPeriodOption

func ReloadPeriodOption(period time.Duration) Option

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

type RateLimitGenerator interface {
	Limiter() limiter.Limiter
}

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.

Jump to

Keyboard shortcuts

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