Documentation ¶
Overview ¶
Package lock implements simple locking primitives on a regular file or directory using flock
Index ¶
- Variables
- func CleanKeyLocks(lockDir string) error
- type FileLock
- func ExclusiveLock(path string, lockType LockType) (*FileLock, error)
- func NewLock(path string, lockType LockType) (*FileLock, error)
- func SharedLock(path string, lockType LockType) (*FileLock, error)
- func TryExclusiveLock(path string, lockType LockType) (*FileLock, error)
- func TrySharedLock(path string, lockType LockType) (*FileLock, error)
- type KeyLock
- func ExclusiveKeyLock(lockDir string, key string) (*KeyLock, error)
- func NewKeyLock(lockDir string, key string) (*KeyLock, error)
- func SharedKeyLock(lockDir string, key string) (*KeyLock, error)
- func TryExclusiveKeyLock(lockDir string, key string) (*KeyLock, error)
- func TrySharedKeyLock(lockDir string, key string) (*KeyLock, error)
- type LockType
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func CleanKeyLocks ¶ added in v0.5.2
CleanKeyLocks remove lock files from the lockDir. For every key it tries to take an Exclusive lock on it and skip it if it fails with ErrLocked
Types ¶
type FileLock ¶ added in v0.5.2
type FileLock struct {
// contains filtered or unexported fields
}
FileLock represents a lock on a regular file or a directory
func ExclusiveLock ¶
ExclusiveLock takes an exclusive lock on a file/directory. It will block if an exclusive lock is already held on the file/directory.
func SharedLock ¶
SharedLock takes a co-operative (shared) lock on a file/directory. It will block if an exclusive lock is already held on the file/directory.
func TryExclusiveLock ¶
TryExclusiveLock takes an exclusive lock on a file/directory without blocking. It will return ErrLocked if any lock is already held on the file/directory.
func TrySharedLock ¶
TrySharedLock takes a co-operative (shared) lock on a file/directory without blocking. It will return ErrLocked if an exclusive lock already exists on the file/directory.
func (*FileLock) ExclusiveLock ¶ added in v0.5.2
ExclusiveLock takes an exclusive lock. This is idempotent when the Lock already represents an exclusive lock, and promotes a shared lock to exclusive atomically. It will block if an exclusive lock is already held.
func (*FileLock) Fd ¶ added in v0.5.2
Fd returns the lock's file descriptor, or an error if the lock is closed
func (*FileLock) SharedLock ¶ added in v0.5.2
SharedLock takes a co-operative (shared) lock on. This is idempotent when the Lock already represents a shared lock, and demotes an exclusive lock to shared atomically. It will block if an exclusive lock is already held.
func (*FileLock) TryExclusiveLock ¶ added in v0.5.2
TryExclusiveLock takes an exclusive lock without blocking. This is idempotent when the Lock already represents an exclusive lock, and tries promote a shared lock to exclusive atomically. It will return ErrLocked if any lock is already held.
func (*FileLock) TrySharedLock ¶ added in v0.5.2
TrySharedLock takes a co-operative (shared) lock without blocking. This is idempotent when the Lock already represents a shared lock, and tries demote an exclusive lock to shared atomically. It will return ErrLocked if an exclusive lock already exists.
type KeyLock ¶ added in v0.5.2
type KeyLock struct {
// contains filtered or unexported fields
}
KeyLock is a lock for a specific key. The lock file is created inside a directory using the key name. This is useful when multiple processes want to take a lock but cannot use FileLock as they don't have a well defined file on the filesystem. key value must be a valid file name (as the lock file is named after the key value).
func ExclusiveKeyLock ¶ added in v0.5.2
ExclusiveLock takes an exclusive lock on a key. lockDir is the directory where the lock file will be created. It will block if an exclusive lock is already held on the key.
func NewKeyLock ¶ added in v0.5.2
NewKeyLock returns a KeyLock for the specified key without acquisition. lockdir is the directory where the lock file will be created. If lockdir doesn't exists it will be created. key value must be a valid file name (as the lock file is named after the key value).
func SharedKeyLock ¶ added in v0.5.2
SharedLock takes a co-operative (shared) lock on a key. lockDir is the directory where the lock file will be created. It will block if an exclusive lock is already held on the key.
func TryExclusiveKeyLock ¶ added in v0.5.2
TryExclusiveLock takes an exclusive lock on the key without blocking. lockDir is the directory where the lock file will be created. It will return ErrLocked if any lock is already held.
func TrySharedKeyLock ¶ added in v0.5.2
TrySharedLock takes a co-operative (shared) lock on a key without blocking. lockDir is the directory where the lock file will be created. It will return ErrLocked if an exclusive lock already exists on the key.
func (*KeyLock) Close ¶ added in v0.5.2
func (l *KeyLock) Close()
Close closes the key lock which implicitly unlocks it as well
func (*KeyLock) ExclusiveKeyLock ¶ added in v0.5.2
ExclusiveLock takes an exclusive lock on a key. This is idempotent when the KeyLock already represents an exclusive lock, and promotes a shared lock to exclusive atomically. It will block if an exclusive lock is already held on the key.
func (*KeyLock) SharedKeyLock ¶ added in v0.5.2
SharedLock takes a co-operative (shared) lock on a key. This is idempotent when the KeyLock already represents a shared lock, and demotes an exclusive lock to shared atomically. It will block if an exclusive lock is already held on the key.
func (*KeyLock) TryExclusiveKeyLock ¶ added in v0.5.2
TryExclusiveLock takes an exclusive lock on a key without blocking. This is idempotent when the KeyLock already represents an exclusive lock, and tries promote a shared lock to exclusive atomically. It will return ErrLocked if any lock is already held on the key.
func (*KeyLock) TrySharedKeyLock ¶ added in v0.5.2
TrySharedLock takes a co-operative (shared) lock on the key without blocking. This is idempotent when the KeyLock already represents a shared lock, and tries demote an exclusive lock to shared atomically. It will return ErrLocked if an exclusive lock already exists on the key.