flock

package
v1.4.7 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2019 License: MIT, BSD-3-Clause Imports: 5 Imported by: 0

README

go-flock

TravisCI Build Status GoDoc License

flock implements a thread-safe sync.Locker interface for file locking. It also includes a non-blocking TryLock() function to allow locking without blocking execution.

License

flock is released under the BSD 3-Clause License. See the LICENSE file for more details.

Go Compatibility

This package makes use of the context package that was introduced in Go 1.7. As such, this package has an implicit dependency on Go 1.7+.

Installation

go get -u github.com/theckman/go-flock

Usage

import "gitee.com/johng/gf/third/github.com/theckman/go-flock"

fileLock := flock.NewFlock("/var/lock/go-lock.lock")

locked, err := fileLock.TryLock()

if err != nil {
	// handle locking error
}

if locked {
	// do work
	fileLock.Unlock()
}

For more detailed usage information take a look at the package API docs on GoDoc.

Documentation

Overview

Package flock implements a thread-safe sync.Locker interface for file locking. It also includes a non-blocking TryLock() function to allow locking without blocking execution.

Package flock is released under the BSD 3-Clause License. See the LICENSE file for more details.

While using this library, remember that the locking behaviors are not guaranteed to be the same on each platform. For example, some UNIX-like operating systems will transparently convert a shared lock to an exclusive lock. If you Unlock() the flock from a location where you believe that you have the shared lock, you may accidently drop the exclusive lock.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Flock

type Flock struct {
	// contains filtered or unexported fields
}

Flock is the struct type to handle file locking. All fields are unexported, with access to some of the fields provided by getter methods (Path() and Locked()).

func NewFlock

func NewFlock(path string) *Flock

NewFlock is a function to return a new instance of *Flock. The only parameter it takes is the path to the desired lockfile.

func (*Flock) Lock

func (f *Flock) Lock() error

Lock is a blocking call to try and take an exclusive file lock. It will wait until it is able to obtain the exclusive file lock. It's recommended that TryLock() be used over this function. This function may block the ability to query the current Locked() or RLocked() status due to a RW-mutex lock.

If we are already exclusive-locked, this function short-circuits and returns immediately assuming it can take the mutex lock.

If the *Flock has a shared lock (RLock), this may transparently replace the shared lock with an exclusive lock on some UNIX-like operating systems. Be careful when using exclusive locks in conjunction with shared locks (RLock()), because calling Unlock() may accidentally release the exclusive lock that was once a shared lock.

func (*Flock) Locked

func (f *Flock) Locked() bool

Locked is a function to return the current lock state (locked: true, unlocked: false).

func (*Flock) Path

func (f *Flock) Path() string

Path is a function to return the path as provided in NewFlock().

func (*Flock) RLock

func (f *Flock) RLock() error

RLock is a blocking call to try and take a ahred file lock. It will wait until it is able to obtain the shared file lock. It's recommended that TryRLock() be used over this function. This function may block the ability to query the current Locked() or RLocked() status due to a RW-mutex lock.

If we are already shared-locked, this function short-circuits and returns immediately assuming it can take the mutex lock.

func (*Flock) RLocked

func (f *Flock) RLocked() bool

RLocked is a function to return the current read lock state (locked: true, unlocked: false).

func (*Flock) String

func (f *Flock) String() string

func (*Flock) TryLock

func (f *Flock) TryLock() (bool, error)

TryLock is the preferred function for taking an exclusive file lock. This function takes an RW-mutex lock before it tries to lock the file, so there is the possibility that this function may block for a short time if another goroutine is trying to take any action.

The actual file lock is non-blocking. If we are unable to get the exclusive file lock, the function will return false instead of waiting for the lock. If we get the lock, we also set the *Flock instance as being exclusive-locked.

func (*Flock) TryLockContext

func (f *Flock) TryLockContext(ctx context.Context, retryDelay time.Duration) (bool, error)

TryLockContext repeatedly tries to take an exclusive lock until one of the conditions is met: TryLock succeeds, TryLock fails with error, or Context Done channel is closed.

func (*Flock) TryRLock

func (f *Flock) TryRLock() (bool, error)

TryRLock is the preferred function for taking a shared file lock. This function takes an RW-mutex lock before it tries to lock the file, so there is the possibility that this function may block for a short time if another goroutine is trying to take any action.

The actual file lock is non-blocking. If we are unable to get the shared file lock, the function will return false instead of waiting for the lock. If we get the lock, we also set the *Flock instance as being share-locked.

func (*Flock) TryRLockContext

func (f *Flock) TryRLockContext(ctx context.Context, retryDelay time.Duration) (bool, error)

TryRLockContext repeatedly tries to take a shared lock until one of the conditions is met: TryRLock succeeds, TryRLock fails with error, or Context Done channel is closed.

func (*Flock) Unlock

func (f *Flock) Unlock() error

Unlock is a function to unlock the file. This file takes a RW-mutex lock, so while it is running the Locked() and RLocked() functions will be blocked.

This function short-circuits if we are unlocked already. If not, it calls syscall.LOCK_UN on the file and closes the file descriptor. It does not remove the file from disk. It's up to your application to do.

Please note, if your shared lock became an exclusive lock this may unintentionally drop the exclusive lock if called by the consumer that believes they have a shared lock. Please see Lock() for more details.

Jump to

Keyboard shortcuts

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