ratelimit

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package ratelimit provides rate limiting middleware for rex routers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(config Config) rex.Middleware

New creates a new RateLimit middleware with the given configuration.

Types

type Config

type Config struct {
	// Rate is the number of requests allowed per second.
	Rate float64

	// Capacity is the maximum burst size.
	Capacity float64

	// KeyFunc generates a key for the request (e.g. IP address).
	// Defaults to c.IP() if nil.
	KeyFunc func(c *rex.Context) string

	// Expiration is the duration after which an idle limiter is removed from memory.
	// Default: 1 minute.
	Expiration time.Duration

	// ErrorHandler is called when the limit is exceeded.
	// Default: returns 429 Too Many Requests.
	ErrorHandler func(c *rex.Context) error
}

Config defines the configuration for the RateLimit middleware.

type Manager

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

Manager manages multiple token buckets (e.g. per IP).

func NewManager

func NewManager(rate, capacity float64, expiration time.Duration) *Manager

NewManager creates a new rate limiter manager. rate: tokens per second. capacity: max burst. expiration: how long to keep an idle bucket in memory.

func (*Manager) Allow

func (m *Manager) Allow(key string) bool

Allow checks if the key is allowed.

Jump to

Keyboard shortcuts

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