ratelimit

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2026 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package ratelimit provides rate limiting middleware for the HTTP server. Uses a sliding window algorithm with in-memory storage.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IPAndPathKeyFunc

func IPAndPathKeyFunc(c *gin.Context) string

IPAndPathKeyFunc uses IP + path as the key (for per-endpoint limiting).

func IPKeyFunc

func IPKeyFunc(c *gin.Context) string

IPKeyFunc uses the client IP as the rate limit key.

Types

type Config

type Config struct {
	Enabled bool

	// Login rate limiting (strict)
	LoginLimit  int           // requests per window
	LoginWindow time.Duration // window duration

	// Public API rate limiting (moderate)
	APILimit  int
	APIWindow time.Duration

	// Submission rate limiting (comments, feedback, newsletter)
	SubmitLimit  int
	SubmitWindow time.Duration
}

Config holds rate limiter configuration.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns sensible defaults.

type KeyFunc

type KeyFunc func(c *gin.Context) string

KeyFunc extracts a rate limit key from a request.

type Limiter

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

Limiter tracks request counts per key using a sliding window.

func New

func New(limit int, windowDuration time.Duration) *Limiter

New creates a new rate limiter.

func (*Limiter) Allow

func (l *Limiter) Allow(key string) (bool, int, time.Time)

Allow checks if a request should be allowed for the given key. Returns (allowed, remaining, resetTime).

func (*Limiter) Middleware

func (l *Limiter) Middleware(keyFunc KeyFunc) gin.HandlerFunc

Middleware creates a Gin middleware that applies rate limiting.

func (*Limiter) Reset

func (l *Limiter) Reset(key string)

Reset clears the rate limit for a key (e.g., after successful login).

func (*Limiter) Stop

func (l *Limiter) Stop()

Stop stops the cleanup goroutine.

Jump to

Keyboard shortcuts

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