redis

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package redis provides distributed lock implementations using Redis.

This package implements the lock.Locker and lock.RWLocker interfaces using Redis as the backend. It uses the Redlock algorithm for exclusive locks and Redis sets for read-write locks.

Features:

  • Redlock algorithm for distributed exclusive locks
  • Retry with exponential backoff and jitter
  • Circuit breaker for Redis health monitoring
  • Optional degraded mode (fallback to local locks)
  • Comprehensive error handling and logging

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoRedisAddrs            = errors.New("at least one Redis address is required")
	ErrInsufficientNodesQuorum = errors.New("insufficient Redis nodes for quorum")
	ErrCircuitBreakerOpen      = errors.New("circuit breaker open: Redis is unavailable")
	ErrWriteLockHeld           = errors.New("write lock already held")
	ErrReadersTimeout          = errors.New("timeout waiting for readers to finish")
	ErrWriteLockTimeout        = errors.New("timeout waiting for write lock to clear")
)

Errors returned by Redis lock operations.

Functions

func NewRWLocker

func NewRWLocker(
	ctx context.Context,
	cfg Config,
	retryCfg lock.RetryConfig,
	allowDegradedMode bool,
) (lock.RWLocker, error)

NewRWLocker creates a new Redis-based read-write locker.

Types

type Config

type Config struct {
	// Addrs is a list of Redis server addresses.
	// For single node: ["localhost:6379"]
	// For cluster: ["node1:6379", "node2:6379", "node3:6379"]
	Addrs []string

	// Username for authentication (optional, required for Redis ACL).
	Username string

	// Password for authentication (optional).
	Password string

	// DB is the Redis database number (0-15).
	DB int

	// UseTLS enables TLS connection.
	UseTLS bool

	// PoolSize is the maximum number of socket connections.
	PoolSize int

	// KeyPrefix for all distributed lock keys.
	KeyPrefix string
}

Config holds Redis configuration for distributed locking.

type Locker

type Locker struct {
	// contains filtered or unexported fields
}

Locker implements lock.Locker using Redis with Redlock algorithm.

func NewLocker

func NewLocker(
	ctx context.Context,
	cfg Config,
	retryCfg lock.RetryConfig,
	allowDegradedMode bool,
) (*Locker, error)

NewLocker creates a new Redis-based locker.

func (*Locker) Lock

func (l *Locker) Lock(ctx context.Context, key string, ttl time.Duration) error

Lock acquires an exclusive lock with retry and exponential backoff.

func (*Locker) TryLock

func (l *Locker) TryLock(ctx context.Context, key string, ttl time.Duration) (bool, error)

TryLock attempts to acquire an exclusive lock without retries.

func (*Locker) Unlock

func (l *Locker) Unlock(ctx context.Context, key string) error

Unlock releases an exclusive lock.

type RWLocker

type RWLocker struct {
	// contains filtered or unexported fields
}

RWLocker implements lock.RWLocker using Redis sets for readers.

func (*RWLocker) Lock

func (rw *RWLocker) Lock(ctx context.Context, key string, ttl time.Duration) error

Lock acquires an exclusive write lock with retry and exponential backoff.

func (*RWLocker) RLock

func (rw *RWLocker) RLock(ctx context.Context, key string, ttl time.Duration) error

RLock acquires a shared read lock.

func (*RWLocker) RUnlock

func (rw *RWLocker) RUnlock(ctx context.Context, key string) error

RUnlock releases a shared read lock.

func (*RWLocker) TryLock

func (rw *RWLocker) TryLock(ctx context.Context, key string, ttl time.Duration) (bool, error)

TryLock attempts to acquire an exclusive write lock without blocking.

func (*RWLocker) Unlock

func (rw *RWLocker) Unlock(ctx context.Context, key string) error

Unlock releases an exclusive write lock.

Jump to

Keyboard shortcuts

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