filelock

package
v1.40.1 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2024 License: Apache-2.0 Imports: 9 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, options ...LockerOption) (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 LockerOption added in v1.38.0

type LockerOption func(*lockerOptions)

LockerOption is an option for a new Locker.

func LockerWithLockRetryDelay added in v1.38.0

func LockerWithLockRetryDelay(lockRetryDelay time.Duration) LockerOption

LockerWithLockRetryDelay sets the default lock retry delay for the Locker.

If Lock/RLock is called with LockWithRetryDelay, that will override the default lock retry delay. If this is not set, the default lock retry delay is set to DefaultLockRetryDelay.

func LockerWithLockTimeout added in v1.38.0

func LockerWithLockTimeout(lockTimeout time.Duration) LockerOption

LockerWithLockTimeout sets the default lock timeout for the Locker.

If Lock/RLock is called with LockWithTimeout, that will override this default timeout. If this is not set, the default lock timeout is set to DefaultLockTimeout.

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