filelock

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2022 License: Apache-2.0 Imports: 8 Imported by: 3

Documentation

Index

Constants

View Source
const (
	// DefaultLockTimeout is the default lock timeout.
	DefaultLockTimeout = 3 * time.Second
	// DefaultLockRetryDelay is the default lock retry delay.
	DefaultLockRetryDelay = 200 * time.Millisecond
)

Variables

This section is empty.

Functions

This section is empty.

Types

type LockOption

type LockOption func(*lockOptions)

LockOption is an option for lock.

func LockWithRetryDelay

func LockWithRetryDelay(retryDelay time.Duration) LockOption

LockWithRetryDelay returns a new LockOption that sets the lock retry delay.

Lock will try to lock on this delay up until the lock timeout.

func LockWithTimeout

func LockWithTimeout(timeout time.Duration) LockOption

LockWithTimeout returns a new LockOption that sets the lock timeout.

Lock returns error if the lock cannot be acquired after this amount of time. If this is set to 0, the lock will never timeout.

type Locker

type Locker interface {
	// Lock locks a file lock within the root directory of the Locker.
	//
	// The given path must be normalized and relative.
	Lock(ctx context.Context, path string, options ...LockOption) (Unlocker, error)
	// RLock read-locks a file lock within the root directory of the Locker.
	//
	// The given path must be normalized and relative.
	RLock(ctx context.Context, path string, options ...LockOption) (Unlocker, error)
}

Locker provides file locks.

func NewLocker

func NewLocker(rootDirPath string) (Locker, error)

NewLocker returns a new Locker for the given root directory path.

The root directory path should generally be a data directory path. The root directory must exist.

func NewNopLocker

func NewNopLocker() Locker

NewNopLocker returns a new no-op Locker.

type Unlocker

type Unlocker interface {
	Unlock() error
}

Unlocker unlocks a file lock.

func Lock

func Lock(ctx context.Context, filePath string, options ...LockOption) (Unlocker, error)

Lock locks a file lock.

Use in cases where you need a lock for a specific system file, such as in testing, otherwise use a Locker to manage your file locks.

func RLock

func RLock(ctx context.Context, filePath string, options ...LockOption) (Unlocker, error)

RLock read-locks a file lock.

Use in cases where you need a lock for a specific system file, such as in testing, otherwise use a Locker to manage your file locks.

Jump to

Keyboard shortcuts

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