lock

package
v0.0.0-...-f7c6332 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAlreadyRunning = errors.New("another instance is already running")

ErrAlreadyRunning is returned by TryAcquire when another instance owns the socket path and is actively listening.

Functions

This section is empty.

Types

type Acquired

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

Acquired represents a successfully acquired singleton lock. Call Release() when the current process is done to clean up the socket file.

func TryAcquire

func TryAcquire(path string) (*Acquired, error)

TryAcquire attempts to claim the given UNIX socket path as a singleton lock.

Strategy:

  1. Try net.Listen("unix", path). - Success → we are the owner; return Acquired. - EADDRINUSE → check if the existing socket is stale (connect fails). If stale, remove it and retry once. If live, return ErrAlreadyRunning.
  2. If the directory does not exist, it is created automatically.

func (*Acquired) Listener

func (a *Acquired) Listener() net.Listener

Listener returns the underlying net.Listener so callers can hand it off to an IPC server (e.g. ipc.ListenFrom) without closing and re-opening the socket, which would introduce a race window.

After calling Listener(), the caller owns the net.Listener. Release() will still remove the socket file but will no longer close the listener (since the caller now manages its lifetime).

func (*Acquired) Path

func (a *Acquired) Path() string

Path returns the socket path owned by this lock.

func (*Acquired) Release

func (a *Acquired) Release() error

Release closes the listening socket (if still owned) and removes the socket file.

Jump to

Keyboard shortcuts

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