lock

package
v0.0.0-...-10bf2d1 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package lock implements advisory shared and exclusive filesystem locks.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrContended = errors.New("lock is held")
	ErrReleased  = errors.New("lock is already released")
)

Functions

func OwnerMatches

func OwnerMatches(metadata Metadata) (bool, error)

OwnerMatches verifies both PID existence and process-start evidence, avoiding false ownership after PID reuse. A false result with a non-nil error is indeterminate and must not be treated as proof that a lock is stale.

Types

type ContentionError

type ContentionError struct {
	Path   string
	Holder *Metadata
}

func (*ContentionError) Error

func (e *ContentionError) Error() string

func (*ContentionError) Unwrap

func (e *ContentionError) Unwrap() error

type Lock

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

func Acquire

func Acquire(ctx context.Context, path string, mode Mode, metadata Metadata) (*Lock, error)

Acquire waits until the lock is acquired or ctx is cancelled. Exclusive acquisitions replace metadata while holding the kernel lock.

func TryAcquire

func TryAcquire(path string, mode Mode, metadata Metadata) (*Lock, error)

func (*Lock) DuplicateFile

func (l *Lock) DuplicateFile() (*os.File, error)

DuplicateFile returns a descriptor for the same open file description. When passed through exec.Cmd.ExtraFiles, it keeps the advisory lock alive if the wrapper process exits before its child.

func (*Lock) Path

func (l *Lock) Path() string

func (*Lock) Release

func (l *Lock) Release() error

type Metadata

type Metadata struct {
	PID          int       `json:"pid"`
	ProcessStart string    `json:"process_start"`
	Hostname     string    `json:"hostname"`
	Command      string    `json:"command"`
	Nonce        string    `json:"nonce"`
	AcquiredAt   time.Time `json:"acquired_at"`
}

func NewMetadata

func NewMetadata(command string) (Metadata, error)

func NewMetadataForPID

func NewMetadataForPID(pid int, command string) (Metadata, error)

func ReadMetadata

func ReadMetadata(path string) (*Metadata, error)

type Mode

type Mode int
const (
	Shared Mode = iota
	Exclusive
)

Jump to

Keyboard shortcuts

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