dblock

package
v0.222.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 9 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 dblock is the exclusive lock on a gofer database: `<db_path>.lock`, held for as long as a process uses the database (design docs/design/2026-11-03-config-into-db-design.md §架构「库锁」). serve takes it before it opens the database and keeps it until exit; init and the --offline commands hold it across probe, create and write, so they can never run beside a server (and not beside each other).

It is a leaf package (stdlib and golang.org/x/sys only): the data layer, the serve orchestration and the command entries all import it without a dependency cycle (G022).

The lock is an OS advisory lock on a sidecar file (unix flock(LOCK_EX|LOCK_NB), windows LockFileEx), so it is released by the kernel when the process dies — a crashed server never leaves a stale lock. The file itself is never deleted, which avoids the unlink/recreate race of lock files.

Index

Constants

View Source
const Suffix = ".lock"

Suffix is appended to the database path to name the lock file.

Variables

View Source
var ErrLocked = errors.New("dblock: the database is in use")

ErrLocked reports that another holder owns the lock: a server (or an offline command) is using the database. Any other error from this package is a failure of the locking mechanism itself (an unsupported file system, a permission problem) and is wrapped with its cause.

Functions

func Path

func Path(dbPath string) string

Path returns the lock file path for a database path.

Types

type Lock

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

Lock is a held database lock. Release it with Release; the zero value and nil are no-ops.

func Acquire

func Acquire(ctx context.Context, dbPath string, opts Options) (*Lock, error)

Acquire takes the lock, retrying while it is held by someone else until opts.Timeout elapses or ctx is done. The last ErrLocked is returned on timeout; a mechanism error is returned at once (retrying cannot fix it).

func TryAcquire

func TryAcquire(dbPath string) (*Lock, error)

TryAcquire makes one non-blocking attempt. It returns ErrLocked when the lock is held and any other error when the lock cannot be taken at all (the caller must not fall back to running without the lock).

func (*Lock) Path

func (l *Lock) Path() string

Path returns the lock file path.

func (*Lock) Release

func (l *Lock) Release() error

Release drops the lock. It is idempotent and safe on nil.

type Options

type Options struct {
	// Timeout is how long to keep retrying while the lock is held; 0 means one attempt.
	Timeout time.Duration
	// Interval is the pause between attempts; 0 means one second.
	Interval time.Duration
}

Options bound the retry of Acquire.

Jump to

Keyboard shortcuts

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