Documentation
¶
Overview ¶
Package supervise keeps one runtime server child process alive: start, health-gate, restart with exponential backoff on crash, graceful stop. It began as the llama.cpp adapter's supervisor and is shared by every runtime family the daemon runs as a subprocess (SPEC §A1.3: subprocess, never cgo): a runtime crash never takes the daemon down.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrExited = errors.New("it exited")
ErrExited is why a start failed when the child exited before it was healthy. Test with errors.Is; the error itself is an *ExitedError.
Functions ¶
func EphemeralPort ¶
EphemeralPort asks the kernel for a free loopback port. Inherently a small TOCTOU race, but standard practice for supervising servers that pick their own listener from a --port flag.
Types ¶
type ExitedError ¶
type ExitedError struct{ Err error }
ExitedError is a child that exited before it was healthy. Err is what Wait returned: nil for a child that exited with status 0, as a server does that could not bind its port and did not think it worth an error.
func (*ExitedError) Error ¶
func (e *ExitedError) Error() string
func (*ExitedError) Is ¶
func (*ExitedError) Is(target error) bool
Is makes errors.Is(err, ErrExited) true.
type FatalError ¶
type FatalError struct{ Err error }
FatalError, returned by a health check, ends the wait for a healthy child at once: the check knows that waiting cannot help (the port is answered by another process).
func (*FatalError) Error ¶
func (e *FatalError) Error() string
func (*FatalError) Unwrap ¶
func (e *FatalError) Unwrap() error
type Options ¶
type Options struct {
// Name is the child as logs and errors call it ("llama-server").
Name string
// Prefix opens every error ("llamacpp").
Prefix string
Bin string
Args []string
// Env is added to the daemon's environment for the child; nil = the
// daemon's environment as it is.
Env []string
// URL is the child's base address (http://127.0.0.1:<port>).
URL string
// Health reports whether the child is ready to serve. Nil = GET
// URL/health answers 200.
Health func(ctx context.Context) error
// StartTimeout bounds the wait for the first healthy answer, and for
// each one after a restart. 0 = 120 s.
StartTimeout time.Duration
Log *slog.Logger
}
Options describes the child to supervise.
type Supervisor ¶
type Supervisor struct {
// contains filtered or unexported fields
}
Supervisor owns one child process.
func (*Supervisor) CheckHealth ¶
func (s *Supervisor) CheckHealth(ctx context.Context) error
CheckHealth is the default health check: GET URL/health answers 200.
func (*Supervisor) Kill ¶
func (s *Supervisor) Kill()
Kill ends the running child without stopping the supervisor: the supervise loop sees it exit and starts a fresh one. It is how work is stopped on a runtime that cannot abort a request any other way.
func (*Supervisor) PID ¶
func (s *Supervisor) PID() int
PID returns the live child's pid, 0 when none.
func (*Supervisor) Restarts ¶
func (s *Supervisor) Restarts() int
Restarts counts the respawns after a crash.
func (*Supervisor) Start ¶
func (s *Supervisor) Start(ctx context.Context) error
Start launches the child and blocks until it reports healthy (or ctx ends). It then keeps supervising in the background.
func (*Supervisor) Stop ¶
func (s *Supervisor) Stop(ctx context.Context)
Stop terminates the child gracefully: terminate() (SIGTERM; a console break on Windows, or nothing without a console), then Kill after 5s, on every platform. Closing the guard last kills anything still in the job on Windows and is a no-op elsewhere.
func (*Supervisor) Tail ¶
func (s *Supervisor) Tail() string
Tail is the child's most recent output, for a load that failed.