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 ¶
- type Client
- func (c *Client) AnalyzeBuffer(args daemon.AnalyzeBufferArgs) (daemon.AnalyzeBufferResult, error)
- func (c *Client) AnalyzeBuffers(args daemon.AnalyzeBuffersArgs) (daemon.AnalyzeBuffersResult, error)
- func (c *Client) Shutdown() error
- func (c *Client) SocketPath() string
- func (c *Client) Status() (daemon.StatusResult, error)
- type SpawnOptions
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 ¶
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 ¶
Shutdown asks the daemon to exit. Safe to call after the daemon is already gone.
func (*Client) SocketPath ¶
SocketPath returns the configured socket path.
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.