Documentation
¶
Overview ¶
Package limiter provides several in-memory rate limiting algorithms.
Supported algorithms:
- Token Bucket
- Fixed Window
- Sliding Window Log
- Sliding Window Counter
- Leaky Bucket
The package is designed to be embedded into HTTP middleware and other request-processing pipelines.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FixedWindow ¶
type FixedWindow struct {
// contains filtered or unexported fields
}
func NewFixedWindow ¶
func NewFixedWindow(limit int, window time.Duration) *FixedWindow
NewFixedWindow creates a fixed window rate limiter.
limit is the maximum number of requests allowed during each window.
window specifies the duration of a single rate-limiting window.
func (*FixedWindow) Allow ¶
func (fw *FixedWindow) Allow() LimiterResult
func (*FixedWindow) Remaining ¶
func (fw *FixedWindow) Remaining() int
func (*FixedWindow) Requests ¶
func (fw *FixedWindow) Requests() int
type LeakyBucket ¶
type LeakyBucket struct {
// contains filtered or unexported fields
}
func NewLeakyBucket ¶
func NewLeakyBucket(capacity int, interval time.Duration) *LeakyBucket
NewLeakyBucket creates a leaky bucket rate limiter.
capacity is the maximum number of queued requests.
interval specifies how frequently one queued request is considered leaked.
func (*LeakyBucket) Allow ¶
func (lb *LeakyBucket) Allow() LimiterResult
func (*LeakyBucket) Remaining ¶
func (lb *LeakyBucket) Remaining() int
Remaining returns the remaining queue capacity in the bucket.
func (*LeakyBucket) Requests ¶
func (lb *LeakyBucket) Requests() int
Requests returns the number of active (unleaked) requests in the bucket.
type Limiter ¶
type Limiter interface {
Allow() LimiterResult
}
Limiter defines the interface implemented by all rate-limiting algorithms.
Allow evaluates whether the next request should be permitted.
type LimiterResult ¶
LimiterResult contains the outcome of a rate-limit evaluation.
Allowed indicates whether the request may proceed. Remaining reports the estimated number of requests that can still be accepted, while RetryAfter indicates how long the caller should wait when the request is rejected.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
func NewManager ¶
func NewManager() *Manager
NewManager creates a Manager for maintaining independent runtime limiter instances for identifiers.
func (*Manager) GetOrCreate ¶
func (m *Manager) GetOrCreate( identifier model.Identifier, config model.LimiterConfig, ) (Limiter, error)
GetOrCreate returns the existing limiter for an identifier when its configuration matches the requested configuration. Otherwise, it creates and stores a new limiter using the provided configuration.
type SlidingWindowCounter ¶
type SlidingWindowCounter struct {
// contains filtered or unexported fields
}
func NewSlidingWindowCounter ¶
func NewSlidingWindowCounter(limit int, window time.Duration) *SlidingWindowCounter
func (*SlidingWindowCounter) Allow ¶
func (swc *SlidingWindowCounter) Allow() LimiterResult
func (*SlidingWindowCounter) Remaining ¶
func (swc *SlidingWindowCounter) Remaining() int
Remaining returns the estimated remaining request capacity in the current sliding window.
func (*SlidingWindowCounter) Requests ¶
func (swc *SlidingWindowCounter) Requests() int
Requests returns the estimated number of active requests in the current sliding window.
type SlidingWindowLog ¶
type SlidingWindowLog struct {
// contains filtered or unexported fields
}
func NewSlidingWindowLog ¶
func NewSlidingWindowLog(limit int, window time.Duration) *SlidingWindowLog
NewSlidingWindowLog creates a sliding window rate limiter.
limit is the maximum number of requests allowed within the rolling time window.
window specifies the duration over which requests are tracked.
func (*SlidingWindowLog) Allow ¶
func (swl *SlidingWindowLog) Allow() LimiterResult
func (*SlidingWindowLog) Remaining ¶
func (swl *SlidingWindowLog) Remaining() int
Remaining returns the remaining request capacity in the current sliding window.
func (*SlidingWindowLog) Requests ¶
func (swl *SlidingWindowLog) Requests() int
Requests returns the number of active requests after removing expired timestamps.
type TokenBucket ¶
type TokenBucket struct {
// contains filtered or unexported fields
}
func NewTokenBucket ¶
func NewTokenBucket(capacity int, interval time.Duration, amount int) *TokenBucket
NewTokenBucket creates a new token bucket rate limiter.
capacity defines the maximum number of tokens the bucket can hold.
interval specifies how often tokens are replenished.
amount specifies how many tokens are added every refill interval. If amount is less than or equal to zero, it defaults to 1.
Refills are computed lazily based on elapsed time whenever the bucket is accessed, rather than by a background goroutine.
func (*TokenBucket) Allow ¶
func (tb *TokenBucket) Allow() LimiterResult
func (*TokenBucket) AllowN ¶
func (tb *TokenBucket) AllowN(amount int) LimiterResult
func (*TokenBucket) LastRefill ¶
func (tb *TokenBucket) LastRefill() time.Time
func (*TokenBucket) Remaining ¶
func (tb *TokenBucket) Remaining() int