lock

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2016 License: Apache-2.0, MIT Imports: 6 Imported by: 0

Documentation

Overview

Package lock implements simple locking primitives on a regular file or directory using flock

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrLocked     = errors.New("file already locked")
	ErrNotExist   = errors.New("file does not exist")
	ErrPermission = errors.New("permission denied")
	ErrNotRegular = errors.New("not a regular file")
)

Functions

func CleanKeyLocks added in v0.5.2

func CleanKeyLocks(lockDir string) error

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

func ExclusiveLock(path string, lockType LockType) (*FileLock, error)

ExclusiveLock takes an exclusive lock on a file/directory. It will block if an exclusive lock is already held on the file/directory.

func NewLock

func NewLock(path string, lockType LockType) (*FileLock, error)

NewLock opens a new lock on a file without acquisition

func SharedLock

func SharedLock(path string, lockType LockType) (*FileLock, error)

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

func TryExclusiveLock(path string, lockType LockType) (*FileLock, error)

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

func TrySharedLock(path string, lockType LockType) (*FileLock, error)

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) Close added in v0.5.2

func (l *FileLock) Close() error

Close closes the lock which implicitly unlocks it as well

func (*FileLock) ExclusiveLock added in v0.5.2

func (l *FileLock) ExclusiveLock() error

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

func (l *FileLock) Fd() (int, error)

Fd returns the lock's file descriptor, or an error if the lock is closed

func (*FileLock) SharedLock added in v0.5.2

func (l *FileLock) SharedLock() error

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

func (l *FileLock) TryExclusiveLock() error

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

func (l *FileLock) TrySharedLock() error

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.

func (*FileLock) Unlock added in v0.5.2

func (l *FileLock) Unlock() error

Unlock unlocks the lock

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

func ExclusiveKeyLock(lockDir string, key string) (*KeyLock, error)

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

func NewKeyLock(lockDir string, key string) (*KeyLock, error)

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

func SharedKeyLock(lockDir string, key string) (*KeyLock, error)

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

func TryExclusiveKeyLock(lockDir string, key string) (*KeyLock, error)

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

func TrySharedKeyLock(lockDir string, key string) (*KeyLock, error)

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

func (l *KeyLock) ExclusiveKeyLock() error

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

func (l *KeyLock) SharedKeyLock() error

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

func (l *KeyLock) TryExclusiveKeyLock() error

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

func (l *KeyLock) TrySharedKeyLock() error

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.

func (*KeyLock) Unlock added in v0.5.2

func (l *KeyLock) Unlock() error

Unlock unlocks the key lock.

type LockType added in v0.5.2

type LockType int
const (
	Dir LockType = iota
	RegFile
)

Jump to

Keyboard shortcuts

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