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 ¶
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, and because the page's localStorage (lich.* settings) is keyed by the origin it forms — a moved port is a wiped settings store.
Variables ¶
This section is empty.
Functions ¶
func Ping ¶
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 ¶
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 ¶
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 ¶
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.
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 ¶
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 ¶
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.