middleware

package
v1.0.8 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultKeyFunc

func DefaultKeyFunc(r *http.Request) string

DefaultKeyFunc generates a rate limit key from the request

func FeatureGuard

func FeatureGuard(featureKey string, next http.Handler) http.Handler

FeatureGuard middleware checks if the current license tier allows access to a feature

func FeatureGuardFunc

func FeatureGuardFunc(featureKey string, next func(http.ResponseWriter, *http.Request)) http.Handler

FeatureGuardFunc is a convenience wrapper for http.HandlerFunc

func RequireFeature

func RequireFeature(featureKeys ...string) func(http.Handler) http.Handler

RequireFeature checks if a specific feature is available

func RequireTier

func RequireTier(minimumTier core.Tier, next http.Handler) http.Handler

RequireTier ensures the request comes from an account with minimum tier

func TierBasedResponse

func TierBasedResponse(next http.Handler) http.Handler

TierBasedResponse modifies the response based on tier

Types

type CombinedLimiter

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

CombinedLimiter combines rate limiting and connection limiting

func NewCombinedLimiter

func NewCombinedLimiter(tier core.Tier) *CombinedLimiter

NewCombinedLimiter creates a new combined limiter

func (*CombinedLimiter) GetTier

func (cl *CombinedLimiter) GetTier() core.Tier

GetTier returns the current tier

func (*CombinedLimiter) Middleware

func (cl *CombinedLimiter) Middleware(next http.Handler) http.Handler

Middleware returns the combined limiting middleware Apply connection limit first, then rate limit

func (*CombinedLimiter) UpdateTier

func (cl *CombinedLimiter) UpdateTier(tier core.Tier)

UpdateTier updates both limiters to use new tier limits

type ConnectionLimiter

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

ConnectionLimiter limits concurrent connections

func NewConnectionLimiter

func NewConnectionLimiter(maxPerClient, globalMax int) *ConnectionLimiter

NewConnectionLimiter creates a new connection limiter

func (*ConnectionLimiter) Acquire

func (cl *ConnectionLimiter) Acquire(r *http.Request) bool

Acquire attempts to acquire a connection slot Returns true if acquired, false if at limit

func (*ConnectionLimiter) GetStats

func (cl *ConnectionLimiter) GetStats() (perClient map[string]int, global int)

GetStats returns current connection statistics

func (*ConnectionLimiter) Middleware

func (cl *ConnectionLimiter) Middleware(next http.Handler) http.Handler

Middleware returns the connection limiting middleware

func (*ConnectionLimiter) Release

func (cl *ConnectionLimiter) Release(r *http.Request)

Release releases a connection slot

type RateLimitConfig

type RateLimitConfig struct {
	// RequestsPerMinute - max requests allowed per minute
	RequestsPerMinute int
	// Burst - max burst requests allowed (above steady rate)
	Burst int
	// BlockDuration - how long to block after limit exceeded
	BlockDuration time.Duration
	// KeyFunc - function to generate unique key for rate limiting
	KeyFunc func(r *http.Request) string
}

RateLimitConfig holds rate limiting configuration

func DefaultRateLimitConfig

func DefaultRateLimitConfig(tier core.Tier) RateLimitConfig

DefaultRateLimitConfig returns default configuration based on tier

type RateLimitStatusHandler

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

RateLimitStatusHandler returns JSON status about rate limits

func NewRateLimitStatusHandler

func NewRateLimitStatusHandler(limiter *CombinedLimiter) *RateLimitStatusHandler

NewRateLimitStatusHandler creates a new status handler

func (*RateLimitStatusHandler) ServeHTTP

ServeHTTP returns rate limit status as JSON

type RateLimiter

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

RateLimiter implements per-client rate limiting using token bucket

func NewRateLimiter

func NewRateLimiter(config RateLimitConfig) *RateLimiter

NewRateLimiter creates a new rate limiter with the given configuration

func (*RateLimiter) Allow

func (rl *RateLimiter) Allow(r *http.Request) bool

Allow checks if a request is allowed under rate limits Returns true if allowed, false if rate limited

func (*RateLimiter) GetRateLimitConfig

func (rl *RateLimiter) GetRateLimitConfig() RateLimitConfig

GetRateLimitConfig returns the current configuration

func (*RateLimiter) Middleware

func (rl *RateLimiter) Middleware(next http.Handler) http.Handler

Middleware returns an HTTP middleware for rate limiting

func (*RateLimiter) Stop

func (rl *RateLimiter) Stop()

Stop stops the rate limiter cleanup goroutine

type TierConnectionLimiter

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

TierConnectionLimiter creates a connection limiter based on tier

func NewTierConnectionLimiter

func NewTierConnectionLimiter(tier core.Tier) *TierConnectionLimiter

NewTierConnectionLimiter creates a new tier-aware connection limiter

func (*TierConnectionLimiter) Middleware

func (tcl *TierConnectionLimiter) Middleware(next http.Handler) http.Handler

Middleware returns the connection limiting middleware

func (*TierConnectionLimiter) UpdateTier

func (tcl *TierConnectionLimiter) UpdateTier(tier core.Tier)

UpdateTier updates the connection limiter to use new tier limits

type TierRateLimiter

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

TierRateLimiter creates a rate limiter based on the tier configuration

func NewTierRateLimiter

func NewTierRateLimiter(tier core.Tier) *TierRateLimiter

NewTierRateLimiter creates a new tier-aware rate limiter

func (*TierRateLimiter) GetTier

func (trl *TierRateLimiter) GetTier() core.Tier

GetTier returns the current tier

func (*TierRateLimiter) Middleware

func (trl *TierRateLimiter) Middleware(next http.Handler) http.Handler

Middleware returns the rate limiting middleware

func (*TierRateLimiter) UpdateTier

func (trl *TierRateLimiter) UpdateTier(tier core.Tier)

UpdateTier updates the rate limiter to use new tier limits

Jump to

Keyboard shortcuts

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