lock

package
v0.1.11 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2026 License: Apache-2.0 Imports: 9 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 implements distributed locking using Redis SET NX EX + Lua scripts.

func NewLocker

func NewLocker(cfg LockerConfig) *Locker

NewLocker creates a new Redis-based distributed locker.

func (*Locker) Lock

func (l *Locker) Lock(ctx context.Context, key, owner string) (*RedisLock, error)

Lock acquires a distributed lock for the given key. Returns a RedisLock that must be Unlock'd when done.

type LockerConfig

type LockerConfig struct {
	RDB                rueidis.Client
	Prefix             string
	LockTTL            time.Duration // Default: 30s
	AutoExtendInterval time.Duration // Default: 0 (no auto-extend)
	Logger             gochainedlog.Logger
}

LockerConfig holds configuration for the locker.

type RedisLock

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

RedisLock represents a held distributed lock.

func (*RedisLock) SetOnLost

func (rl *RedisLock) SetOnLost(fn func())

SetOnLost registers a callback that fires (exactly once) when autoExtend discovers the lock is no longer held. Typical use: cancel the handler context.

func (*RedisLock) Unlock

func (rl *RedisLock) Unlock(_ context.Context) error

Unlock releases the distributed lock, but only if we still own it. Safe to call multiple times; subsequent calls are no-ops.

Jump to

Keyboard shortcuts

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