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 ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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
Example ¶
lock, err := New(filepath.Join(os.TempDir(), "lock.me.now.lck")) if err != nil { fmt.Printf("Cannot init lock. reason: %v", err) panic(err) // handle properly please! } // Error handling is essential, as we only try to get the lock. if err = lock.TryLock(); err != nil { fmt.Printf("Cannot lock %q, reason: %v", lock, err) panic(err) // handle properly please! } defer func() { if err := lock.Unlock(); err != nil { fmt.Printf("Cannot unlock %q, reason: %v", lock, err) panic(err) // handle properly please! } }() fmt.Println("Do stuff under lock")
Output: Do stuff under 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") }