limiter

package
v0.1.24 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetSupportedAlgorithms

func GetSupportedAlgorithms() []string

GetSupportedAlgorithms returns a list of registered algorithms

func RegisterAlgorithm

func RegisterAlgorithm(name string, factory AlgorithmFactory)

RegisterAlgorithm registers a new rate limiting algorithm

Types

type AlgorithmFactory

type AlgorithmFactory func(config Config) (Limiter, error)

AlgorithmFactory is a function that creates a limiter for a specific algorithm

type Clock

type Clock interface {
	Now() time.Time
}

Clock provides an abstraction for time operations (useful for testing)

type Config

type Config struct {
	Algorithm       string
	Limits          []LimitConfig
	Backend         string // "memory" or "redis"
	RedisClient     redis.UniversalClient
	KeyPrefix       string
	CleanupInterval time.Duration
	AlgorithmConfig map[string]interface{}
}

Config holds configuration for creating a rate limiter

type FixedClock

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

FixedClock returns a fixed time (for testing) Thread-safe for concurrent reads and writes

func NewFixedClock

func NewFixedClock(t time.Time) *FixedClock

NewFixedClock creates a new FixedClock with the given time

func (*FixedClock) Now

func (c *FixedClock) Now() time.Time

Now returns the fixed time (thread-safe)

func (*FixedClock) Set

func (c *FixedClock) Set(t time.Time)

Set updates the fixed time (thread-safe)

type LimitConfig

type LimitConfig struct {
	Limit    int64
	Duration time.Duration
	Burst    int64 // Optional, interpretation depends on algorithm
}

LimitConfig is algorithm-agnostic limit configuration

type Limiter

type Limiter interface {
	// Allow checks if a request is allowed for the given key
	// Returns a Result with rate limit information
	Allow(ctx context.Context, key string) (*Result, error)

	// AllowN checks if N requests are allowed for the given key
	AllowN(ctx context.Context, key string, n int64) (*Result, error)

	// GetAvailable returns the available tokens for the given key without consuming
	// This is useful for checking remaining capacity before making a request
	GetAvailable(ctx context.Context, key string) (int64, error)

	// Close cleans up limiter resources
	Close() error
}

Limiter is the main rate limiter interface (common to all algorithms)

func CreateLimiter

func CreateLimiter(config Config) (Limiter, error)

CreateLimiter creates a rate limiter based on the algorithm specified in config

type Result

type Result struct {
	// Allowed indicates whether the request is allowed
	Allowed bool

	// Limit is the maximum number of requests allowed in the time window
	Limit int64

	// Remaining is the number of requests remaining in the current window
	Remaining int64

	// Reset is the time when the rate limit will reset
	Reset time.Time

	// RetryAfter is the duration to wait before retrying (if rate limited)
	RetryAfter time.Duration

	// FullQuotaAt is the time when the full quota (all burst capacity) will be available
	// This is when TAT <= now, meaning all consumed tokens have regenerated
	FullQuotaAt time.Time

	// Duration is the rate limit window duration (common across all algorithms)
	Duration time.Duration

	// Policy that was evaluated (algorithm-specific)
	Policy interface{}
}

Result contains the outcome of a rate limit check

func (*Result) SetHeaders

func (r *Result) SetHeaders(w http.ResponseWriter)

SetHeaders sets all standard rate limit headers on an HTTP response

func (*Result) SetIETFHeaders

func (r *Result) SetIETFHeaders(w http.ResponseWriter)

SetIETFHeaders sets IETF draft standard headers Reference: draft-ietf-httpapi-ratelimit-headers-10

func (*Result) SetRetryAfterHeader

func (r *Result) SetRetryAfterHeader(w http.ResponseWriter)

SetRetryAfterHeader sets Retry-After header when rate limited (RFC 7231) This should only be set on 429 Too Many Requests responses

func (*Result) SetXRateLimitHeaders

func (r *Result) SetXRateLimitHeaders(w http.ResponseWriter)

SetXRateLimitHeaders sets X-RateLimit-* headers (de facto standard) Used by GitHub, Stripe, Twitter, and many other APIs

type SystemClock

type SystemClock struct{}

SystemClock uses the system time

func (*SystemClock) Now

func (c *SystemClock) Now() time.Time

Now returns the current system time

Jump to

Keyboard shortcuts

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