filelock

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrHeld indicates the lock is currently held by an exclusive holder.
	ErrHeld = errors.New("lock held")
	// ErrBusy indicates the lock could not be acquired because only shared askers were present.
	// It is distinct from ErrHeld and does not wrap it.
	ErrBusy = errors.New("lock busy")
	// ErrTimeout indicates waiting for the lock timed out.
	ErrTimeout = errors.New("lock timeout")
	// ErrNotSupported indicates that filelock is not supported on the current platform.
	ErrNotSupported = errors.New("filelock: locking is not supported on this platform")
)

Functions

func Probe

func Probe(path string) (State, Stamp, error)

Probe inspects path without taking an exclusive lock and without creating the file if absent.

Types

type FileLock

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

FileLock represents an acquired, open file lock.

func Lock

func Lock(path string, label string, timeout time.Duration) (*FileLock, error)

Lock acquires the exclusive file lock on path, waiting up to timeout with jittered backoff.

The lockfile path must be dedicated to filelock: taking an exclusive lock on an existing regular file truncates it when writing the holder stamp. The file is created if absent and is never removed.

func TryLock

func TryLock(path string, label string) (*FileLock, error)

TryLock attempts to acquire the exclusive file lock on path without waiting.

The lockfile path must be dedicated to filelock: taking an exclusive lock on an existing regular file truncates it when writing the holder stamp. The file is created if absent and is never removed.

func (*FileLock) Path

func (l *FileLock) Path() string

Path returns the path of the locked file.

func (*FileLock) Previous

func (l *FileLock) Previous() *Stamp

Previous returns the stamp of the previous unreleased holder (e.g. killed/crashed), or nil.

func (*FileLock) Stamp

func (l *FileLock) Stamp() Stamp

Stamp returns the stamp written by this holder when taking the lock.

func (*FileLock) String

func (l *FileLock) String() string

String returns a description of the held lock.

func (*FileLock) Unlock

func (l *FileLock) Unlock() error

Unlock releases the file lock by truncating the file to zero bytes, syncing, releasing the OS lock, and closing the descriptor. The file is NEVER deleted. Calling Unlock multiple times is safe and returns nil on subsequent calls.

type HeldError

type HeldError struct {
	Path     string
	Holder   Stamp
	Wait     time.Duration
	TimedOut bool
}

HeldError describes a refusal because another process holds the lock, or a timeout occurred while waiting for the lock.

func AsHeldError

func AsHeldError(err error) (*HeldError, bool)

AsHeldError reports whether err is a *HeldError.

func (*HeldError) Error

func (e *HeldError) Error() string

func (*HeldError) Unwrap

func (e *HeldError) Unwrap() []error

Unwrap returns []error so that errors.Is(err, ErrHeld) and errors.Is(err, ErrTimeout) (if TimedOut) both match.

type Stamp

type Stamp struct {
	PID     int
	Host    string
	Started time.Time
	Label   string
}

Stamp records diagnostic details of who holds the lock: PID, host, start time, and label.

func ParseStamp

func ParseStamp(s string) (Stamp, error)

ParseStamp parses a Stamp from raw text.

func ReadStamp

func ReadStamp(path string) (Stamp, error)

ReadStamp reads and parses the Stamp from path without acquiring the lock. It opens with O_RDONLY and does not block on FIFOs.

func (Stamp) Format

func (s Stamp) Format() string

Format returns the canonical multi-line representation of the stamp.

func (Stamp) IsZero

func (s Stamp) IsZero() bool

IsZero reports whether the stamp is empty.

func (Stamp) String

func (s Stamp) String() string

String returns a single-line summary of the stamp.

type State

type State string

State is the observation of a lock file path.

const (
	// StateAbsent means the lock file does not exist.
	StateAbsent State = "absent"
	// StateFree means the lock file exists and is not held by any process.
	StateFree State = "free"
	// StateHeld means the lock file exists and is held by an exclusive holder.
	StateHeld State = "held"
)

func (State) String

func (s State) String() string

Jump to

Keyboard shortcuts

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