unixlock

package module
v0.0.0-...-4211ac5 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2025 License: MIT Imports: 14 Imported by: 2

Documentation

Overview

Package unixlock provides inter-process mutual exclusion using Unix domain sockets. It creates a cooperative mutex at a specified file path, allowing processes to:

  • Acquire and release locks.
  • Communicate with the lock owner to request graceful shutdown.
  • Query the owner's process ID (PID).
  • Verify if the owner is an ancestor process.

The mutex supports non-blocking and blocking lock acquisition, context-aware cancellation, and status reporting between processes.

Index

Constants

This section is empty.

Variables

View Source
var ErrShutdown = errors.New("shutdown")

ErrShutdown indicates the context was canceled due to a shutdown request.

View Source
var ErrUnlocked = errors.New("unlocked")

ErrUnlocked indicates the context was canceled because the lock was closed.

Functions

func Report

func Report(ctx context.Context, m *Mutex, status error) error

Report sends a status message to the parent process if it is the current lock owner. A nil status indicates success. Only one report can be sent per mutex; subsequent calls are no-ops. It returns an error if the report cannot be sent.

func WaitForReport

func WaitForReport(ctx context.Context, m *Mutex) error

WaitForReport blocks until a status report is received via the Unix domain socket or the context is canceled. It is used by parent processes to wait for initialization status from a child process.

func WithLock

func WithLock(ctx context.Context, m *Mutex) (context.Context, error)

WithLock returns a context that is canceled when the input context is canceled, the mutex is unlocked (ErrUnlocked), or a shutdown request is received (ErrShutdown). It returns os.ErrInvalid if the mutex is not held by the current process. The mutex is not automatically unlocked when the context is canceled.

Types

type Mutex

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

func New

func New(fpath string) *Mutex

New creates a cooperative mutual exclusion lock instance using a Unix domain socket at the specified file path. The mutex supports inter-process communication for shutdown requests, PID queries, and ancestor checks.

func (*Mutex) CheckAncestor

func (m *Mutex) CheckAncestor(ctx context.Context) error

CheckAncestor verifies if the lock owner is a parent or ancestor of the current process. It returns nil if the check passes, otherwise an error. The lock owner tracks PIDs of processes that pass this check to support deeply nested descendants.

func (*Mutex) Close

func (m *Mutex) Close()

Close releases the lock if held by the current process and cancels all derived contexts with os.ErrClosed. It waits for all associated goroutines to terminate.

func (*Mutex) Lock

func (m *Mutex) Lock(ctx context.Context, shutdown bool) (unlockf func(), status error)

Lock acquires the lock, waiting until it is available or the context expires. If shutdown is true, it sends a shutdown request to the owner before waiting. It returns an unlock function and nil on success, or an error on failure.

func (*Mutex) Owner

func (m *Mutex) Owner(ctx context.Context) (int, error)

Owner returns the PID of the process currently holding the lock.

func (*Mutex) SocketPath

func (m *Mutex) SocketPath() string

SocketPath returns the monitor socket path.

func (*Mutex) TryLock

func (m *Mutex) TryLock(ctx context.Context) (unlockf func(), status error)

TryLock attempts to acquire the lock without waiting. It returns an unlock function and nil error on success. It returns os.ErrInvalid if the lock is held by the current process, or another error if acquisition fails.

Jump to

Keyboard shortcuts

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