filelock

package module
v0.0.0-...-1dbf710 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2017 License: MIT Imports: 7 Imported by: 12

README

go-filelock

GoDoc License

Linux
Windows

Package go-filelock provides a cross-process mutex based on file locks that works on windows and *nix platforms.

Installation

go get github.com/zbiljic/go-filelock

Example:

import github.com/zbiljic/go-filelock

fl, err := filelock.New(filename)
if err != nil {
    panic(err)
}
var lock filelock.TryLockerSafe
lock, err = fl.Lock()
if err != nil {
    panic(err)
}
defer lock.Unlock()

...

See the reference for more info.


Copyright © 2017 Nemanja Zbiljić

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

View Source
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

View Source
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.

func New

func New(path string) (TryLockerSafe, error)

New creates a new lock

Jump to

Keyboard shortcuts

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