lock

package
v0.2.0 Latest Latest
Warning

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

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

Documentation

Overview

Package lock provides cross-process advisory file locking.

File acquires an exclusive lock on the file at path, blocking until the context is cancelled (or its deadline elapses). TryFile does the same but returns ErrContended immediately if the lock is already held. In both cases the returned release function drops the lock and closes the underlying file descriptor.

The lock is released automatically by the kernel on process termination (including crash), so no stale-lock recovery is needed.

The lock file at path is created if it does not exist. It is never unlinked — flock is keyed by inode, not path, and unlinking could create a window where two processes lock different inodes at the same path.

This is the canonical file-locking helper for Seshat. Callers should prefer it over rolling their own platform-specific code.

Index

Constants

This section is empty.

Variables

View Source
var ErrContended = errors.New("file lock is held by another process")

ErrContended is returned by TryFile when the lock is already held by another process.

Functions

func File

func File(ctx context.Context, path string) (func(), error)

File acquires an exclusive advisory lock on the file at path, blocking until the lock is acquired or ctx is cancelled. It returns a release function that drops the lock and closes the underlying file descriptor.

Pass a context with a deadline (e.g. context.WithTimeout) to bound the wait. Pass context.Background() to block indefinitely.

func TryFile

func TryFile(path string) (func(), error)

TryFile is like File but returns ErrContended immediately if the lock is already held by another process. Use this when you want to fail fast rather than wait.

Types

This section is empty.

Jump to

Keyboard shortcuts

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