ratelimit

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RateLimitMiddleware

func RateLimitMiddleware(rl *RateLimiter, keyFunc func(*http.Request) string) func(http.Handler) http.Handler

RateLimitMiddleware returns middleware that limits requests, keyed by keyFunc (e.g. client IP).

Types

type RateLimiter

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

RateLimiter is a token bucket per key (a client's address, a user, an account's name). An IPv6 address counts as its /64 network: one host is given a whole one, and would get a new bucket per address.

func NewRateLimiter

func NewRateLimiter(requestsPerMinute int, burst int) *RateLimiter

NewRateLimiter allows burst requests at once, then requestsPerMinute.

func NewRateLimiterEvery

func NewRateLimiterEvery(interval time.Duration, burst int) *RateLimiter

NewRateLimiterEvery allows burst requests at once, then one every interval (for rare actions: an hour, a day).

func (*RateLimiter) Allow

func (rl *RateLimiter) Allow(key string) bool

Allow takes a request of key from its bucket: false when it is empty. For a key a middleware can't tell (one from the form, after a check).

func (*RateLimiter) Reject

func (rl *RateLimiter) Reject(w http.ResponseWriter, r *http.Request)

Reject answers a request over the limit: 429, Retry-After (the seconds until a request is allowed again) and a message in the request's language - {"error": …} to a script (wantsJSON), the text to a form.

Jump to

Keyboard shortcuts

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