Documentation
¶
Overview ¶
Package ratelimit provides rate limiting for AuthSome endpoints.
Index ¶
Constants ¶
const DefaultMaxKeys = 100_000
DefaultMaxKeys bounds the memory limiter. A flood of distinct keys (one per spoofed address, say) would otherwise grow the map without limit; at the bound, idle windows are dropped before a new key is admitted.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type KVLimiter ¶
type KVLimiter struct {
// contains filtered or unexported fields
}
KVLimiter is a fixed-window limiter on the shared KV store, so a limit holds across every replica that shares the database.
Each key gets one counter per window, keyed by the window's start, and the counter expires with the window. A fixed window lets up to twice the limit through around a boundary; the limits it guards are small, and one increment per request is what keeps the shared store cheap.
type Limiter ¶
type Limiter interface {
// Allow checks if a request identified by key is allowed within the given
// limit and window. Returns true if allowed, false if rate limited.
Allow(ctx context.Context, key string, limit int, window time.Duration) (bool, error)
// Remaining returns the number of requests remaining for the given key.
Remaining(ctx context.Context, key string, limit int, window time.Duration) (int, error)
}
Limiter is the interface for rate limiting.
type MemoryLimiter ¶
type MemoryLimiter struct {
// contains filtered or unexported fields
}
MemoryLimiter is an in-memory sliding window rate limiter.
func NewMemoryLimiter ¶
func NewMemoryLimiter() *MemoryLimiter
NewMemoryLimiter creates a new in-memory rate limiter.
func NewMemoryLimiterWithLimit ¶
func NewMemoryLimiterWithLimit(maxKeys int) *MemoryLimiter
NewMemoryLimiterWithLimit creates a memory limiter bounded to maxKeys.
func (*MemoryLimiter) Allow ¶
func (l *MemoryLimiter) Allow(_ context.Context, key string, limit int, dur time.Duration) (bool, error)
Allow checks if a request is allowed under the sliding window.
func (*MemoryLimiter) Len ¶
func (l *MemoryLimiter) Len() int
Len reports how many keys the limiter tracks.
type NoopLimiter ¶
type NoopLimiter struct{}
NoopLimiter is a rate limiter that always allows requests.
func NewNoopLimiter ¶
func NewNoopLimiter() *NoopLimiter
NewNoopLimiter creates a no-op rate limiter.