middleware

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Auth

func Auth(next http.Handler) http.Handler

func IPFromRequest

func IPFromRequest(r *http.Request) string

IPFromRequest extracts the client IP, respecting X-Forwarded-For for reverse-proxy deployments.

func IPRateLimit

func IPRateLimit(rl *RateLimiter, limit int, window time.Duration) func(http.Handler) http.Handler

IPRateLimit returns middleware that limits requests by client IP.

func MessageRateLimit

func MessageRateLimit(rl *RateLimiter, ipLimit, pubkeyLimit int, window time.Duration) func(http.Handler) http.Handler

MessageRateLimit returns middleware for POST /{room}/messages. It applies an IP-based limit and, when a pubkey is present in the JSON body, a separate per-pubkey limit. The request body is buffered so the handler can still read it.

Types

type RateLimiter

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

RateLimiter is a sliding-window counter rate limiter backed by a sync.Map. It approximates a true sliding window using two consecutive fixed buckets:

rate ≈ prevCount*(1 - elapsed/window) + currCount

func NewRateLimiter

func NewRateLimiter(cleanupInterval time.Duration) *RateLimiter

NewRateLimiter creates a RateLimiter and starts a background goroutine that evicts stale entries every cleanupInterval.

func (*RateLimiter) Allow

func (rl *RateLimiter) Allow(key string, limit int, window time.Duration) bool

Allow returns true if the request identified by key is within the rate limit, and increments the counter. Returns false (without incrementing) when the estimated rate equals or exceeds limit.

Jump to

Keyboard shortcuts

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