ratelimit

package
v0.80.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RateLimitingEnabledEnv        = "NB_API_RATE_LIMITING_ENABLED"
	RateLimitingBurstEnv          = "NB_API_RATE_LIMITING_BURST"
	RateLimitingRPMEnv            = "NB_API_RATE_LIMITING_RPM"
	RateLimitingTrustedProxiesEnv = "NB_API_RATE_LIMITING_TRUSTED_PROXIES"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type APIRateLimiter

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

APIRateLimiter manages rate limiting for API tokens

func NewAPIRateLimiter

func NewAPIRateLimiter(config *RateLimiterConfig) *APIRateLimiter

NewAPIRateLimiter creates a new API rate limiter with the given configuration

func (*APIRateLimiter) Allow

func (rl *APIRateLimiter) Allow(key string) bool

Allow checks if a request for the given key (token) is allowed

func (*APIRateLimiter) Enabled

func (rl *APIRateLimiter) Enabled() bool

func (*APIRateLimiter) Middleware

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

Middleware returns an HTTP middleware that rate limits requests by client IP. Returns 429 Too Many Requests if the rate limit is exceeded.

func (*APIRateLimiter) Reset

func (rl *APIRateLimiter) Reset(key string)

Reset removes the rate limiter for a specific key

func (*APIRateLimiter) SetEnabled

func (rl *APIRateLimiter) SetEnabled(enabled bool)

func (*APIRateLimiter) Stop

func (rl *APIRateLimiter) Stop()

Stop stops the cleanup goroutine

func (*APIRateLimiter) UpdateConfig

func (rl *APIRateLimiter) UpdateConfig(config *RateLimiterConfig)

func (*APIRateLimiter) Wait

func (rl *APIRateLimiter) Wait(ctx context.Context, key string) error

Wait blocks until the rate limiter allows another request for the given key Returns an error if the context is canceled

type RateLimiterConfig

type RateLimiterConfig struct {
	// RequestsPerMinute defines the rate at which tokens are replenished
	RequestsPerMinute float64
	// Burst defines the maximum number of requests that can be made in a burst
	Burst int
	// CleanupInterval defines how often to clean up old limiters (how often garbage collection runs)
	CleanupInterval time.Duration
	// LimiterTTL defines how long a limiter should be kept after last use (age threshold for removal)
	LimiterTTL time.Duration
	// TrustedProxies lists the upstream proxies whose forwarding headers may be
	// believed. Empty means requests are keyed by their direct peer address.
	TrustedProxies *trustedproxy.List
}

RateLimiterConfig holds configuration for the API rate limiter

func DefaultRateLimiterConfig

func DefaultRateLimiterConfig() *RateLimiterConfig

DefaultRateLimiterConfig returns a default configuration

func RateLimiterConfigFromEnv

func RateLimiterConfigFromEnv() (cfg *RateLimiterConfig, enabled bool)

Jump to

Keyboard shortcuts

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