Documentation
¶
Overview ¶
Package ratelimit provides rate limiting functionality to control the rate at which requests are processed over time.
The package implements a generic RequestProcessor that can be used with both HTTP and gRPC requests to enforce rate limits with optional backlog queuing. When rate limits are exceeded, requests can be queued in a backlog with configurable timeout, or rejected immediately if the backlog is full.
Key features:
- Token bucket and sliding window rate limiting algorithms
- Configurable rate limits per key or globally
- Optional backlog queuing with timeout and retry-after headers
- LRU-based key management for memory efficiency
- Integration with external rate limiters via Limiter interface
Index ¶
Constants ¶
const DefaultRateLimitBacklogTimeout = time.Second * 5
DefaultRateLimitBacklogTimeout determines the default timeout for backlog processing.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BacklogParams ¶
BacklogParams defines parameters for the backlog processing.
type LeakyBucketLimiter ¶
type LeakyBucketLimiter struct {
// contains filtered or unexported fields
}
LeakyBucketLimiter implements GCRA (Generic Cell Rate Algorithm). It's a leaky bucket variant algorithm. More details and good explanation of this alg is provided here: https://brandur.org/rate-limiting#gcra.
func NewLeakyBucketLimiter ¶
func NewLeakyBucketLimiter(maxRate Rate, maxBurst, maxKeys int) (*LeakyBucketLimiter, error)
NewLeakyBucketLimiter creates a new leaky bucket rate limiter.
type Limiter ¶
type Limiter interface {
Allow(ctx context.Context, key string) (allow bool, retryAfter time.Duration, err error)
}
Limiter interface defines the rate limiting contract.
type RequestHandler ¶
type RequestHandler interface {
// GetContext returns the request context.
GetContext() context.Context
// GetKey extracts the rate limiting key from the request.
// Returns key, bypass (whether to bypass rate limiting), and error.
GetKey() (string, bool, error)
// Execute processes the actual request.
Execute() error
// OnReject handles request rejection when rate limit is exceeded.
OnReject(params Params) error
// OnError handles errors that occur during rate limiting.
OnError(params Params, err error) error
}
RequestHandler abstracts the common operations for both HTTP and gRPC requests.
type RequestProcessor ¶
type RequestProcessor struct {
// contains filtered or unexported fields
}
RequestProcessor handles the common rate limiting logic for any request type.
func NewRequestProcessor ¶
func NewRequestProcessor(limiter Limiter, backlogParams BacklogParams) (*RequestProcessor, error)
NewRequestProcessor creates a new generic request processor.
func (*RequestProcessor) ProcessRequest ¶
func (p *RequestProcessor) ProcessRequest(rh RequestHandler) error
ProcessRequest contains the shared rate limiting logic.
type SlidingWindowLimiter ¶
type SlidingWindowLimiter struct {
// contains filtered or unexported fields
}
SlidingWindowLimiter implements sliding window rate limiting algorithm.
func NewSlidingWindowLimiter ¶
func NewSlidingWindowLimiter(maxRate Rate, maxKeys int) (*SlidingWindowLimiter, error)
NewSlidingWindowLimiter creates a new sliding window rate limiter.