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

lockfile

package
v0.32.1 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package lockfile serializes mutating d8 operations across concurrent processes.

Problem

Self-update and plugin install/update rewrite binaries, symlinks, and caches on disk. Two such commands can run concurrently (separate terminals or scripts); without a cross-process lock they would step on each other.

Call sites

Two features share this package (same mechanism, different paths):

  • CLI self-update: ~/.deckhouse-cli/cli/install.lock
  • Plugin install: <plugins-root>/<name>/install.lock

Usage

Hold the lock for the whole critical section. Do not block waiting - if another process holds a live lock, return a user-facing error:

release, err := lockfile.Acquire(path, staleAfter, onReclaim)
if errors.Is(err, lockfile.ErrLocked) {
    return fmt.Errorf("operation already in progress")
}
if err != nil {
    return err
}
defer release()

Stale locks

A holder killed with SIGKILL never runs release(). Acquire treats a lock file older than staleAfter as orphaned, reclaims it, and optionally notifies via onReclaim. Callers typically use one hour.

Implementation

  • Capture: atomic O_EXCL create (empty file at path; no open fd required).
  • Reclaim: rename to a scratch path, verify file identity, then remove. Plain Remove(path) races with a concurrent acquirer and can delete their fresh lock; identity-checked rename closes that hole.

Index

Constants

This section is empty.

Variables

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

ErrLocked means another process holds a live lock at the path. Callers should fail fast with a user-facing message instead of waiting.

Functions

func Acquire

func Acquire(path string, staleAfter time.Duration, onReclaim func(age time.Duration)) (func(), error)

Acquire creates an exclusive lock file at path for the duration of a mutating operation (self-update or plugin install). See package lockfile for call sites, the usage pattern, and reclaim semantics.

  • Returns release; call it exactly once when done (typically defer release()).
  • Returns ErrLocked if another process already holds a non-stale lock.
  • Reclaims a lock older than staleAfter before trying to acquire.
  • Calls onReclaim (optional) with the orphan's age after a successful reclaim.

Types

This section is empty.

Jump to

Keyboard shortcuts

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