buildlock

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package buildlock provides an exclusive, crash-safe lock on a build output directory so two sarde processes cannot write into the same dist/ at once (a second dev server or a build racing a dev server silently corrupts fingerprinted assets; see my-docs/handoffs/dev-server-asset-404-handoff.md).

The lock is a kernel advisory lock held on <outputDir>/.sarde.lock for the lifetime of the owning session. The file also carries one line of JSON metadata (pid, command, version, start time) used purely for diagnostics: the OS lock is authoritative, so a crash releases the lock automatically and leftover lock files never require manual cleanup.

Limitations: sarde binaries built before this package never acquire the lock, so it cannot defend against them. Network filesystems (SMB, NFS) have unreliable locking semantics; keep the output dir on local disk.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RefCount

func RefCount(outputDir string) int

RefCount returns the in-process reference count for outputDir (0 when this process does not hold the lock). Intended for tests and diagnostics only.

Types

type ErrLocked

type ErrLocked struct {
	OutputDir string
	LockPath  string
	Meta      Metadata
	MetaErr   error
}

ErrLocked is returned by Acquire when another process holds the lock on the output directory. Meta is the zero value when MetaErr is non-nil.

func IsLocked

func IsLocked(err error) (*ErrLocked, bool)

IsLocked reports whether err is (or wraps) an *ErrLocked.

func (*ErrLocked) Error

func (e *ErrLocked) Error() string

type Lock

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

Lock is a held reference to an output directory's exclusive lock.

func Acquire

func Acquire(outputDir, cmd string) (*Lock, error)

Acquire creates outputDir if needed and takes an exclusive, non-blocking, crash-safe lock on it. cmd is a short diagnostic label ("dev", "build", "sidecar-preview", "sidecar-build") embedded in the lock file's metadata. Re-entrant within the same process: repeated calls for the same output directory succeed and reference-count instead of re-locking.

func (*Lock) Release

func (l *Lock) Release() error

Release drops this reference. The OS lock is released and the file closed only when the last in-process reference for the output directory is gone. The lock FILE is deliberately left in place: deleting it on release would let a contender holding a handle to the removed inode and a newcomer creating a fresh file both "acquire" the same directory. Idempotent and nil-safe.

type Metadata

type Metadata struct {
	PID     int       `json:"pid"`
	Cmd     string    `json:"cmd"`
	Version string    `json:"version"`
	Started time.Time `json:"started"`
}

Metadata describes the process holding a lock. Purely diagnostic: it is never used to decide whether the lock can be acquired.

Jump to

Keyboard shortcuts

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