redis

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2026 License: MIT Imports: 7 Imported by: 0

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.

func (*Driver) Close

func (d *Driver) Close(ctx context.Context) error

Close implements the distlimit.Driver interface. For Redis, the client lifecycle is typically managed outside of this driver, so this method is a no-op that returns nil.

type Option

type Option func(*Driver)

Option configures functional parameters for the redis Driver.

func WithPrefix

func WithPrefix(prefix string) Option

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:".

Jump to

Keyboard shortcuts

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