Documentation
¶
Index ¶
- Constants
- Variables
- func DefaultLimitReachedHandler(ctx fh.Ctx, result Result) error
- func New(config Config) fh.HandlerFunc
- func NewAdaptive(cfg AdaptiveConfig) (fh.HandlerFunc, func())
- type AdaptiveConfig
- type Config
- type KeyFunc
- type LimitReachedHandler
- type LoadAwareLimiter
- type Result
- type SkipFunc
Constants ¶
View Source
const ( HeaderLimit = "X-RateLimit-Limit" HeaderRemaining = "X-RateLimit-Remaining" HeaderReset = "X-RateLimit-Reset" HeaderRetryAfter = "Retry-After" )
Variables ¶
View Source
var ErrLimitReached = errors.New("ratelimiter: limit reached")
Functions ¶
func New ¶
func New(config Config) fh.HandlerFunc
func NewAdaptive ¶
func NewAdaptive(cfg AdaptiveConfig) (fh.HandlerFunc, func())
Types ¶
type AdaptiveConfig ¶
type Config ¶
type Config struct {
Max int
Window time.Duration
KeyFunc KeyFunc
Skip SkipFunc
// Store backs the rate-limit counters. Defaults to an in-process
// kv.MemoryStore. Pass a kv.NewFileStore(dir, ...) to persist counters
// across restarts on a single node, or any other kv.Store implementation
// for a distributed backend.
Store kv.Store
SendHeaders bool
LimitReached LimitReachedHandler
}
type LoadAwareLimiter ¶
type LoadAwareLimiter struct {
// contains filtered or unexported fields
}
func NewLoadAware ¶
func NewLoadAware(baseCfg Config, highWater int64) *LoadAwareLimiter
func (*LoadAwareLimiter) CurrentLimit ¶
func (l *LoadAwareLimiter) CurrentLimit() int
func (*LoadAwareLimiter) Middleware ¶
func (l *LoadAwareLimiter) Middleware() fh.HandlerFunc
func (*LoadAwareLimiter) SetBaseLimit ¶
func (l *LoadAwareLimiter) SetBaseLimit(limit int)
func (*LoadAwareLimiter) Stop ¶
func (l *LoadAwareLimiter) Stop()
type Result ¶
type Result struct {
Allowed bool
Limit int
Remaining int
Used int
ResetAt time.Time
RetryAfter time.Duration
}
func Allow ¶
func Allow(store kv.Store, key string, limit int, window time.Duration, now time.Time) (Result, error)
Allow applies a fixed-window rate-limit check for key against store, admitting up to limit requests per window. One file-per-key or shard-per-key counter is read, incremented and written back atomically via kv.Store.Mutate, so the same algorithm works unchanged whether store is a kv.MemoryStore, a kv.FileStore, or any other kv.Store implementation.
Click to show internal directories.
Click to hide internal directories.