ratelimit

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package ratelimit provides a sliding-window HTTP rate limiter middleware.

Requests that exceed the limit receive a 429 Too Many Requests response with standard X-RateLimit-* and Retry-After headers. The rate-limiting key defaults to the client IP address but is fully configurable.

Quick start:

store := ratelimit.NewMemoryStore()
limiter := ratelimit.New(store, ratelimit.Config{
    Limit:  100,
    Window: time.Minute,
})
mux.Handle("/api/", limiter(apiHandler))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ByIP

func ByIP(r *http.Request) string

ByIP returns a KeyFn that rate-limits per client IP address. This is the default KeyFn.

func ByRoute

func ByRoute(r *http.Request) string

ByRoute returns a KeyFn that combines the client IP with the URL path, allowing different limits per route without separate middleware instances.

func BySubject

func BySubject(subjectFn func(r *http.Request) string) func(r *http.Request) string

BySubject returns a KeyFn that rate-limits per JWT subject stored in the request context. Falls back to the remote IP when no claims are present. Pair with auth/middleware.JWT to rate-limit authenticated users individually.

func New

func New(store Store, cfg Config) func(http.Handler) http.Handler

New returns an HTTP middleware that enforces the given rate limit using store.

Types

type Config

type Config struct {
	// Limit is the maximum number of requests allowed within Window.
	Limit int64
	// Window is the sliding-window duration (e.g. time.Minute).
	Window time.Duration
	// KeyFn derives the rate-limit key from the request.
	// Defaults to the client's remote IP address when nil.
	KeyFn func(r *http.Request) string
	// OnLimited is called when a request is rejected.
	// Defaults to a JSON 429 response when nil.
	OnLimited func(w http.ResponseWriter, r *http.Request, reset time.Time)
}

Config configures a rate limiter middleware instance.

func Per

func Per(requests int64, window time.Duration) Config

Per returns a helper that expresses rates as "n requests per duration" and can be used to compute Limit/Window pairs more readably:

cfg := ratelimit.Per(100, time.Minute) // 100 req/min

type MemoryStore

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

MemoryStore is a thread-safe, in-process Store implementation using a sliding-window counter algorithm. It is suitable for single-process deployments. For horizontally-scaled services use a shared store (e.g. Redis).

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore returns an initialised MemoryStore.

func (*MemoryStore) Inc

func (s *MemoryStore) Inc(key string, window time.Duration) (int64, error)

func (*MemoryStore) Reset

func (s *MemoryStore) Reset(key string) error

type Store

type Store interface {
	// Inc atomically increments the counter for key within the given window and
	// returns the current count. The store must expire the key after window elapses.
	Inc(key string, window time.Duration) (count int64, err error)
	// Reset clears the counter for key immediately.
	Reset(key string) error
}

Store is the backend for tracking request counts. Implement this interface to back the rate limiter with Redis, Memcached, etc.

Jump to

Keyboard shortcuts

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