ratelimiter

package
v0.0.2-beta.3 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewNewRateLimiterByClientIP

func NewNewRateLimiterByClientIP(limit time.Duration, burst int, expire time.Duration, options ...Option) httpx.Middleware

func NewRateLimiter

func NewRateLimiter(key func(httpx.Context) string, createLimiter func(httpx.Context) (*rate.Limiter, time.Duration), options ...Option) httpx.Middleware

NewRateLimiter creates a new rate limiting middleware with customizable key extraction and limiter creation. It uses caching to store rate limiters per key and singleflight to prevent cache stampedes.

Types

type Option

type Option func(*options)

Option is a functional option for configuring the rate limiter middleware.

func WithAbort

func WithAbort(fn func(ctx httpx.Context, status int, err error)) Option

WithAbort sets a custom error handler for rate limit violations.

func WithCache

func WithCache(cache cache.Cache[*rate.Limiter]) Option

WithCache sets a custom cache implementation for storing rate limiters. The default cache is an in-memory cache.

func WithSetTTL

func WithSetTTL(ttl time.Duration) Option

WithSetTTL sets the timeout for cache set operations. This prevents hanging when the cache backend is unresponsive.

Jump to

Keyboard shortcuts

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