Documentation
¶
Index ¶
- type Config
- type EndpointStat
- type Limiter
- func (l *Limiter) Allow(ctx context.Context, key string) (*Result, error)
- func (l *Limiter) AllowN(ctx context.Context, key string, n int) (*Result, error)
- func (l *Limiter) BuildKey(components ...string) string
- func (l *Limiter) Close() error
- func (l *Limiter) GetLimitForEndpoint(endpoint string) int
- func (l *Limiter) Reset(ctx context.Context, key string) error
- type MemoryStorage
- func (s *MemoryStorage) Close() error
- func (s *MemoryStorage) Get(ctx context.Context, key string) (int64, time.Time, error)
- func (s *MemoryStorage) Increment(ctx context.Context, key string, window time.Duration) (int64, time.Time, error)
- func (s *MemoryStorage) Reset(ctx context.Context, key string) error
- type Metrics
- type RedisStorage
- func (s *RedisStorage) Close() error
- func (s *RedisStorage) Get(ctx context.Context, key string) (int64, time.Time, error)
- func (s *RedisStorage) Increment(ctx context.Context, key string, window time.Duration) (int64, time.Time, error)
- func (s *RedisStorage) Reset(ctx context.Context, key string) error
- type Result
- type Stats
- type Storage
- type UserStat
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 ¶
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 ¶
NewLimiter creates a new rate limiter
func (*Limiter) GetLimitForEndpoint ¶
GetLimitForEndpoint returns the rate limit for a specific endpoint
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
type Metrics ¶
type Metrics struct {
// contains filtered or unexported fields
}
Metrics tracks rate limiting statistics
func (*Metrics) GetBlockedRate ¶
GetBlockedRate returns the percentage of blocked requests
func (*Metrics) GetTopEndpoints ¶
func (m *Metrics) GetTopEndpoints(n int) []EndpointStat
GetTopEndpoints returns the top N endpoints by request count
func (*Metrics) GetTopUsers ¶
GetTopUsers returns the top N users by request count
func (*Metrics) RecordRequest ¶
RecordRequest records a request
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
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