lockr

package
v1.0.0-beta.228 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoLockAcquired    = errors.New("lock could not be acquired")
	ErrNoLockReleased    = errors.New("lock could not be released")
	ErrSessionLockerDone = errors.New("session locker is already closed")
	ErrSessionLockerBusy = errors.New("session locker is blocked by another lock request")
)
View Source
var ErrLockTimeout = errors.New("lock operation timed out")

ErrLockTimeout is returned when a lock operation times out

Functions

This section is empty.

Types

type Key

type Key interface {
	String() string
	Hash64() uint64
}

Key is a unique identifier for a resource and a scope that can be locked

func NewKey

func NewKey(scopes ...string) (Key, error)

NewKey constructs a key for the given scopes. Scope is always required and must be non-empty.

type Locker

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

Locker is the generic interface for distributed business level locks.

func NewLocker

func NewLocker(cfg *LockerConfig) (*Locker, error)

func (*Locker) LockForTX

func (l *Locker) LockForTX(ctx context.Context, key Key) error

LockForTX locks the key for the duration of the transaction.

func (*Locker) LockForTXWithScopes

func (l *Locker) LockForTXWithScopes(ctx context.Context, scopes ...string) error

type LockerConfig

type LockerConfig struct {
	Logger *slog.Logger
}

func (*LockerConfig) Validate

func (c *LockerConfig) Validate() error

type Releaser

type Releaser func(context.Context) error

type SessionLocker

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

SessionLocker is a locker that uses PostgreSQL advisory locks to acquire locks. It requires a dedicated connection to acquire locks.

func NewSessionLockr

func NewSessionLockr(config SessionLockerConfig) (*SessionLocker, error)

func (*SessionLocker) Close

func (l *SessionLocker) Close()

Close releases all locks held by the SessionLocker and closes the underlying database connection.

func (*SessionLocker) Lock

func (l *SessionLocker) Lock(ctx context.Context, key Key) (Releaser, error)

Lock blocks until a lock is acquired and returns a Releaser that can be used to release the lock if it is successfully acquired. The ErrNoLockAcquired is acquiring the lock is denied by the database server. The ErrSessionLockerDone is returned if SessionLocker is closed, meaning it cannot be used for acquiring locks.

func (*SessionLocker) LockWithScopes

func (l *SessionLocker) LockWithScopes(ctx context.Context, scopes ...string) (Releaser, error)

func (*SessionLocker) Start

func (l *SessionLocker) Start(ctx context.Context) error

func (*SessionLocker) TryLock

func (l *SessionLocker) TryLock(ctx context.Context, key Key) (Releaser, error)

TryLock attempts to acquire a lock for the given key in a non-blocking way and returns a Releaser that can be used to release the lock if it is successfully acquired. The ErrNoLockAcquired is acquiring the lock is denied by the database server. It may return ErrSessionLockerBusy if the SessionLocker is blocked by another caller, indicating that the lock request may be retried. The ErrSessionLockerDone is returned if SessionLocker is closed, meaning it cannot be used for acquiring locks.

func (*SessionLocker) TryLockWithScopes

func (l *SessionLocker) TryLockWithScopes(ctx context.Context, scopes ...string) (Releaser, error)

type SessionLockerConfig

type SessionLockerConfig struct {
	Logger         *slog.Logger
	PostgresDriver *pgdriver.Driver
}

Jump to

Keyboard shortcuts

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