Documentation
¶
Overview ¶
Package redis implements a distributed rate limiting driver using Redis as the backend. It relies on Redis sorted sets and Lua scripting to implement a precise, sliding-window rate limiter across distributed services.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Driver ¶
type Driver struct {
// contains filtered or unexported fields
}
Driver implements the distlimit.Driver interface. It communicates with a Redis backend using a UniversalClient (supporting standalone, cluster, or sentinel Redis deployments). All rate limit operations are executed atomically in Redis via a sliding-window Lua script.
Driver is safe for concurrent use by multiple goroutines.
func New ¶
func New(client redis.UniversalClient, opts ...Option) *Driver
New creates and initializes a new Redis-backed rate limiting Driver.
func (*Driver) Allow ¶
func (d *Driver) Allow(ctx context.Context, key string, limit int64, window time.Duration) (distlimit.Result, error)
Allow evaluates the rate limit for a key using a sliding window algorithm. It executes a Redis Lua script to clean up expired timestamps, check the request limit against the current window volume, add the current request timestamp if allowed, and return the result.
type Option ¶
type Option func(*Driver)
Option configures functional parameters for the redis Driver.
func WithPrefix ¶
WithPrefix sets a custom key prefix for all rate limit keys stored in Redis. This is useful for namespacing and avoiding key collisions. The default prefix is "distlimit:".