lockfile

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: May 18, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package lockfile provides cross-process file locks. The OS releases the lock on process exit, so there's no stale-lock recovery problem.

Index

Constants

This section is empty.

Variables

View Source
var ErrLocked = errors.New("lockfile already held")

ErrLocked is returned by Acquire when another process holds the lock.

Functions

func ReadHolderPID

func ReadHolderPID(path string) int

ReadHolderPID returns the PID written into the lock file, or 0 if the file is empty/unreadable/contains garbage. Best-effort; for diagnostic messages only.

func WithTimeout

func WithTimeout(ctx context.Context, path string, timeout time.Duration, fn func() error) (err error)

WithTimeout acquires path, runs fn while holding the lock, and releases it. If another process holds the lock, it retries until ctx is canceled or timeout elapses. The returned lock error wraps ErrLocked on timeout.

Types

type Lock

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

Lock represents an acquired exclusive lock. The OS releases on process exit if Release is not called.

func Acquire

func Acquire(path string) (*Lock, error)

Acquire takes an exclusive OS-level lock on path. Returns ErrLocked if another process holds the lock; other errors indicate I/O or permission failures. The PID written into the file is advisory diagnostic only — see ReadHolderPID. FD_CLOEXEC is set so subprocesses don't inherit the lock FD.

func (*Lock) Release

func (l *Lock) Release() error

Release releases the OS lock and closes the file. Idempotent.

Jump to

Keyboard shortcuts

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