Documentation
¶
Overview ¶
Package ratelimiter provides a distributed rate limiter for Go using Redis and the Sliding Window Counter algorithm.
This package is designed for high-performance, concurrent systems and ensures atomic rate-limiting operations across multiple instances by leveraging Redis Lua scripting.
Typical use cases include:
- API rate limiting
- Distributed systems throttling
- Per-user or per-IP request control
See the README for integration examples and framework-specific middleware.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
Limit int // Maximum number of allowed requests.
Window time.Duration // The duration of the sliding window.
}
Config holds the parameters for the rate limiting window.
type Limiter ¶
type Limiter interface {
// Allow checks if a request for the given key is permitted within the configured time window.
// Returns a Result containing the current status or an error if the store is unreachable.
Allow(ctx context.Context, key string) (*Result, error)
}
Limiter defines the contract for rate limiting implementations. It allows for different backend stores (Redis, In-Memory, etc.) to be used interchangeably.
type Option ¶
type Option func(*Config)
Option is a functional configuration for the RedisStore.
func WithWindow ¶
WithWindow sets the duration for the rate limiting sliding window.
type RedisStore ¶
type RedisStore struct {
// contains filtered or unexported fields
}
RedisStore implements the Limiter interface using Redis as the backend. It utilizes Lua scripting to ensure atomic increments and window management.
func NewRedisStore ¶
func NewRedisStore(client *redis.Client, opts ...Option) *RedisStore
NewRedisStore initializes a new RedisStore with the provided Redis client and options. If no options are provided, it defaults to a limit of 100 requests per minute.
type Result ¶
type Result struct {
Allowed bool // True if the request is permitted.
Remaining int // Number of requests remaining in the current window.
Limit int // The total configured limit for the window.
ResetAfter time.Duration // Time remaining until the rate limit window resets.
}
Result represents the outcome of a rate limit check.