github.com/rotari-dev/rotari/internal/lease

Overview

package lease // import "github.com/rotari-dev/rotari/internal/lease"

Package lease holds a process's claim on a file with flock: a process holds the
exclusive lock for as long as it lives, and others probe it with a shared lock.
The kernel drops the lock however the holder ends, so a probe never mistakes a
dead holder, or a process that reused its PID, for a live one.

Variables

var ErrHeld = errors.New("the lease is held by another process")
    ErrHeld says another process holds the lease.

Functions

func Hold(path string, mode os.FileMode, wait time.Duration) (*os.File, error)
    Hold opens path, creating it with mode, and takes its exclusive lock,
    retrying for up to wait so that a probe holding the shared lock for an
    instant does not read as a holder. The lease lasts until the returned file
    is closed or the process ends. The file itself is never removed: a process
    that opened it before a removal could otherwise lock a file no one else
    sees.

func Probe(path string, whileFree func() error) (bool, error)
    Probe reports whether another process holds the lease at path. A missing
    file is a lease no one holds. When no one holds it, whileFree, if set,
    runs while Probe holds the shared lock, so no holder can start meanwhile;
    its error is returned. Any other failure is returned rather than read as
    free.

func TryLock(file *os.File) error
    TryLock takes the exclusive lock on file without waiting. It returns ErrHeld
    while another process holds it, and any other failure as it is.