ratelimit

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AdaptiveRateLimitMiddleware

func AdaptiveRateLimitMiddleware(baseConfig *RateLimitConfig, logger *logrus.Logger) gin.HandlerFunc

AdaptiveRateLimitMiddleware rate limiting adaptativo basado en load

func DefaultKeyGenerator

func DefaultKeyGenerator(c *gin.Context) string

DefaultKeyGenerator generador de clave por defecto (por IP)

func PathKeyGenerator

func PathKeyGenerator(c *gin.Context) string

PathKeyGenerator generador de clave por path + IP

func PerPathRateLimitMiddleware

func PerPathRateLimitMiddleware(pathLimits map[string]*RateLimitConfig, logger *logrus.Logger) gin.HandlerFunc

PerPathRateLimitMiddleware rate limiting por path específico

func RateLimitMiddleware

func RateLimitMiddleware(rateLimiter RateLimiter, config *RateLimitConfig) gin.HandlerFunc

RateLimitMiddleware middleware de rate limiting

func RateLimitStatsHandler

func RateLimitStatsHandler(rateLimiter RateLimiter, config *RateLimitConfig) gin.HandlerFunc

RateLimitStatsHandler handler para obtener estadísticas de rate limiting

func TenantKeyGenerator

func TenantKeyGenerator(c *gin.Context) string

TenantKeyGenerator generador de clave por tenant

func UserKeyGenerator

func UserKeyGenerator(c *gin.Context) string

UserKeyGenerator generador de clave por usuario

Types

type KeyGeneratorFunc

type KeyGeneratorFunc func(c *gin.Context) string

KeyGeneratorFunc función para generar la clave de rate limiting

type MemoryRateLimiter

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

MemoryRateLimiter implementación en memoria

func NewMemoryRateLimiter

func NewMemoryRateLimiter(config *RateLimitConfig, logger *logrus.Logger) *MemoryRateLimiter

NewMemoryRateLimiter crea un rate limiter en memoria

func (*MemoryRateLimiter) Allow

func (mrl *MemoryRateLimiter) Allow(ctx context.Context, key string) (*RateLimit, error)

Allow verifica si se permite la request

func (*MemoryRateLimiter) Close

func (mrl *MemoryRateLimiter) Close()

Close cierra el rate limiter

func (*MemoryRateLimiter) GetStats

func (mrl *MemoryRateLimiter) GetStats(ctx context.Context, key string) (*RateLimit, error)

GetStats obtiene estadísticas para una clave

func (*MemoryRateLimiter) Reset

func (mrl *MemoryRateLimiter) Reset(ctx context.Context, key string) error

Reset resetea el rate limit para una clave

type RateLimit

type RateLimit struct {
	Key             string
	Limit           int
	Remaining       int
	Reset           time.Time
	ResetDuration   time.Duration
	WindowStart     time.Time
	RequestCount    int
	LastRequestTime time.Time
}

RateLimit representa el estado del rate limiting

type RateLimitConfig

type RateLimitConfig struct {
	// Rate limiting settings
	RequestsPerSecond int
	RequestsPerMinute int
	RequestsPerHour   int
	BurstSize         int

	// Window settings
	WindowSize time.Duration

	// Key generation
	KeyGenerator KeyGeneratorFunc

	// Skip conditions
	SkipPaths    []string
	SkipIPs      []string
	WhitelistIPs []string

	// Response settings
	IncludeHeaders bool
	CustomMessage  string

	// Storage
	UseRedis    bool
	RedisClient *redis.Client
	RedisPrefix string

	// Callbacks
	OnLimitExceeded func(c *gin.Context, limit *RateLimit)
	OnReset         func(c *gin.Context, limit *RateLimit)
}

RateLimitConfig configuración del rate limiter

func DefaultRateLimitConfig

func DefaultRateLimitConfig() *RateLimitConfig

DefaultRateLimitConfig retorna configuración por defecto

type RateLimiter

type RateLimiter interface {
	Allow(ctx context.Context, key string) (*RateLimit, error)
	Reset(ctx context.Context, key string) error
	GetStats(ctx context.Context, key string) (*RateLimit, error)
}

RateLimiter interfaz para implementaciones de rate limiting

func NewRateLimiter

func NewRateLimiter(config *RateLimitConfig, logger *logrus.Logger) RateLimiter

NewRateLimiter crea un rate limiter basado en la configuración

type RedisRateLimiter

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

RedisRateLimiter implementación con Redis

func NewRedisRateLimiter

func NewRedisRateLimiter(config *RateLimitConfig, logger *logrus.Logger) *RedisRateLimiter

NewRedisRateLimiter crea un rate limiter con Redis

func (*RedisRateLimiter) Allow

func (rrl *RedisRateLimiter) Allow(ctx context.Context, key string) (*RateLimit, error)

Allow verifica si se permite la request usando Redis

func (*RedisRateLimiter) GetStats

func (rrl *RedisRateLimiter) GetStats(ctx context.Context, key string) (*RateLimit, error)

GetStats obtiene estadísticas desde Redis

func (*RedisRateLimiter) Reset

func (rrl *RedisRateLimiter) Reset(ctx context.Context, key string) error

Reset resetea el rate limit en Redis

Jump to

Keyboard shortcuts

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