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 ¶
const Suffix = ".lock"
Suffix is appended to the database path to name the lock file.
Variables ¶
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 ¶
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 ¶
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 ¶
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).