Documentation
¶
Overview ¶
Package ratelimit provides distributed rate limiting capabilities (reserved module).
Index ¶
Constants ¶
const ( ActionSignIn = "signin" ActionSignUp = "signup" ActionPasswordReset = "password_reset" ActionEmailOTP = "email_otp" ActionMagicLink = "magic_link" ActionPhoneOTP = "phone_otp" ActionAccountRecovery = "account_recovery" ActionAPIKey = "api_key" ActionAdmin = "admin" )
Standard rate-limited action identifiers in GoModularAuth.
const ( EventRateLimitExceeded auth.EventName = "ratelimit.exceeded" EventRateLimitReset auth.EventName = "ratelimit.reset" )
Rate limiting event names emitted to the GoModularAuth event bus.
const PluginID = "rate-limit"
PluginID identifies this plugin in the GoModularAuth runtime.
Variables ¶
var ( // ErrRateLimitExceeded indicates that the client has performed too many requests within the window. ErrRateLimitExceeded = errors.New("ratelimit: rate limit exceeded") ErrRateLimiterUnavailable = errors.New("ratelimit: rate limiter backend unavailable") // ErrInvalidPolicy indicates an unworkable or erroneous policy definition. ErrInvalidPolicy = errors.New("ratelimit: invalid policy configuration") // ErrActionRequired indicates that an evaluation request omitted the action name. ErrActionRequired = errors.New("ratelimit: action is required") )
Functions ¶
This section is empty.
Types ¶
type Algorithm ¶
type Algorithm string
Algorithm specifies the mathematical throttling model applied.
type Config ¶
type Config struct {
Policies map[string]Policy
DefaultFailMode FailMode
AutoHooks bool
Clock func() time.Time
}
Config defines the configuration options for the rate-limit plugin.
type Decision ¶
type Decision struct {
Allowed bool `json:"allowed"`
Remaining int64 `json:"remaining"`
Limit int64 `json:"limit"`
ResetAfter time.Duration `json:"reset_after"`
RetryAfter time.Duration `json:"retry_after"`
Action string `json:"action"`
Key string `json:"key"`
}
Decision represents the outcome of a rate limit evaluation.
type ExceededPayload ¶
type ExceededPayload struct {
Action string `json:"action"`
Key string `json:"key"`
RetryAfter time.Duration `json:"retry_after"`
OccurredAt time.Time `json:"occurred_at"`
}
ExceededPayload describes a throttled request event for security auditing.
type FailMode ¶
type FailMode string
FailMode determines behavior when the cache/counter infrastructure is unavailable.
type KeyType ¶
type KeyType string
KeyType specifies which attributes form the rate limit counter key.
const ( // KeyTypeIP rate limits solely based on client IP address. KeyTypeIP KeyType = "ip" // KeyTypeIdentity rate limits based on user ID, email hash, or username hash. KeyTypeIdentity KeyType = "identity" // KeyTypeComposite rate limits based on both IP and Identity. KeyTypeComposite KeyType = "composite" )
type Module ¶
type Module struct {
// contains filtered or unexported fields
}
Module implements auth.PluginModule for configurable authentication rate limiting.
type Option ¶
type Option func(*Config)
Option modifies Config settings.
func WithAutoHooks ¶
WithAutoHooks enables or disables automated Core workflow interception (BeforeSignIn / BeforeSignUp).
func WithDefaultFailMode ¶
WithDefaultFailMode configures whether throttling fails open or closed when the cache is down.
func WithPolicy ¶
WithPolicy sets or overrides a rate limiting policy for an action.
type Policy ¶
type Policy struct {
Action string `json:"action"`
Algorithm Algorithm `json:"algorithm"`
Limit int64 `json:"limit"`
Window time.Duration `json:"window"`
Capacity int64 `json:"capacity"`
RefillRate float64 `json:"refill_rate"`
KeyType KeyType `json:"key_type"`
FailMode FailMode `json:"fail_mode"`
}
Policy defines the rate limiting parameters applied to an action.
type RateLimit ¶
type RateLimit interface {
// Check evaluates whether a request exceeds configured rate limits without consuming capacity.
Check(ctx context.Context, req Request) (*Decision, error)
// Allow evaluates and consumes one token if within limits, returning the full evaluation decision.
Allow(ctx context.Context, req Request) (*Decision, error)
// Require checks and consumes a token, returning ErrRateLimitExceeded if throttled.
Require(ctx context.Context, req Request) error
// Reset clears active rate limit counters for a given request target (e.g. upon successful authentication).
Reset(ctx context.Context, req Request) error
// Policy retrieves the currently configured policy for an action.
Policy(action string) (Policy, bool)
// SetPolicy registers or dynamically overrides the policy for an action.
SetPolicy(policy Policy) error
}
RateLimit represents the strongly typed capability provided by the rate-limit plugin. Applications retrieve this capability via auth.Get[plugins.RateLimit]().