Documentation
¶
Overview ¶
Package sys provides low-level operating system helpers including file locks and Unix domain socket listeners.
Index ¶
- Variables
- func AcquireLock(lockPath string) (*os.File, error)
- func AcquireLockWithRetry(ctx context.Context, lockPath string, timeout, interval time.Duration) (*os.File, error)
- func ListenUnix(ctx context.Context, socketPath string) (net.Listener, error)
- func LockHolderPID(lockPath string) (int, bool)
- func ReleaseLock(f *os.File) error
- func WaitForLockRelease(ctx context.Context, lockPath string, timeout, interval time.Duration) error
Constants ¶
This section is empty.
Variables ¶
var ErrLocked = errors.New("sys: lock file is already held")
ErrLocked is returned when a requested lock file is already held by another process.
Functions ¶
func AcquireLock ¶
AcquireLock opens the file at lockPath (creating its parent directories if necessary) and acquires an exclusive, non-blocking lock. It records the current process id in the lock file so a second process can report who holds it. It returns ErrLocked if the lock is already held.
func AcquireLockWithRetry ¶
func AcquireLockWithRetry(ctx context.Context, lockPath string, timeout, interval time.Duration) (*os.File, error)
AcquireLockWithRetry behaves like AcquireLock but keeps retrying every interval until the lock is acquired, timeout elapses, or ctx is canceled. On timeout it returns ErrLocked. It gives a daemon that is still tearing down (and holding the lock past its socket shutdown) a short window to finish before a replacement gives up.
func ListenUnix ¶
ListenUnix creates a Unix domain socket listener at socketPath with 0600 permissions, removing any stale socket file first.
func LockHolderPID ¶
LockHolderPID reads the process id recorded in the lock file at lockPath. It returns false when the file is missing, unreadable, or does not hold a valid pid.
func ReleaseLock ¶
ReleaseLock unlocks and closes f. It is a no-op if f is nil.
func WaitForLockRelease ¶
func WaitForLockRelease(ctx context.Context, lockPath string, timeout, interval time.Duration) error
WaitForLockRelease blocks until the lock at lockPath is free, timeout elapses, or ctx is canceled. Probing never writes to the lock file, so a waiter cannot clobber the pid recorded by the current holder. It returns ErrLocked when the timeout elapses with the lock still held, and passes other lock failures through unchanged.
Types ¶
This section is empty.