redis

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package redis implements a distributed rate limiting driver using Redis as the backend. It relies on atomic Redis Lua scripts provided by pluggable algorithm strategies to enforce rate limits consistently across distributed microservices.

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 using Redis as the storage backend. All rate limit operations execute atomically in Redis via algorithm-specific Lua scripts.

Driver is thread-safe and 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, alg algorithm.Algorithm) (algorithm.Result, error)

Allow evaluates the rate limit for a key using the Lua script provided by the algorithm strategy. It sends evaluation parameters (limit, window, current timestamp) to Redis and parses the response.

func (*Driver) Close

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

Close closes the underlying Redis client connection pool.

func (*Driver) Reset added in v1.1.1

func (d *Driver) Reset(ctx context.Context, key string) error

Reset deletes the rate limit key (and its sequence key if applicable) from Redis.

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 across environments or services. The default prefix is "distlimit".

Jump to

Keyboard shortcuts

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