Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct {
Tiers map[Tier]TierConfig
KeyExtractor KeyExtractor
TierResolver TierResolver
EventHandler EventHandler
CleanupInterval time.Duration
WarningThreshold float64 // fires OnApproaching when remaining tokens fall below this fraction (0.0-1.0)
}
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns config with sensible defaults. consumers should override KeyExtractor and TierResolver.
type ErrorResponse ¶
type ErrorResponse struct {
Error string `json:"error"`
Message string `json:"message"`
Limit int `json:"limit"`
Remaining int `json:"remaining"`
ResetAt int64 `json:"reset_at"`
RetryAfter int `json:"retry_after"`
}
error response structure returned when rate limit is exceeded
type Event ¶
type EventHandler ¶
type EventHandler interface {
OnLimited(event Event) // called when request is rate limited (before 429 sent)
OnApproaching(event Event) // called when usage crosses warning threshold
OnBlocked(event Event) // called for escalating blocks (future feature)
}
EventHandler receives notifications on rate limit events. implementations can log, send emails, update databases, etc.
type KeyExtractor ¶
KeyExtractor returns the rate limit key, key type (e.g. "session", "apikey", "ip"), and error
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter implements token bucket rate limiting with tier support
func (*Limiter) Middleware ¶
wraps an http.Handler to enforce rate limits. sets standard rate limit headers on all responses and returns 429 with json body when limit is exceeded.
func (*Limiter) StartCleanup ¶
starts background goroutine to remove stale visitors. safe to call multiple times.
func (*Limiter) VisitorCount ¶
type NoopEventHandler ¶
type NoopEventHandler struct{}
func (*NoopEventHandler) OnApproaching ¶
func (n *NoopEventHandler) OnApproaching(event Event)
func (*NoopEventHandler) OnBlocked ¶
func (n *NoopEventHandler) OnBlocked(event Event)
func (*NoopEventHandler) OnLimited ¶
func (n *NoopEventHandler) OnLimited(event Event)
type TierConfig ¶
type TierResolver ¶
TierResolver maps a key to its tier based on key type