Help us shape the future of Go! Take the 2026 Go Developer Survey

filelock

package
v0.37.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package filelock is an inter-process exclusive lock on one path.

It is the mechanism internal/common/distfiles uses for its per-distfile locks, generalised to any path: the lock file is created with O_EXCL, held with flock(2) LOCK_EX for as long as the holder lives, and carries the holder's PID for the human who finds it. Whether a lock is live is decided by the kernel, never by the PID: a lock file whose flock can be taken belongs to a process that has died, and is reaped — but only after confirming the path still names the same inode, so a reap can never remove a live lock that replaced the dead one.

Index

Constants

This section is empty.

Variables

View Source
var (
	Wait = 2 * time.Minute
	Poll = 25 * time.Millisecond
)

Wait bounds how long Acquire waits for a live holder, and Poll is the interval between attempts. They are variables so tests can shorten them.

View Source
var ErrLocked = errors.New("lock is held by another process")

ErrLocked is returned by Acquire when another live process kept the lock for longer than Wait.

Functions

This section is empty.

Types

type Lock

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

Lock is a held lock. Release gives it up.

func Acquire

func Acquire(path, purpose string) (*Lock, error)

Acquire takes the exclusive lock at path, waiting up to Wait for a live holder and reaping the lock file of a holder that has died. purpose is written into the lock file so a person who finds it knows what holds it.

On timeout the error wraps ErrLocked and names path and the holder's PID. Any other failure is wrapped naming path and does not wrap ErrLocked.

func (*Lock) Release

func (l *Lock) Release()

Release gives up the lock: the file is unlinked while its flock is still held, and only if the path still names the file this Lock holds, then it is closed. It is safe on a nil Lock and safe to call twice.

Jump to

Keyboard shortcuts

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