flock

package
v0.13.3 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package flock provides a small cross-process advisory-lock primitive built on POSIX flock (Unix) / LockFileEx (Windows). It exists so that checkpoint and strategy can both serialize on shared resources without one taking the other as an import dependency.

Index

Constants

This section is empty.

Variables

View Source
var ErrLocked = errors.New("flock: lock is held")

ErrLocked reports that TryAcquireIn found the lock held by someone else.

Functions

func Acquire

func Acquire(path string) (release func(), err error)

Acquire takes an exclusive advisory lock on path, creating the file if needed. The returned release closes the file, which drops the flock. Callers must invoke release exactly once. The lock file persists between runs - flock state is held by the file descriptor, not by the inode on disk - so the lockfile contents are immaterial.

Acquire blocks indefinitely until the lock is available. Use AcquireContext with a deadline to bound the wait.

func AcquireContext added in v0.10.0

func AcquireContext(ctx context.Context, path string) (release func(), err error)

AcquireContext behaves like Acquire but honors ctx. When ctx carries a deadline it polls a non-blocking lock until the lock is acquired or the deadline/cancellation fires, returning a wrapped ctx.Err() on timeout. When ctx has no deadline it takes the same blocking kernel path as Acquire, so existing callers keep their exact behavior. This lets latency-critical hooks (turn-start) bound their wait and degrade gracefully instead of stalling behind a long-running lock holder (e.g. checkpoint condensation).

func AcquireContextIn added in v0.10.4

func AcquireContextIn(ctx context.Context, root *os.Root, name string) (release func(), err error)

AcquireContextIn is AcquireContext for a lock file named inside root.

func AcquireIn added in v0.10.4

func AcquireIn(root *os.Root, name string) (release func(), err error)

AcquireIn is Acquire for a lock file named inside root. It is the form the .git-resident locks use: their names carry agent-supplied session IDs, so resolving them through the git common dir's root keeps a name that escaped validation from naming a file outside the clone.

func TryAcquireIn added in v0.13.3

func TryAcquireIn(root *os.Root, name string) (release func(), err error)

TryAcquireIn takes the lock named inside root if it is free right now, and returns ErrLocked if another holder has it. Use it to tell a caller that a wait is about to begin, then wait with AcquireContextIn.

It is AcquireContextIn with a deadline already past: both platforms make one non-blocking attempt before they first consult the context, so an expired one means exactly one attempt.

Types

This section is empty.

Jump to

Keyboard shortcuts

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