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 ¶
var ErrLocked = errors.New("lockfile already held")
ErrLocked is returned by Acquire when another process holds the lock.
Functions ¶
func ReadHolderPID ¶
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 ¶
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.