Documentation
¶
Overview ¶
Package flock is a lightweight, standard-library-only replacement for github.com/gofrs/flock tailored to the needs of dque.
It provides exclusive, non-blocking advisory file locking through a small Flock type. The supported API is intentionally minimal:
- New(path) creates a new Flock for the given path.
- TryLock() attempts to acquire the lock without blocking.
- Unlock() releases the lock.
- Close() releases the lock and closes the underlying file handle.
The following features from gofrs/flock are intentionally not implemented: shared locks (RLock/TryRLock), blocking locks (Lock), timeouts, and retry logic. Only Unix-like systems and Windows are supported.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Flock ¶
type Flock struct {
// contains filtered or unexported fields
}
Flock is a file-backed advisory lock.
func New ¶
New returns a new Flock for the given path. The file is not opened until TryLock is called.
func (*Flock) Close ¶
Close releases the lock if held and closes the underlying file. Any errors from unlocking and closing are joined so the file descriptor is always released even if the unlock step fails.
func (*Flock) TryLock ¶
TryLock attempts to acquire an exclusive, non-blocking advisory lock. It returns (true, nil) on success, (false, nil) if the file is already locked by another process, and (false, err) for other errors.
If this Flock already holds the lock, TryLock short-circuits and returns (true, nil). If the lock cannot be acquired, the underlying file handle is left open so subsequent calls can retry efficiently. Callers must use Unlock or Close to release the lock and close the file descriptor.
On contention, TryLock retries a few times with short delays to tolerate filesystem-level lag (e.g., ZFS) where the kernel may not immediately release a flock after the owning process dies.