Documentation
¶
Index ¶
- func Auth(next http.Handler) http.Handler
- func IPFromRequest(r *http.Request) string
- func IPRateLimit(rl *RateLimiter, limit int, window time.Duration) func(http.Handler) http.Handler
- func MessageRateLimit(rl *RateLimiter, ipLimit, pubkeyLimit int, window time.Duration) func(http.Handler) http.Handler
- type RateLimiter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IPFromRequest ¶
IPFromRequest extracts the client IP, respecting X-Forwarded-For for reverse-proxy deployments.
func IPRateLimit ¶
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.