dlock

package
v1.5.4 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultTTL        = 10 * time.Second
	DefaultRetryDelay = 50 * time.Millisecond
)

Variables

View Source
var (
	ErrLockFailed   = errors.New("failed to acquire lock")
	ErrInvalidToken = errors.New("invalid token or lock expired")
)

Functions

func DoWithLock added in v1.5.4

func DoWithLock(ctx context.Context, locker Locker, key string, fn func() error, opts ...Option) error

DoWithLock acquires the lock, runs the function fn, and safely releases the lock. It returns an error if the lock cannot be acquired, or if fn returns an error.

func TryDoWithLock added in v1.5.4

func TryDoWithLock(ctx context.Context, locker Locker, key string, fn func() error, opts ...Option) (bool, error)

TryDoWithLock attempts to acquire the lock non-blocking, runs the function fn, and safely releases the lock. It returns a boolean indicating if the lock was acquired, and an error if fn returns an error or backend fails.

Types

type Lock

type Lock interface {
	// Unlock releases the lock. It must be idempotent.
	Unlock(ctx context.Context) error

	// Valid checks if the lock is still valid (non-blocking).
	Valid() bool

	// Done returns a channel that's closed when the lock is lost or unlocked.
	Done() <-chan struct{}
}

Lock represents an acquired distributed lock.

type Locker

type Locker interface {
	// Lock acquires the lock, blocking until it succeeds or ctx is canceled.
	Lock(ctx context.Context, key string, opts ...Option) (Lock, error)

	// TryLock attempts to acquire the lock without blocking.
	TryLock(ctx context.Context, key string, opts ...Option) (Lock, bool, error)
}

Locker is the unified interface for distributed locks.

type Option

type Option func(*Options)

func WithRetryDelay

func WithRetryDelay(d time.Duration) Option

func WithTTL

func WithTTL(d time.Duration) Option

func WithToken

func WithToken(token string) Option

type Options

type Options struct {
	TTL        time.Duration
	RetryDelay time.Duration
	Token      string
}

func NewOptions

func NewOptions(opts ...Option) *Options

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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