Documentation
¶
Overview ¶
Package ratelimit holds the in-memory, bounded-memory state behind the public surface's abuse protection: a keyed map that evicts its least recently used entry when full, and a token bucket limiter built on it. Nothing here is persisted, logged or shared between processes.
Evicting an entry forgets its state, which can only help a client (a fresh bucket). The bound is what matters: an attacker rotating IPv6 /64s or junk keys cannot grow the map, and an operation costs O(1) under one mutex. Shard the mutex if a profile ever shows it.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Limiter ¶
type Limiter struct {
// Now is the clock; nil means time.Now (tests set it).
Now func() time.Time
// contains filtered or unexported fields
}
Limiter is a per-key token bucket: a key may burst up to the burst size, then gets rate requests per second. Memory is bounded by the key cap given to New.
func New ¶
New returns a limiter allowing rate requests per second per key with bursts of burst, tracking at most maxKeys keys. A rate <= 0 disables it (Allow always says yes).
type Map ¶
type Map[V any] struct { // contains filtered or unexported fields }
Map is a goroutine-safe map from string keys to V holding at most max entries.
func (*Map[V]) GetOrCreate ¶
GetOrCreate returns the value for key, storing mk() first when there is none. The oldest entry is evicted when the map is full.