lockfile

package
v6.0.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 14, 2019 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package lockfile coordinates process-based file locking.

This package is designed to aid concurrency issues between different processes.

Sample usage:

lf := lockfile.New("/var/apt/apt.lock")
// Blocks and waits if /var/apt/apt.lock already exists.
if err := lf.Lock(); err != nil {
	log.Fatal(err)
}

defer lf.MustUnlock()

// Do something in /var/apt/??

Two concurrent invocations of this program will compete to create /var/apt/apt.lock, and then make the other wait until /var/apt/apt.lock disappears.

If the lock holding process disappears without removing the lock, another process using this library will detect that and remove the lock.

If some other entity removes the lockfile erroneously, the lock holder's call to Unlock() will return an ErrRogueDeletion.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrRogueDeletion means the lock file was removed by someone
	// other than the lock holder.
	ErrRogueDeletion = errors.New("cannot unlock lockfile owned by another process")

	// ErrBusy means the lock is being held by another living process.
	ErrBusy = errors.New("file is locked by another process")

	// ErrInvalidPID means the lock file contains an incompatible syntax.
	ErrInvalidPID = errors.New("lockfile points to file with invalid content")

	// ErrProcessDead means the lock is held by a process that does not exist.
	ErrProcessDead = errors.New("lockfile points to invalid PID")
)

Functions

This section is empty.

Types

type Lockfile

type Lockfile struct {
	// contains filtered or unexported fields
}

Lockfile is a process-based file lock.

func New

func New(path string) *Lockfile

New returns a new lock file at the given path.

func (*Lockfile) Lock

func (l *Lockfile) Lock() error

Lock blocks until it can create a valid lock file.

If a valid lock file already exists, it waits for the file to be deleted or until the associated process dies.

If an invalid lock file exists, it will be deleted and we'll retry locking.

func (*Lockfile) MustUnlock

func (l *Lockfile) MustUnlock()

MustUnlock panics if the call to Unlock fails.

func (*Lockfile) TryLock

func (l *Lockfile) TryLock() error

TryLock attempts to create the lock file, or returns ErrBusy if a valid lock file already exists.

If the lock file is detected not to be valid, it is removed and replaced with our lock file.

func (*Lockfile) Unlock

func (l *Lockfile) Unlock() error

Unlock attempts to delete the lock file.

If we are not the lock holder, and the lock holder is an existing process, ErrRogueDeletion will be returned.

If we are not the lock holder, and there is no valid lock holder (process died, invalid lock file syntax), ErrRogueDeletion will be returned.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL