Documentation
¶
Overview ¶
Package lockfile provides utilities for managing file locks and cleanup.
Index ¶
- func AcquireTrackedLock(ctx context.Context, lockPath string, retryDelay time.Duration) (*flock.Flock, bool, error)
- func CleanupAllLocks()
- func CleanupStaleLocks(directories []string, maxAge time.Duration)
- func NewTrackedLock(lockPath string) *flock.Flock
- func ReleaseTrackedLock(lockPath string, lock *flock.Flock)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AcquireTrackedLock ¶ added in v0.51.2
func AcquireTrackedLock(ctx context.Context, lockPath string, retryDelay time.Duration) (*flock.Flock, bool, error)
AcquireTrackedLock acquires an exclusive lock on lockPath and registers it for cleanup, the way NewTrackedLock+TryLockContext do, but additionally guards against acquiring a lock on a stale, already-orphaned inode.
Because flock(2) locks an open file description on an inode rather than a path, and acquisition (open, then flock) and release (unlink, then unlock — see ReleaseTrackedLock) are separate steps, a caller can open lockPath and still be blocked in flock() at the moment another holder unlinks and releases: that other holder's Remove can land between this caller's open() and its successful flock(), or a concurrent third party can recreate lockPath under a new inode while this caller's flock succeeds on the old, now-unreachable one. Either way this caller would believe it holds "the" lock for lockPath while actually holding a lock nothing else will ever contend for again. After acquiring, this function checks that the locked file's inode is still the one lockPath resolves to (os.SameFile); on mismatch it discards the stale lock and retries with a fresh open, bounded by ctx.
func CleanupAllLocks ¶
func CleanupAllLocks()
CleanupAllLocks provides global cleanup of all registered lock files
func CleanupStaleLocks ¶
CleanupStaleLocks removes stale lock files from the specified directories A lock file is considered stale if it's older than the maxAge duration
func NewTrackedLock ¶
NewTrackedLock creates a new file lock and registers it for cleanup
func ReleaseTrackedLock ¶
ReleaseTrackedLock unlocks, removes, and unregisters a lock file.
The path is unlinked before the flock is released. flock(2) locks attach to an inode, not a path, so releasing in the other order leaves a window where a waiter can flock this soon-to-be-removed inode and then have the path pulled out from under it by this call's Remove, while a third locker creates a fresh inode at the same path — two lockers now believe they hold "the" lock. Unlinking first means anyone who opens lockPath from here on necessarily gets a new inode, which is what AcquireTrackedLock's identity check depends on to notice and retry past a stale lock.
Types ¶
This section is empty.