daemonclient

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package daemonclient lets short-lived CLI verbs call a long-lived krit daemon when one is reachable, and fall back to in-process execution otherwise. Auto-spawn (start `krit serve` if no daemon is running) is opt-in via EnsureRunning.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is a lightweight wrapper that remembers a socket path and lets verbs send daemon.Call without re-resolving the path. A zero-value Client (or nil) is unusable; construct via Discover or EnsureRunning.

func Discover

func Discover(repoRoot string) (*Client, bool)

Discover resolves the daemon socket path for a project rooted at repoRoot and returns a Client only when a daemon is reachable. Returns (nil, false) when no daemon is running — callers should fall back to in-process execution or call EnsureRunning to spawn one.

func EnsureCompatible

func EnsureCompatible(repoRoot string, opts SpawnOptions) (*Client, bool, error)

EnsureCompatible discovers a running daemon, compares its reported binary hash against the running CLI's, and shuts the daemon down + (when opts.AutoRestart is true) spawns a fresh one when they differ. Use this from CLI entry points so a `krit` upgrade doesn't leave callers talking to a stale daemon. Returns nil with ok=false when no daemon is running and AutoRestart is false — callers fall back to in-process.

func EnsureRunning

func EnsureRunning(repoRoot string, opts SpawnOptions) (*Client, error)

EnsureRunning returns a Client connected to a daemon at the conventional socket path under repoRoot. If no daemon is reachable, EnsureRunning forks `<binary> serve --root <repoRoot>` in the background and waits until its socket is reachable.

The spawned process is detached: closing the parent process does not stop the daemon. Callers that want a self-contained lifecycle (e.g. tests) should send `daemon.VerbShutdown` explicitly before exiting.

func (*Client) AnalyzeBuffer

func (c *Client) AnalyzeBuffer(args daemon.AnalyzeBufferArgs) (daemon.AnalyzeBufferResult, error)

AnalyzeBuffer dispatches the analyze-buffer verb against the daemon. Returns the daemon's response on success.

func (*Client) AnalyzeBuffers

func (c *Client) AnalyzeBuffers(args daemon.AnalyzeBuffersArgs) (daemon.AnalyzeBuffersResult, error)

AnalyzeBuffers dispatches the batched analyze-buffers verb. The daemon processes the entire batch in one round trip, so callers with N staged files trade N dial+RTT cycles for one.

func (*Client) Shutdown

func (c *Client) Shutdown() error

Shutdown asks the daemon to exit. Safe to call after the daemon is already gone.

func (*Client) SocketPath

func (c *Client) SocketPath() string

SocketPath returns the configured socket path.

func (*Client) Status

func (c *Client) Status() (daemon.StatusResult, error)

Status returns the daemon's status payload. Mostly useful for the CLI's --daemon-status flag and for tests.

type SpawnOptions

type SpawnOptions struct {
	// Binary is the krit binary to exec. Empty defaults to os.Executable
	// (the current process's binary), which is the right choice when
	// the CLI itself spawns the daemon.
	Binary string
	// WaitTimeout is how long to wait for the daemon's socket to come
	// up after fork. Zero defaults to 2 seconds — short enough that a
	// pre-commit hook with this on the critical path stays responsive,
	// long enough that a normal cold start still wins. First-run cold
	// projects with massive warm cost should bump this explicitly.
	WaitTimeout time.Duration
	// PollInterval controls how often EnsureRunning re-checks for the
	// socket. Zero defaults to 25ms.
	PollInterval time.Duration
	// Env, when non-nil, overrides the spawned daemon's environment.
	// Nil inherits os.Environ.
	Env []string
	// LogPath, when non-empty, is the file the spawned daemon's
	// stdout+stderr land in. Empty discards them so daemon banner
	// output doesn't leak into the parent's terminal (relevant for
	// pre-commit hooks).
	LogPath string
	// AutoRestart controls EnsureCompatible's behaviour on a
	// version mismatch and on a missing daemon. False = honor only
	// the running daemon; true = spawn a fresh one when needed.
	AutoRestart bool
}

SpawnOptions controls EnsureRunning's behaviour.

Jump to

Keyboard shortcuts

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