Documentation
¶
Index ¶
- func FormatMessage(baseMsg string, retryAfter time.Duration) string
- func FormatRetryAfter(d time.Duration) string
- func FormatRetryAfterSeconds(sec int64) string
- func IPKeyFunc(r *http.Request) string
- func Middleware(l *Limiter, max int64, period time.Duration, keyFunc KeyFunc) func(http.Handler) http.Handler
- type Config
- type KeyFunc
- type Limiter
- type MemoryLimiter
- type Result
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FormatMessage ¶
FormatMessage appends a human-friendly retry message to a base error message. E.g. FormatMessage("Too many requests", 75*time.Second) -> "Too many requests. Please try again in 1 minute and 15 seconds."
func FormatRetryAfter ¶
FormatRetryAfter formats a duration into human-friendly text (e.g. "1 minute and 15 seconds").
func FormatRetryAfterSeconds ¶
FormatRetryAfterSeconds formats seconds into human-friendly text.
Types ¶
type Config ¶
type Config struct {
RedisClient *redis.Client
Prefix string
Fallback bool // If true, automatically degrades to in-memory limiter when Redis is unreachable
MaxBuckets int
}
Config holds configuration options for the Limiter.
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter coordinates distributed Redis rate limiting with automatic in-memory fallback.
func (*Limiter) Allow ¶
func (l *Limiter) Allow(ctx context.Context, identifier string, max int64, period time.Duration) (Result, error)
Allow checks whether a request fits within the sliding window budget. If Redis is unreachable or unconfigured and Fallback is enabled, it seamlessly degrades to the in-memory token bucket limiter.
func (*Limiter) Memory ¶
func (l *Limiter) Memory() *MemoryLimiter
Memory returns the internal in-memory fallback limiter.
type MemoryLimiter ¶
type MemoryLimiter struct {
// contains filtered or unexported fields
}
MemoryLimiter is a high-performance in-memory token bucket rate limiter with automatic stale bucket reclamation and capacity bounds.
func NewMemoryLimiter ¶
func NewMemoryLimiter(maxBuckets ...int) *MemoryLimiter
NewMemoryLimiter creates a new in-memory fallback limiter.
func (*MemoryLimiter) Allow ¶
func (m *MemoryLimiter) Allow(key string, max int64, period time.Duration) (allowed bool, remaining int64, retryAfter time.Duration)
Allow consumes one token if available.
func (*MemoryLimiter) Len ¶
func (m *MemoryLimiter) Len() int
Len returns the number of active tracked keys in memory.
func (*MemoryLimiter) Remove ¶
func (m *MemoryLimiter) Remove(key string)
Remove restores one token to the bucket (used to refund budget on failed requests).