Documentation
¶
Index ¶
- Variables
- func GetIP(r *http.Request) string
- func GetUserID(ctx context.Context) string
- func RateLimitMiddleware(limiter *IPRateLimiter) func(http.Handler) http.Handler
- func UserRateLimitMiddleware(userLimiter *UserRateLimiter, ipLimiter *IPRateLimiter) func(http.Handler) http.Handler
- type IPRateLimiter
- type UserRateLimiter
- type WindowConfig
- type WindowLimiter
Constants ¶
This section is empty.
Variables ¶
var ErrCacheDoesNotSupportPerKeyTTL = errors.New("cache backend does not support per-key TTL")
Functions ¶
func RateLimitMiddleware ¶
func RateLimitMiddleware(limiter *IPRateLimiter) func(http.Handler) http.Handler
RateLimitMiddleware applies cache-backed IP rate limiting.
func UserRateLimitMiddleware ¶
func UserRateLimitMiddleware(userLimiter *UserRateLimiter, ipLimiter *IPRateLimiter) func(http.Handler) http.Handler
UserRateLimitMiddleware applies user-based limiting and falls back to IP for unauthenticated requests.
Types ¶
type IPRateLimiter ¶
type IPRateLimiter struct {
// contains filtered or unexported fields
}
IPRateLimiter applies cache-backed per-IP window limits.
func NewIPRateLimiter ¶
func NewIPRateLimiter(raw cache.RawCache, config *WindowConfig) (*IPRateLimiter, error)
NewIPRateLimiter creates a new cache-backed IP rate limiter. If raw is nil, an in-memory frame cache is created.
func (*IPRateLimiter) Allow ¶
func (rl *IPRateLimiter) Allow(ctx context.Context, ip string) bool
Allow checks whether a request from the given IP should be allowed.
func (*IPRateLimiter) Close ¶
func (rl *IPRateLimiter) Close() error
Close releases owned resources.
type UserRateLimiter ¶
type UserRateLimiter struct {
// contains filtered or unexported fields
}
UserRateLimiter applies cache-backed per-user window limits.
func NewUserRateLimiter ¶
func NewUserRateLimiter(raw cache.RawCache, config *WindowConfig) (*UserRateLimiter, error)
NewUserRateLimiter creates a new cache-backed user rate limiter. If raw is nil, an in-memory frame cache is created.
func (*UserRateLimiter) Allow ¶
func (rl *UserRateLimiter) Allow(ctx context.Context, userID string) bool
Allow checks whether a request from the given user should be allowed.
func (*UserRateLimiter) Close ¶
func (rl *UserRateLimiter) Close() error
Close releases owned resources.
type WindowConfig ¶
type WindowConfig struct {
WindowDuration time.Duration
MaxPerWindow int
KeyPrefix string
FailOpen bool
}
WindowConfig defines fixed-window counter limiter settings backed by cache.
func DefaultWindowConfig ¶
func DefaultWindowConfig() *WindowConfig
DefaultWindowConfig returns conservative cache-backed limiter defaults.
type WindowLimiter ¶
type WindowLimiter struct {
// contains filtered or unexported fields
}
WindowLimiter enforces per-key fixed-window limits using atomic cache increments.
func NewWindowLimiter ¶
func NewWindowLimiter(raw cache.RawCache, cfg *WindowConfig) (*WindowLimiter, error)
NewWindowLimiter creates a cache-backed window limiter.