lock

package
v1.2.9 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package lock provides an interface for file-based locking.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrLockExists           = errors.New("lock already exists")
	ErrLockStale            = errors.New("stale lock detected")
	ErrLockLost             = errors.New("lock lost")
	ErrLockAcquire          = errors.New("failed to acquire lock")
	ErrLockContextCancelled = errors.New("context was cancelled while obtaining the lock")
	ErrLockCorrup           = errors.New("corrupt lock file")
	ErrMaxRetryExceeded     = errors.New("failed to acquire lock, max retries exceeded")
	DefaultStaleTimeout     = time.Millisecond * 500
	DefaultRefreshInterval  = time.Millisecond * 200
	DefaultRetryInterval    = 1 * time.Second
	DefaultMaxRetry         = 5
)

Functions

func LockDir

func LockDir(ctx context.Context, dirPath string, fn func() error, opts ...FileLockOption) error

LockDir acquires a lock for the specified directory, executes the function, then releases the lock. The lock file is created within the target directory as `.dir.lock`.

func LockFile

func LockFile(ctx context.Context, filePath string, fn func() error, opts ...FileLockOption) error

LockFile acquires a lock for the specified file, executes the function, then releases the lock. The lock file is created in the same directory as `${filepath}.lock`

Types

type FileLock

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

func NewFileLock

func NewFileLock(ctx context.Context, filepath string, opts ...FileLockOption) *FileLock

func (*FileLock) Acquire

func (fl *FileLock) Acquire() error

func (*FileLock) Release

func (fl *FileLock) Release() error

type FileLockOption

type FileLockOption func(fl *FileLock)

func WithMaxRetry

func WithMaxRetry(retry int) FileLockOption

func WithNoMaxRetry

func WithNoMaxRetry() FileLockOption

func WithRefreshInterval

func WithRefreshInterval(interval time.Duration) FileLockOption

func WithRetryInterval

func WithRetryInterval(interval time.Duration) FileLockOption

func WithStaleTimeout

func WithStaleTimeout(timeout time.Duration) FileLockOption

Jump to

Keyboard shortcuts

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