ratelimiter

package
v1.76.3 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCacheDoesNotSupportPerKeyTTL = errors.New("cache backend does not support per-key TTL")

Functions

func GetIP

func GetIP(r *http.Request) string

GetIP extracts caller IP from request headers/remote address.

func GetUserID

func GetUserID(ctx context.Context) string

GetUserID extracts user identity from frame auth claims in context.

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.

func (*WindowLimiter) Allow

func (wl *WindowLimiter) Allow(ctx context.Context, key string) bool

Allow checks whether key is still within configured window limit.

Jump to

Keyboard shortcuts

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