lock

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrLockAcquireFailed    = errors.New("failed to acquire lock")                       // lock acquisition failed.
	ErrLockReleaseUnlocked  = errors.New("cannot release an unlocked lock")              // release a lock that doesn't exist.
	ErrLockReleaseForbidden = errors.New("cannot release a lock that's no longer owned") // release a lock owned by someone else.
)

Sentinel errors for lock operations.

Functions

func ReleaseLock

func ReleaseLock(ctx context.Context, redisClient *redis.Client, lock *LockData)

ReleaseLock releases a distributed lock using a Lua script for atomic operation. The Lua script ensures that only the lock owner (matching token) can release it.

func ReleaseMultipleLock

func ReleaseMultipleLock(ctx context.Context, redisClient *redis.Client, locks []*LockData) error

ReleaseMultipleLock releases multiple locks using a pipeline.

Types

type LockData

type LockData struct {
	Key      string
	Token    string
	Acquired bool
	Released bool
	Error    error
}

LockData represents a distributed lock.

func AcquireLock

func AcquireLock(
	ctx context.Context,
	redisClient *redis.Client,
	key string,
	timeout time.Duration,
	interval time.Duration,
	wait bool,
	waitTimeout time.Duration,
) *LockData

AcquireLock attempts to acquire a distributed lock. It uses SETNX (SET if Not eXists) with expiration to create the lock. If wait is true, it will retry at the specified interval until waitTimeout.

func AcquireMultipleLock

func AcquireMultipleLock(
	ctx context.Context,
	redisClient *redis.Client,
	keys []string,
	timeout time.Duration,
	interval time.Duration,
	wait bool,
	waitTimeout time.Duration,
) ([]*LockData, error)

AcquireMultipleLock attempts to acquire multiple locks atomically. It uses a pipeline to batch SETNX operations.

Jump to

Keyboard shortcuts

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