Documentation
¶
Overview ¶
Package ratelimit provides a small, dependency-free per-key token-bucket rate limiter suitable for guarding authentication and agent endpoints.
Buckets are keyed by an arbitrary string (typically a client IP). Each bucket refills lazily based on elapsed wall-clock time, so there is no background goroutine on the hot path. Idle buckets are evicted opportunistically to keep memory bounded under a churning set of source addresses.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Rate is the sustained number of allowed events per second.
Rate float64
// Burst is the maximum number of events allowed in an instantaneous spike.
Burst float64
// TTL evicts buckets that have not been touched for this long. Defaults to
// 10 minutes when zero.
TTL time.Duration
// MaxKeys caps the number of tracked buckets to bound memory. Defaults to
// 100000 when zero. When exceeded, the limiter sheds the oldest buckets.
MaxKeys int
// Now is an injectable clock for tests. Defaults to time.Now.
Now func() time.Time
}
Config controls a Limiter. Rate and Burst must both be positive.
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter is a concurrency-safe collection of per-key token buckets.
func (*Limiter) Allow ¶
Allow reports whether an event for key may proceed, consuming one token when it returns true.