singleton

package
v0.76.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: AGPL-3.0 Imports: 10 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

Overview

Package singleton keeps a second lich launch from racing the pinned listener port and dying with a bare error. It owns runtime.json — the file recording the running instance's loopback coordinates ({pid,port,token}) — and, when a fresh launch cannot bind, uses it to tell "another live lich already holds my port" (a duplicate launch, exit cleanly and focus it) apart from "the port is taken by something else" (a real error). A duplicate launch asks the running lich for its window (Show), which opens one when the user had closed it: the backend outlives its window. The pinned port bind is itself the lock; this package is only the detection and the read/write of the file.

Index

Constants

View Source
const DefaultPort = 47821

DefaultPort is the loopback port lich pins when LICH_LISTEN_PORT does not override it. It lives here because binding it is the single-instance lock.

Variables

This section is empty.

Functions

func Ping

func Ping(port int, token string) bool

Ping reports whether a token-gated lich listener answers on port. It is the production probe passed to Detect: only lich serves /ping behind the token, so a 204 proves the recorded instance is alive and is lich.

func Port

func Port(getenv func(string) string) int

Port is the listener port this launch pins: LICH_LISTEN_PORT when it holds a number, DefaultPort otherwise. getenv reads the environment (os.Getenv in production), so a caller diagnosing a launch resolves the same port the launch would.

func Show

func Show(running *Info) error

Show asks the running lich to open its window, or to bring the open one to the front. An older lich answers it with an error: it has no such call, and always has a window open, which the caller then focuses the old way.

func UncleanExit

func UncleanExit(configDir, restartWait string) bool

UncleanExit reports whether the run before this one ended without quitting cleanly. Write records the runtime file at startup and the clean exit (`lich quit`, a signal, a restart) removes it, so a file still on disk when a fresh launch is about to write its own is the previous run's — it crashed, was killed, or the machine went down under it. Call it before Write, which overwrites the evidence.

restartWait is the restart.WaitEnv value: a successor is spawned while its predecessor still holds the file, and that handover is not a crash.

func Write

func Write(configDir string, port int, token string) (string, error)

Write records this process as the running instance. Mode 0600: the token is a loopback credential and the file persists across sessions. Returns the path so the caller can remove it on the clean exit.

Types

type BindVerdict

type BindVerdict int

BindVerdict is what a failed bind of the pinned listener port means.

const (
	// BindFailureIsReal: log and exit 1 — the port is held by something that is
	// not a lich we can hand over to, or this process is a restart successor,
	// which legitimately expects a busy port and retries the bind itself (so a
	// failure here is genuine).
	BindFailureIsReal BindVerdict = iota
	// BindFailureIsDuplicate: another live lich holds the port: show its
	// window and exit 0. Re-launching an app you already have open should give
	// you the window, not an error.
	BindFailureIsDuplicate
)

func BindFailureVerdict

func BindFailureVerdict(restartWait string, running *Info) BindVerdict

BindFailureVerdict decides what a failed bind means. restartWait is the restart.WaitEnv value; running is what Detect found, which the caller probes for only when restartWait is empty (a successor never pays for the probe).

type Info

type Info struct {
	PID   int    `json:"pid"`
	Port  int    `json:"port"`
	Token string `json:"token"`
}

Info is the running lich recorded in runtime.json. It lets install.sh reach a running lich for /restart when it runs outside a lich terminal, and lets a second launch find the instance already holding the pinned port.

func Detect

func Detect(configDir string, wantPort int, ping func(port int, token string) bool) (*Info, error)

Detect reports the live lich holding wantPort, or nil if there is none. It returns the recorded instance only when runtime.json names wantPort (so a dev instance on another port is never mistaken for this one) and its token pings back (proving the recorded process is both alive and actually lich, not a stray process that grabbed the port). ping is injectable for tests; production passes Ping. A nil result with a nil error means "not a duplicate launch" — the caller should treat the bind failure as the real error it is.

func Read

func Read(configDir string) (*Info, error)

Read loads runtime.json. A missing file returns (nil, nil): no instance recorded, not an error.

Jump to

Keyboard shortcuts

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