writerlock

package
v1.0.521 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 4 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 writerlock takes the single-writer lock on a persisted-state directory (CHAOS-103, ADR-0060): an exclusive, non-blocking advisory lock on one well-known file inside the directory, held for as long as the returned Lock is open.

The package answers one question, "does a live process already hold this directory?", and reports one of three verdicts. Acquired: the caller now holds it. Held: a process that is running right now holds it; the kernel releases an flock when its owner dies, so there is no stale-lock case. Undetermined: the question could not be answered (no directory named, the directory or the lock file could not be created, the platform or the filesystem has no advisory locking).

What a verdict means is the caller's decision. Package main refuses to boot on Held unless the operator set an override, and degrades with a notice on Undetermined; none of that is here. The package reads no environment variable, logs nothing, starts no goroutine and has no package-level state. Each Take is independent, and the lock it returns is released only by Close or by the process exiting, so the caller must keep the Lock reachable for as long as it means to be the writer.

It is deliberately not built on internal/storeguard, whose locks are per-attempt markers armed and released around one store open. This lock has the opposite lifetime.

Index

Constants

View Source
const FileName = ".culvert-writer.lock"

FileName is the lock file's name inside the directory. It lives in the directory, not beside it, so it travels with a bind mount, a docker volume and an NFS export: the three ways two processes end up sharing one data root. The file is zero bytes and means nothing on its own; the flock is the state.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attempt

type Attempt struct {
	// Verdict is the outcome.
	Verdict Verdict
	// Path is the lock file path that was used. Empty only when no directory
	// was named.
	Path string
	// Cause is the underlying error for an Undetermined verdict, and nil
	// otherwise.
	Cause error
	// Lock is the held lock. Non-nil exactly when Verdict is Acquired.
	Lock *Lock
}

Attempt is what Take found.

func Take

func Take(dir string) Attempt

Take takes the single-writer lock on dir.

It writes nothing but the directory itself, when absent, and the zero-byte lock file, and it never blocks: a live holder is reported immediately, because waiting would turn a misconfiguration into a hang.

type Lock

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

Lock is a held single-writer lock. The flock is tied to the open file description, so the Lock must stay reachable for as long as the caller means to hold it: dropping it would let the descriptor be finalized and the lock released.

func (*Lock) Close

func (l *Lock) Close() error

Close releases the lock. The kernel also releases it when the process exits, which is how a process-lifetime holder gives it up.

type Verdict

type Verdict int

Verdict is the outcome of one acquisition attempt.

const (
	// Acquired means this caller is the single writer for as long as the Lock in
	// the Attempt stays open.
	Acquired Verdict = iota
	// Held means a live process holds the lock.
	Held
	// Undetermined means the lock state could not be established. Cause in the
	// Attempt says why.
	Undetermined
)

Jump to

Keyboard shortcuts

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