Documentation ¶
Overview ¶
Package filelock handles 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 Lock in order to serialize an action between different goroutines in a single program and also multiple invocations of this program.
Index ¶
Constants ¶
const ( F_OFD_GETLK = 37 F_OFD_SETLK = 37 F_OFD_SETLKW = 38 )
This used to call syscall.Flock() but that call fails with EBADF on NFS. An alternative is lockf() which works on NFS but that call lets a process lock the same file twice. Instead, use Linux's non-standard open file descriptor locks which will block if the process already holds the file lock.
constants from /usr/include/bits/fcntl-linux.h
Variables ¶
var ( ErrNeedAbsPath = errors.New("absolute file path must be provided") // ErrLocked is returned if the backing file is already locked by some other // process. ErrLocked = errors.New("file already locked") )
Various errors returned by this package
Functions ¶
This section is empty.
Types ¶
type TryLocker ¶
type TryLocker interface { sync.Locker // TryLock attempts to grab the lock, but does not hang if the lock is // actively held by another process. Instead, it returns false. TryLock() bool }
TryLocker is a sync.Locker augmented with TryLock.
type TryLockerSafe ¶
type TryLockerSafe interface { fmt.Stringer // TryLock attempts to grab the lock, but does not hang if the lock is // actively held by another process. Instead, it returns false. TryLock() (bool, error) // Lock blocks until it's able to grab the lock. Lock() error // Unlock releases the lock. Should only be called when the lock is // held. Unlock() error // Must returns a TryLocker whose Lock, TryLock and Unlock methods // panic rather than return errors. Must() TryLocker // Destroy cleans up any possible resources. Destroy() error }
TryLockerSafe is like TryLocker, but the methods can return an error and never panic.