Documentation
¶
Index ¶
- func NewLimiter(config limiter.Config) (limiter.Limiter, error)
- type MemoryLimiter
- func (m *MemoryLimiter) Allow(ctx context.Context, key string) (*limiter.Result, error)
- func (m *MemoryLimiter) AllowN(ctx context.Context, key string, n int64) (*limiter.Result, error)
- func (m *MemoryLimiter) Close() error
- func (m *MemoryLimiter) GetAvailable(ctx context.Context, key string) (int64, error)
- func (m *MemoryLimiter) WithClock(clock limiter.Clock) *MemoryLimiter
- type MultiLimiter
- func (m *MultiLimiter) Allow(ctx context.Context, key string) (*limiter.Result, error)
- func (m *MultiLimiter) AllowN(ctx context.Context, key string, n int64) (*limiter.Result, error)
- func (m *MultiLimiter) Close() error
- func (m *MultiLimiter) GetAvailable(ctx context.Context, key string) (int64, error)
- type Policy
- type RedisLimiter
- func (r *RedisLimiter) Allow(ctx context.Context, key string) (*limiter.Result, error)
- func (r *RedisLimiter) AllowN(ctx context.Context, key string, n int64) (*limiter.Result, error)
- func (r *RedisLimiter) Close() error
- func (r *RedisLimiter) GetAvailable(ctx context.Context, key string) (int64, error)
- func (r *RedisLimiter) WithClock(clock limiter.Clock) *RedisLimiter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type MemoryLimiter ¶
type MemoryLimiter struct {
// contains filtered or unexported fields
}
MemoryLimiter implements fixed window rate limiting with in-memory storage
func NewMemoryLimiter ¶
func NewMemoryLimiter(policy *Policy, cleanupInterval time.Duration) *MemoryLimiter
NewMemoryLimiter creates a new in-memory fixed window rate limiter policy: Rate limit policy defining limit and window duration cleanupInterval: How often expired entries are removed (0 to disable, recommended: 5 minutes)
func (*MemoryLimiter) AllowN ¶
AllowN checks if N requests are allowed for the given key Atomically consumes N request tokens if allowed
func (*MemoryLimiter) Close ¶
func (m *MemoryLimiter) Close() error
Close stops the cleanup goroutine and releases resources Safe to call multiple times
func (*MemoryLimiter) GetAvailable ¶ added in v0.1.22
GetAvailable returns the available tokens for the given key without consuming
func (*MemoryLimiter) WithClock ¶
func (m *MemoryLimiter) WithClock(clock limiter.Clock) *MemoryLimiter
WithClock sets a custom clock (for testing)
type MultiLimiter ¶
type MultiLimiter struct {
// contains filtered or unexported fields
}
MultiLimiter supports multiple concurrent rate limit policies It checks all limiters and returns the most restrictive result
func NewMultiLimiter ¶
func NewMultiLimiter(limiters ...limiter.Limiter) *MultiLimiter
NewMultiLimiter creates a limiter that enforces multiple policies Each policy is checked independently, and the most restrictive result is returned Example: Combine a short-term (10/second) and long-term (1000/hour) rate limit
func (*MultiLimiter) Allow ¶
Allow checks if a single request is allowed against all policies Returns the most restrictive result (fail-fast on first denial)
func (*MultiLimiter) AllowN ¶
AllowN checks if N requests are allowed against all policies Returns the most restrictive result (fail-fast on first denial)
func (*MultiLimiter) Close ¶
func (m *MultiLimiter) Close() error
Close closes all limiters Safe to call multiple times
func (*MultiLimiter) GetAvailable ¶ added in v0.1.22
GetAvailable returns the minimum available tokens across all policies
type Policy ¶
type Policy struct {
// Limit is the maximum number of requests allowed per window
Limit int64
// Duration is the window duration (e.g., 1 second, 1 minute, 1 hour)
Duration time.Duration
}
Policy defines a fixed window rate limit policy Fixed window divides time into fixed-duration intervals and counts requests per interval
func NewPolicy ¶
NewPolicy creates a new fixed window rate limit policy limit: maximum number of requests allowed in the window duration: time window duration
type RedisLimiter ¶
type RedisLimiter struct {
// contains filtered or unexported fields
}
RedisLimiter implements fixed window rate limiting with Redis backend
func NewRedisLimiter ¶
func NewRedisLimiter(client redis.UniversalClient, policy *Policy, keyPrefix string) *RedisLimiter
NewRedisLimiter creates a new Redis-backed fixed window rate limiter client: Redis client for storage policy: Rate limit policy defining limit and window duration keyPrefix: Prefix for all Redis keys (e.g., "ratelimit:v1:")
func (*RedisLimiter) AllowN ¶
AllowN checks if N requests are allowed for the given key Uses atomic INCRBY in Redis - no Lua script needed
func (*RedisLimiter) Close ¶
func (r *RedisLimiter) Close() error
Close releases resources (no-op for Redis as connections are managed externally) Safe to call multiple times
func (*RedisLimiter) GetAvailable ¶ added in v0.1.22
GetAvailable returns the available tokens for the given key without consuming
func (*RedisLimiter) WithClock ¶
func (r *RedisLimiter) WithClock(clock limiter.Clock) *RedisLimiter
WithClock sets a custom clock (for testing)