ratelimit

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// RequestsPerMinute defines the maximum requests allowed per minute
	RequestsPerMinute int

	// BurstSize defines the maximum burst of requests allowed
	BurstSize int

	// TimeWindow defines the time window for rate limiting
	TimeWindow time.Duration

	// PerUser enables per-user rate limiting
	PerUser bool

	// PerEndpoint enables per-endpoint rate limiting
	PerEndpoint bool

	// EndpointLimits defines custom limits for specific endpoints
	EndpointLimits map[string]int

	// Storage defines the storage backend type
	StorageType string

	// RedisURL is the Redis connection URL (if using Redis storage)
	RedisURL string
}

Config contains rate limiter configuration

type EndpointStat

type EndpointStat struct {
	Endpoint string
	Count    uint64
}

EndpointStat contains statistics for a single endpoint

type Limiter

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

Limiter implements rate limiting using the token bucket algorithm

func NewLimiter

func NewLimiter(storage Storage, config Config) *Limiter

NewLimiter creates a new rate limiter

func (*Limiter) Allow

func (l *Limiter) Allow(ctx context.Context, key string) (*Result, error)

Allow checks if a request is allowed for the given key

func (*Limiter) AllowN

func (l *Limiter) AllowN(ctx context.Context, key string, n int) (*Result, error)

AllowN checks if N requests are allowed for the given key

func (*Limiter) BuildKey

func (l *Limiter) BuildKey(components ...string) string

BuildKey builds a rate limit key from the given components

func (*Limiter) Close

func (l *Limiter) Close() error

Close closes the rate limiter and its storage backend

func (*Limiter) GetLimitForEndpoint

func (l *Limiter) GetLimitForEndpoint(endpoint string) int

GetLimitForEndpoint returns the rate limit for a specific endpoint

func (*Limiter) Reset

func (l *Limiter) Reset(ctx context.Context, key string) error

Reset resets the rate limit for the given key

type MemoryStorage

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

MemoryStorage implements in-memory rate limiting storage

func NewMemoryStorage

func NewMemoryStorage() *MemoryStorage

NewMemoryStorage creates a new in-memory storage backend

func (*MemoryStorage) Close

func (s *MemoryStorage) Close() error

Close closes the storage backend

func (*MemoryStorage) Get

func (s *MemoryStorage) Get(ctx context.Context, key string) (int64, time.Time, error)

Get retrieves the current count and reset time for a key

func (*MemoryStorage) Increment

func (s *MemoryStorage) Increment(ctx context.Context, key string, window time.Duration) (int64, time.Time, error)

Increment increments the counter for a key and returns the new count

func (*MemoryStorage) Reset

func (s *MemoryStorage) Reset(ctx context.Context, key string) error

Reset resets the counter for a key

type Metrics

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

Metrics tracks rate limiting statistics

func NewMetrics

func NewMetrics() *Metrics

NewMetrics creates a new metrics tracker

func (*Metrics) GetBlockedRate

func (m *Metrics) GetBlockedRate() float64

GetBlockedRate returns the percentage of blocked requests

func (*Metrics) GetStats

func (m *Metrics) GetStats() Stats

GetStats returns current statistics

func (*Metrics) GetTopEndpoints

func (m *Metrics) GetTopEndpoints(n int) []EndpointStat

GetTopEndpoints returns the top N endpoints by request count

func (*Metrics) GetTopUsers

func (m *Metrics) GetTopUsers(n int) []UserStat

GetTopUsers returns the top N users by request count

func (*Metrics) RecordRequest

func (m *Metrics) RecordRequest(allowed bool, endpoint string, user string)

RecordRequest records a request

func (*Metrics) Reset

func (m *Metrics) Reset()

Reset resets all metrics

type RedisStorage

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

RedisStorage implements Redis-based rate limiting storage

func NewRedisStorage

func NewRedisStorage(url string, prefix string) (*RedisStorage, error)

NewRedisStorage creates a new Redis storage backend

func (*RedisStorage) Close

func (s *RedisStorage) Close() error

Close closes the storage backend

func (*RedisStorage) Get

func (s *RedisStorage) Get(ctx context.Context, key string) (int64, time.Time, error)

Get retrieves the current count and reset time for a key

func (*RedisStorage) Increment

func (s *RedisStorage) Increment(ctx context.Context, key string, window time.Duration) (int64, time.Time, error)

Increment increments the counter for a key and returns the new count

func (*RedisStorage) Reset

func (s *RedisStorage) Reset(ctx context.Context, key string) error

Reset resets the counter for a key

type Result

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

	// Limit is the maximum number of requests allowed
	Limit int64

	// Remaining is the number of requests remaining
	Remaining int64

	// ResetAt is when the rate limit resets
	ResetAt time.Time

	// RetryAfter is the duration to wait before retrying (if blocked)
	RetryAfter time.Duration
}

Result contains the result of a rate limit check

type Stats

type Stats struct {
	TotalRequests    uint64
	AllowedRequests  uint64
	BlockedRequests  uint64
	EndpointRequests map[string]uint64
	UserRequests     map[string]uint64
	LastReset        time.Time
}

Stats contains rate limiting statistics

type Storage

type Storage interface {
	// Get retrieves the current count and reset time for a key
	Get(ctx context.Context, key string) (count int64, resetAt time.Time, err error)

	// Increment increments the counter for a key and returns the new count
	Increment(ctx context.Context, key string, window time.Duration) (count int64, resetAt time.Time, err error)

	// Reset resets the counter for a key
	Reset(ctx context.Context, key string) error

	// Close closes the storage backend
	Close() error
}

Storage defines the interface for rate limit storage backends

type UserStat

type UserStat struct {
	User  string
	Count uint64
}

UserStat contains statistics for a single user

Jump to

Keyboard shortcuts

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