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.
Click to show internal directories.
Click to hide internal directories.