flock – github.com/gofrs/flock Index | Files

package flock

import "github.com/gofrs/flock"

Package flock implements a thread-safe 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 accidentally drop the exclusive lock.

Index

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 New

func New(path string, opts ...Option) *Flock

New returns a new instance of *Flock. The only parameter it takes is the path to the desired lockfile.

func NewFlock

func NewFlock(path string) *Flock

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

Deprecated: Use New instead.

func (*Flock) Close

func (f *Flock) Close() error

Close is equivalent to calling Unlock.

This will release the lock and close the underlying file descriptor. It will not remove the file from disk, that's up to your application.

func (*Flock) Lock

func (f *Flock) Lock() error

func (*Flock) Locked

func (f *Flock) Locked() bool

Locked returns the lock state (locked: true, unlocked: false).

Warning: by the time you use the returned value, the state may have changed.

func (*Flock) Path

func (f *Flock) Path() string

Path returns the path as provided in NewFlock().

func (*Flock) RLock

func (f *Flock) RLock() error

func (*Flock) RLocked

func (f *Flock) RLocked() bool

RLocked returns the read lock state (locked: true, unlocked: false).

Warning: by the time you use the returned value, the state may have changed.

func (*Flock) String

func (f *Flock) String() string

func (*Flock) TryLock

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

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 - Context Done channel is closed.

func (*Flock) TryRLock

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

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 - Context Done channel is closed.

func (*Flock) Unlock

func (f *Flock) Unlock() error

type Option

type Option func(f *Flock)

func SetFlag

func SetFlag(flag int) Option

SetFlag sets the flag used to create/open the file.

func SetPermissions

func SetPermissions(perm fs.FileMode) Option

SetPermissions sets the OS permissions to set on the file.

Source Files

flock.go flock_others.go

Version
v0.12.1 (latest)
Published
Jul 22, 2024
Platform
js/wasm
Imports
7 packages
Last checked
2 weeks ago

Tools for package owners.