Documentation
¶
Overview ¶
Package lock implements a cross-process file lock using an exclusive PID file, ported from FileLock in src/utils.ts of github.com/colbymchenry/codegraph (MIT).
Protocol: the lock file is created with O_CREATE|O_EXCL so only one process can win the race. On conflict the holder's PID and the file mtime are read: the lock is considered STALE (deleted and retaken) if the mtime is older than 2 minutes OR the PID is no longer alive (signal 0 probe). Otherwise ErrLockUnavailable is returned.
Release verifies the stored PID matches our own before unlinking, so a late-running stale-cleanup by another process cannot accidentally release a lock we legitimately hold.
All external dependencies (clock, own PID, alive-probe) are injectable for deterministic unit tests.
Index ¶
Constants ¶
const StaleTimeout = 2 * time.Minute
StaleTimeout is how old a lock file's mtime must be before it is considered stale regardless of PID liveness, matching the original's STALE_TIMEOUT_MS = 2 minutes.
Variables ¶
ErrLockUnavailable is returned when the lock is held by another live process that has not timed out. Callers (e.g. the file watcher) can use errors.Is to distinguish this from unexpected I/O errors.
Functions ¶
This section is empty.
Types ¶
type AliveFunc ¶
AliveFunc probes whether a process is alive. The production value sends signal 0 (os.FindProcess + p.Signal(syscall.Signal(0))).
type FileLock ¶
type FileLock struct {
// contains filtered or unexported fields
}
FileLock is a cross-process exclusive lock backed by a PID file. The zero value is not usable; construct with New.
func (*FileLock) Acquire ¶
Acquire attempts to take the lock. It returns ErrLockUnavailable (wrapped) when another live, non-timed-out process holds it, and other errors for unexpected I/O failures.
type NowFunc ¶
NowFunc returns the current wall-clock time. Injectable so tests can control time without sleeping.
type Option ¶
type Option func(*FileLock)
Option configures a FileLock.
func WithAliveProbe ¶
WithAliveProbe overrides the alive-probe function. Defaults to a signal-0 probe via os.FindProcess.