lockfile

package
v0.3.37 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package lockfile handles pid file based locking. While a sync.Mutex helps against concurrency issues within a single process, this package is designed to help against concurrency issues between cooperating processes or serializing multiple invocations of the same process. You can also combine sync.Mutex with Lockfile in order to serialize an action between different goroutines in a single program and also multiple invocations of this program.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBusy          = TemporaryError("Locked by other process")             // If you get this, retry after a short sleep might help
	ErrNotExist      = TemporaryError("Lockfile created, but doesn't exist") // If you get this, retry after a short sleep might help
	ErrNeedAbsPath   = errors.New("Lockfiles must be given as absolute path names")
	ErrInvalidPid    = errors.New("Lockfile contains invalid pid for system")
	ErrDeadOwner     = errors.New("Lockfile contains pid of process not existent on this system anymore")
	ErrRogueDeletion = errors.New("Lockfile owned by me has been removed unexpectedly")
)

Various errors returned by this package

Functions

This section is empty.

Types

type Lockfile

type Lockfile string

Lockfile is a pid file which can be locked

func New

func New(path string) (Lockfile, error)

New describes a new filename located at the given absolute path.

func (Lockfile) GetOwner

func (l Lockfile) GetOwner() (*os.Process, error)

GetOwner returns who owns the lockfile.

func (Lockfile) TryLock

func (l Lockfile) TryLock() error

TryLock tries to own the lock. It Returns nil, if successful and and error describing the reason, it didn't work out. Please note, that existing lockfiles containing pids of dead processes and lockfiles containing no pid at all are simply deleted.

func (Lockfile) Unlock

func (l Lockfile) Unlock() error

Unlock a lock again, if we owned it. Returns any error that happened during release of lock.

type TemporaryError

type TemporaryError string

TemporaryError is a type of error where a retry after a random amount of sleep should help to mitigate it.

func (TemporaryError) Error

func (t TemporaryError) Error() string

func (TemporaryError) Temporary

func (t TemporaryError) Temporary() bool

Temporary returns always true. It exists, so you can detect it via

if te, ok := err.(interface{ Temporary() bool }); ok {
	fmt.Println("I am a temporary error situation, so wait and retry")
}

Jump to

Keyboard shortcuts

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