Documentation
¶
Overview ¶
Package distlimit provides a high-performance, resilient, and pluggable rate limiting library for Go applications. It supports pluggable algorithm strategies (Token Bucket, Leaky Bucket, Sliding Window Counter, Sliding Window Log, Fixed Window), multiple storage drivers (In-Memory, Redis, Hybrid Dual-Tier), and turn-key middleware adapters for popular frameworks.
Index ¶
- Variables
- type Config
- type Driver
- type KeyFunc
- type Limiter
- func (l *Limiter) Allow(ctx context.Context) (Result, error)
- func (l *Limiter) AllowKey(ctx context.Context, key string) (Result, error)
- func (l *Limiter) Close(ctx context.Context) error
- func (l *Limiter) ResetKey(ctx context.Context, key string) error
- func (l *Limiter) UpdatePolicy(limit int64, window time.Duration)
- type Option
- type Policy
- type PolicyResolver
- type Result
- type State
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidLimit is returned when the configured rate limit is less than or equal to 0. ErrInvalidLimit = errors.New("distlimit: limit must be greater than 0") // ErrInvalidWindow is returned when the configured rate limit window is less than or equal to 0. ErrInvalidWindow = errors.New("distlimit: window must be greater than 0") // ErrNilDriver is returned when a nil Driver is supplied during Limiter initialization. ErrNilDriver = errors.New("distlimit: storage driver cannot be nil") // ErrNilAlgorithm is returned when a nil Algorithm is supplied to WithAlgorithm. ErrNilAlgorithm = errors.New("distlimit: algorithm strategy cannot be nil") )
Sentinels errors returned by distlimit.
Functions ¶
This section is empty.
Types ¶
type Config ¶ added in v1.1.0
type Config struct {
// contains filtered or unexported fields
}
Config holds configuration parameters used during Limiter construction.
type Driver ¶
type Driver interface {
// Allow evaluates rate limit rules for the specified key using the given algorithm strategy.
Allow(ctx context.Context, key string, limit int64, window time.Duration, alg algorithm.Algorithm) (algorithm.Result, error)
// Reset clears the rate limit state entry for the specified key in the storage driver.
Reset(ctx context.Context, key string) error
// Close gracefully releases any storage connections, background routines, or resources held by the driver.
Close(ctx context.Context) error
}
Driver defines the interface that all rate limiting storage backends (Memory, Redis, Hybrid) must implement.
type KeyFunc ¶
KeyFunc defines a function signature for dynamically extracting a rate limit key from a Context.
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter is the central rate limiting coordinator. It combines a storage Driver, rate rules, and an algorithm strategy to evaluate incoming requests.
Limiter is thread-safe and designed for concurrent use by multiple goroutines.
func New ¶
New creates and initializes a new Limiter instance with the specified storage driver and optional options. If WithAlgorithm is omitted, it defaults to the Sliding Window Counter algorithm strategy for backward compatibility. Returns an error if driver is nil, or if limit or window are invalid (<= 0).
func (*Limiter) Allow ¶
Allow evaluates the rate limit key extracted via the configured KeyFunc against the active limits.
func (*Limiter) AllowKey ¶
AllowKey evaluates the rate limit for an explicitly specified key string against the active limits.
func (*Limiter) Close ¶
Close gracefully closes the underlying storage driver and releases associated resources.
type Option ¶
type Option func(*Config)
Option configures functional parameters for Limiter initialization.
func WithAlgorithm ¶ added in v1.1.0
WithAlgorithm sets the pluggable rate limiting algorithm strategy (e.g., tokenbucket, leakybucket, slidingcounter).
func WithKeyFunc ¶
WithKeyFunc configures a custom key extraction function for extracting rate limit keys from context.
func WithLimit ¶
WithLimit sets the maximum number of requests allowed within the configured time window.
func WithMetricObserver ¶ added in v1.2.0
WithMetricObserver sets a custom telemetry/metrics observer for recording evaluation events.
func WithPolicyResolver ¶ added in v1.2.0
func WithPolicyResolver(resolver PolicyResolver) Option
WithPolicyResolver sets a dynamic policy resolver for evaluating tier-based rate limits per key.
func WithWindow ¶
WithWindow sets the duration of the rate limiting time window.
type Policy ¶ added in v1.2.0
type Policy struct {
// Limit is the maximum number of requests allowed within the Window duration.
Limit int64
// Window is the time duration of the rate limit window.
Window time.Duration
}
Policy defines the rate limit capacity rules (Limit and Window duration).
type PolicyResolver ¶ added in v1.2.0
type PolicyResolver interface {
// ResolvePolicy returns a dynamic Policy for the specified key.
// If false is returned, the default Limiter policy will be used.
ResolvePolicy(ctx context.Context, key string) (Policy, bool)
}
PolicyResolver defines an interface for dynamically resolving rate limit policies per key or user tier.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package algorithm defines core interfaces and shared data structures for rate limiting algorithm strategies.
|
Package algorithm defines core interfaces and shared data structures for rate limiting algorithm strategies. |
|
fixedwindow
Package fixedwindow implements the Fixed Window rate limiting algorithm.
|
Package fixedwindow implements the Fixed Window rate limiting algorithm. |
|
leakybucket
Package leakybucket implements the Leaky Bucket rate limiting algorithm for traffic shaping.
|
Package leakybucket implements the Leaky Bucket rate limiting algorithm for traffic shaping. |
|
slidingcounter
Package slidingcounter implements the Sliding Window Counter rate limiting algorithm.
|
Package slidingcounter implements the Sliding Window Counter rate limiting algorithm. |
|
slidinglog
Package slidinglog implements the Sliding Window Log rate limiting algorithm.
|
Package slidinglog implements the Sliding Window Log rate limiting algorithm. |
|
tokenbucket
Package tokenbucket implements the Token Bucket rate limiting algorithm.
|
Package tokenbucket implements the Token Bucket rate limiting algorithm. |
|
driver
|
|
|
hybrid
Package hybrid implements a dual-tier resilient rate limiting driver with a Half-Open Circuit Breaker.
|
Package hybrid implements a dual-tier resilient rate limiting driver with a Half-Open Circuit Breaker. |
|
memory
Package memory implements a fast, thread-safe, sharded in-memory rate limiting driver.
|
Package memory implements a fast, thread-safe, sharded in-memory rate limiting driver. |
|
redis
Package redis implements a distributed rate limiting driver using Redis as the backend.
|
Package redis implements a distributed rate limiting driver using Redis as the backend. |
|
examples
|
|
|
dynamic-policy
command
|
|
|
echo
command
|
|
|
echov5
command
|
|
|
fiber-slidingcounter
command
|
|
|
gin-hybrid
command
|
|
|
grpc
command
|
|
|
nethttp
command
|
|
|
prometheus-metrics
command
|
|
|
Package metrics provides observability interfaces and telemetry data structures for distlimit.
|
Package metrics provides observability interfaces and telemetry data structures for distlimit. |
|
otel
Package otel provides an OpenTelemetry metrics observer implementation for distlimit.
|
Package otel provides an OpenTelemetry metrics observer implementation for distlimit. |
|
prometheus
Package prometheus provides a Prometheus metrics observer implementation for distlimit.
|
Package prometheus provides a Prometheus metrics observer implementation for distlimit. |
|
middleware
|
|
|
echo
Package echo provides rate limiting middleware for the Echo v4 web framework.
|
Package echo provides rate limiting middleware for the Echo v4 web framework. |
|
echov5
Package echov5 provides rate limiting middleware for the Echo v5 web framework.
|
Package echov5 provides rate limiting middleware for the Echo v5 web framework. |
|
fiber
Package fiber provides rate limiting middleware for the Fiber v3 web framework.
|
Package fiber provides rate limiting middleware for the Fiber v3 web framework. |
|
gin
Package gin provides rate limiting middleware for the Gin web framework.
|
Package gin provides rate limiting middleware for the Gin web framework. |
|
grpc
Package grpc provides gRPC server interceptor middleware for rate limiting.
|
Package grpc provides gRPC server interceptor middleware for rate limiting. |
|
nethttp
Package nethttp provides rate limiting middleware for Go's standard net/http package.
|
Package nethttp provides rate limiting middleware for Go's standard net/http package. |