ratelimit

package
v0.0.0-...-8acab51 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package ratelimit provides per-client rate limiting middleware.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MiddlewareWithConfig

func MiddlewareWithConfig(cfg Config) echo.MiddlewareFunc

MiddlewareWithConfig creates a rate limiting middleware with the given config.

Types

type AuthConfig

type AuthConfig struct {
	// LoginRate is the number of login attempts allowed per window.
	LoginRate int
	// LoginWindow is the time window for login rate limiting.
	LoginWindow time.Duration
	// PasswordResetRate is the number of password reset requests allowed per window.
	PasswordResetRate int
	// PasswordResetWindow is the time window for password reset rate limiting.
	PasswordResetWindow time.Duration
	// MFARate is the number of MFA attempts allowed per window.
	MFARate int
	// MFAWindow is the time window for MFA rate limiting.
	MFAWindow time.Duration
	// BlockDuration is how long to block after exceeding limits.
	BlockDuration time.Duration
}

AuthConfig holds configuration for authentication rate limiting.

func DefaultAuthConfig

func DefaultAuthConfig() AuthConfig

DefaultAuthConfig returns the default authentication rate limiting configuration.

type AuthLimiter

type AuthLimiter struct {
	// contains filtered or unexported fields
}

AuthLimiter provides specialized rate limiting for authentication endpoints.

func NewAuthLimiter

func NewAuthLimiter(cfg AuthConfig) *AuthLimiter

NewAuthLimiter creates a new authentication rate limiter.

func (*AuthLimiter) AllowLogin

func (a *AuthLimiter) AllowLogin(key string) bool

AllowLogin checks if a login attempt is allowed.

func (*AuthLimiter) AllowMFA

func (a *AuthLimiter) AllowMFA(key string) bool

AllowMFA checks if an MFA attempt is allowed.

func (*AuthLimiter) AllowPasswordReset

func (a *AuthLimiter) AllowPasswordReset(key string) bool

AllowPasswordReset checks if a password reset request is allowed.

func (*AuthLimiter) Block

func (a *AuthLimiter) Block(key string)

Block blocks a key for the configured duration.

func (*AuthLimiter) IsBlocked

func (a *AuthLimiter) IsBlocked(key string) bool

IsBlocked checks if a key is currently blocked.

func (*AuthLimiter) LoginMiddleware

func (a *AuthLimiter) LoginMiddleware() echo.MiddlewareFunc

LoginMiddleware returns middleware for login rate limiting.

func (*AuthLimiter) MFAMiddleware

func (a *AuthLimiter) MFAMiddleware() echo.MiddlewareFunc

MFAMiddleware returns middleware for MFA rate limiting.

func (*AuthLimiter) PasswordResetMiddleware

func (a *AuthLimiter) PasswordResetMiddleware() echo.MiddlewareFunc

PasswordResetMiddleware returns middleware for password reset rate limiting.

func (*AuthLimiter) Stop

func (a *AuthLimiter) Stop()

Stop stops all rate limiters.

func (*AuthLimiter) Unblock

func (a *AuthLimiter) Unblock(key string)

Unblock removes a block for a key.

type Config

type Config struct {
	// Rate is the number of requests allowed per window.
	Rate int
	// Window is the time window for rate limiting.
	Window time.Duration
	// KeyFunc extracts the client identifier from the request.
	// If nil, uses the client IP address.
	KeyFunc func(c echo.Context) string
	// ExceededHandler is called when rate limit is exceeded.
	// If nil, returns 429 Too Many Requests.
	ExceededHandler echo.HandlerFunc
	// CleanupInterval is how often to clean up expired entries.
	CleanupInterval time.Duration
}

Config holds rate limiter configuration.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a default rate limiter configuration.

type Limiter

type Limiter struct {
	// contains filtered or unexported fields
}

Limiter implements per-client rate limiting.

func New

func New(cfg Config) *Limiter

New creates a new rate limiter.

func (*Limiter) Allow

func (l *Limiter) Allow(key string) bool

Allow checks if a request is allowed for the given key.

func (*Limiter) Middleware

func (l *Limiter) Middleware() echo.MiddlewareFunc

Middleware returns an Echo middleware for rate limiting.

func (*Limiter) Remaining

func (l *Limiter) Remaining(key string) int

Remaining returns the number of remaining requests for the given key.

func (*Limiter) Reset

func (l *Limiter) Reset(key string) time.Time

Reset returns the time when the rate limit resets for the given key.

func (*Limiter) Stop

func (l *Limiter) Stop()

Stop stops the rate limiter cleanup goroutine.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL