lock

package
v1.261126.1 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: MPL-2.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrLocked = errors.New("lock already held: server may already be running")

ErrLocked is what TryLock returns when another process holds the lock: the one failure that means "an instance is already running". Callers tell it apart from the others (an unusable config directory, a full disk) with errors.Is, because only a held lock calls for focusing or taking over the running instance.

Functions

This section is empty.

Types

type FileLock

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

FileLock manages exclusive file locking for single-instance enforcement. The lock is automatically released when the process dies, even if it crashes. It also stores the current process PID for signal-based shutdown.

func NewFileLock

func NewFileLock(configDir string) *FileLock

NewFileLock creates a new file lock instance. The lock file will be created in the specified config directory.

func (*FileLock) GetLockFilePath

func (fl *FileLock) GetLockFilePath() string

GetLockFilePath returns the lock file path for debugging purposes.

func (*FileLock) GetPID

func (fl *FileLock) GetPID() (int, error)

GetPID returns the PID stored in the lock file. Returns error if the lock file doesn't exist or contains invalid data.

func (*FileLock) IsLocked

func (fl *FileLock) IsLocked() bool

IsLocked checks if the lock is currently held by another process. Returns false if the lock is available or if this process holds it.

func (*FileLock) ReadPort

func (fl *FileLock) ReadPort() (int, error)

ReadPort returns the port recorded by the running server. Callers must gate on IsLocked() and treat any error as "port unknown" (fall back to config).

func (*FileLock) ReadVersion

func (fl *FileLock) ReadVersion() (string, error)

ReadVersion returns the version recorded by the running server. Callers must gate on IsLocked() and treat any error as "version unknown" — servers started by builds predating version recording never wrote one.

func (*FileLock) RemoveRuntimeFiles

func (fl *FileLock) RemoveRuntimeFiles() error

RemoveRuntimeFiles deletes every runtime artifact tied to this lock (port and version files). Unlock already does this for the lock holder; the stop command uses it to clean up after a server that was killed without releasing the lock itself. Adding a new runtime artifact means adding it here (and to the per-platform constructors) so no removal site is missed.

func (*FileLock) TryLock

func (fl *FileLock) TryLock() error

TryLock attempts to acquire the file lock. Returns an error if the lock is already held by another process. The lock file remains on disk but is unlocked when this process dies. On success, stores the current process PID in the lock file for shutdown signals.

func (*FileLock) Unlock

func (fl *FileLock) Unlock() error

Unlock releases the file lock. Safe to call multiple times; subsequent calls are no-ops.

func (*FileLock) WritePort

func (fl *FileLock) WritePort(port int) error

WritePort records the port the running server is listening on. It is a runtime artifact tied to this lock's lifetime: written after TryLock and removed by Unlock (or RemoveRuntimeFiles on the stopping side).

func (*FileLock) WriteVersion

func (fl *FileLock) WriteVersion(version string) error

WriteVersion records the running server's build version. Same lifetime rules as WritePort.

type PortFile

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

PortFile records the port the running server is actually listening on. Other CLI processes (cc/profile/log/status/open) read it to discover the live port, since the server port is intentionally not persisted in the config file. See .design/runtime-port-file.md.

func NewPortFile

func NewPortFile(configDir string) *PortFile

NewPortFile creates a port file handle for the given config directory.

func (*PortFile) Read

func (pf *PortFile) Read() (int, error)

Read returns the recorded port. Callers should treat any error as "port unknown" and fall back to the configured port.

func (*PortFile) Remove

func (pf *PortFile) Remove() error

Remove deletes the port file. A missing file is not an error.

func (*PortFile) Write

func (pf *PortFile) Write(port int) error

Write records the listening port.

type VersionFile

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

VersionFile records the build version of the running server. Launchers read it to tell whether the running server matches their own version — a missing or unreadable file means "version unknown" (e.g. a server started by a build predating version recording). Same lifecycle and reader rules as PortFile.

func NewVersionFile

func NewVersionFile(configDir string) *VersionFile

NewVersionFile creates a version file handle for the given config directory.

func (*VersionFile) Read

func (vf *VersionFile) Read() (string, error)

Read returns the recorded version. Callers should treat any error as "version unknown".

func (*VersionFile) Remove

func (vf *VersionFile) Remove() error

Remove deletes the version file. A missing file is not an error.

func (*VersionFile) Write

func (vf *VersionFile) Write(version string) error

Write records the running server's version.

Jump to

Keyboard shortcuts

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