redlock

package
v0.13.2 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Locker

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

Locker represents a distributed lock using Redis. The lock is identified by a unique key and value, where the value is used to ensure that only the lock holder can release or renew the lock.

func NewLocker

func NewLocker(client redis.UniversalClient, key, value string) *Locker

NewLocker initializes a new Locker instance with a Redis client, a key, and a value. Parameters: - client: A Redis universal client to interact with Redis. - key: The unique identifier for the lock. - value: A unique value to associate with the lock (ensures lock ownership). Returns a pointer to a new Locker instance.

func (*Locker) ExtendLock

func (l *Locker) ExtendLock(ctx context.Context, extension time.Duration) error

ExtendLock extends the TTL of the lock if the calling instance is the lock holder. This method ensures that the lock is renewed only by the lock holder. Parameters: - ctx: The context for managing the extension request. - extension: The additional time to extend the lock by. Returns an error if the extension fails, either because the lock expired or the caller is not the lock holder.

func (*Locker) Lock

func (l *Locker) Lock(ctx context.Context, timeout time.Duration) error

Lock attempts to acquire the lock for the specified key with a timeout. If the lock is already held, it returns an error indicating the lock is unavailable. Parameters: - ctx: The context for managing the lock request lifecycle. - timeout: The time-to-live (TTL) for the lock. Returns an error if the lock is already held or if there is a Redis error.

func (*Locker) Unlock

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

Unlock releases the lock if the calling instance is the lock holder (based on the value). The operation is atomic, ensuring only the holder of the lock can release it. Parameters: - ctx: The context for managing the unlock request lifecycle. Returns an error if the unlock operation fails, either because the lock expired or the caller is not the lock holder.

func (*Locker) WaitLock

func (l *Locker) WaitLock(ctx context.Context, lockTimeout, waitTimeout time.Duration) error

WaitLock tries to acquire the lock within a specified waiting period. It will attempt to acquire the lock with exponential backoff if the lock is held by another process. Parameters: - ctx: The context for managing the wait request lifecycle. - lockTimeout: The TTL to set for the lock when acquired. - waitTimeout: The maximum time to wait for the lock to become available. Returns an error if the lock could not be acquired within the wait timeout.

type MultiLocker added in v0.13.0

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

MultiLocker manages multiple distributed locks with deterministic ordering. It acquires locks in lexicographic order to prevent deadlocks when multiple transactions need to lock the same set of keys in different orders.

func NewMultiLocker added in v0.13.0

func NewMultiLocker(client redis.UniversalClient, keys []string, value string) *MultiLocker

NewMultiLocker creates a new MultiLocker instance that manages locks for multiple keys. Keys are deduplicated and sorted lexicographically to ensure consistent lock ordering across all callers, preventing deadlocks. Parameters: - client: A Redis universal client to interact with Redis. - keys: The keys to lock (duplicates are removed, order is normalized). - value: A unique value to associate with all locks (ensures lock ownership). Returns a pointer to a new MultiLocker instance.

func (*MultiLocker) Keys added in v0.13.0

func (m *MultiLocker) Keys() []string

Keys returns the deduplicated and sorted keys that this MultiLocker manages.

func (*MultiLocker) Lock added in v0.13.0

func (m *MultiLocker) Lock(ctx context.Context, timeout time.Duration) error

Lock attempts to acquire all locks in deterministic order. If any lock acquisition fails, all previously acquired locks are released (rollback). Parameters: - ctx: The context for managing the lock request lifecycle. - timeout: The time-to-live (TTL) for each lock. Returns an error if any lock could not be acquired.

func (*MultiLocker) Unlock added in v0.13.0

func (m *MultiLocker) Unlock(ctx context.Context) error

Unlock releases all locks in reverse order of acquisition. Releasing in reverse order is a best practice for multi-lock scenarios. Parameters: - ctx: The context for managing the unlock request lifecycle. Returns an error if any unlock operation fails.

func (*MultiLocker) WaitLock added in v0.13.0

func (m *MultiLocker) WaitLock(ctx context.Context, lockTimeout, waitTimeout time.Duration) error

WaitLock tries to acquire all locks within a specified waiting period. It will attempt to acquire locks with exponential backoff if any lock is held. If lock acquisition fails after the wait timeout, all acquired locks are released. Parameters: - ctx: The context for managing the wait request lifecycle. - lockTimeout: The TTL to set for each lock when acquired. - waitTimeout: The maximum time to wait for all locks to become available. Returns an error if all locks could not be acquired within the wait timeout.

Jump to

Keyboard shortcuts

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