locking

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const LockOwnerKey contextKey = "lock_owner"

Variables

View Source
var (
	ErrLockAcquisitionTimeout = errors.New("lock acquisition timed out")
	ErrLockNotHeld            = errors.New("lock not held")
)

Functions

This section is empty.

Types

type InMemLocker

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

InMemLocker is a simple thread-safe in-memory locker.

func NewInMemLocker

func NewInMemLocker() *InMemLocker

NewInMemLocker creates a new InMemLocker.

func (*InMemLocker) Acquire

func (l *InMemLocker) Acquire(ctx context.Context, key string, ttl time.Duration, timeout time.Duration) (bool, error)

func (*InMemLocker) Close

func (l *InMemLocker) Close() error

func (*InMemLocker) Release

func (l *InMemLocker) Release(ctx context.Context, key string) error

type Locker

type Locker interface {
	// Acquire attempts to acquire a lock for the given key.
	// ttl is the maximum time the lock can be held before auto-expiring.
	// timeout is the maximum time to wait for the lock to become available.
	Acquire(ctx context.Context, key string, ttl time.Duration, timeout time.Duration) (bool, error)

	// Release releases the lock for the given key.
	Release(ctx context.Context, key string) error

	// Close shuts down the locker.
	Close() error
}

Locker defines the interface for distributed locking.

type NATSLocker

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

NATSLocker implements Locker using NATS JetStream KeyValue.

func NewNATSLocker

func NewNATSLocker(ctx context.Context, nc *nats.Conn, bucketName string) (*NATSLocker, error)

NewNATSLocker creates a new NATSLocker.

func (*NATSLocker) Acquire

func (l *NATSLocker) Acquire(ctx context.Context, key string, ttl time.Duration, timeout time.Duration) (bool, error)

func (*NATSLocker) Close

func (l *NATSLocker) Close() error

func (*NATSLocker) Release

func (l *NATSLocker) Release(ctx context.Context, key string) error

type RedisLocker

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

RedisLocker implements Locker using Redis.

func NewRedisLocker

func NewRedisLocker(client *redis.Client) *RedisLocker

NewRedisLocker creates a new RedisLocker.

func (*RedisLocker) Acquire

func (l *RedisLocker) Acquire(ctx context.Context, key string, ttl time.Duration, timeout time.Duration) (bool, error)

func (*RedisLocker) Close

func (l *RedisLocker) Close() error

func (*RedisLocker) Release

func (l *RedisLocker) Release(ctx context.Context, key string) error

Jump to

Keyboard shortcuts

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