Documentation
¶
Overview ¶
Package readiness answers "can the server I just launched actually be reached yet?" for every entry point that starts one (start, restart, open, the TUI). A bound lock file or a forked process proves nothing: the URL a launcher prints is only honest once the HTTP health endpoint answers.
The wait is a small state machine:
starting --(health 200 from *this* instance)--> ready starting --(the process ended)---------------> failed starting --(total timeout, process alive)----> still starting
Every observation is classified so a timeout can say *why* it did not become ready: the port never accepted a connection, the port accepted but the health endpoint was not 200, or the port was answered by a different instance than the one being started.
Index ¶
Constants ¶
const ( // DefaultPollInterval is how often the health endpoint is probed. DefaultPollInterval = 100 * time.Millisecond // DefaultRequestTimeout bounds a single health request. It is short so a // single slow/hung probe cannot hide an early process exit for long. DefaultRequestTimeout = time.Second )
const HealthPath = "/api/v1/info/health"
HealthPath is the unauthenticated endpoint a ready server answers 200 on.
Variables ¶
This section is empty.
Functions ¶
func NewInstanceToken ¶ added in v1.261203.0
func NewInstanceToken() string
NewInstanceToken returns a random token a launcher hands to the server it starts and then requires back on the health response.
Types ¶
type ExitedError ¶
type ExitedError struct {
Err error
}
ExitedError means the server process ended before it became ready. Err is the process's exit status or the error its Start returned (may be nil).
func (*ExitedError) Error ¶
func (e *ExitedError) Error() string
func (*ExitedError) ExitCode ¶ added in v1.261203.0
func (e *ExitedError) ExitCode() (int, bool)
ExitCode returns the process exit code when Err is an *exec.ExitError.
func (*ExitedError) Unwrap ¶
func (e *ExitedError) Unwrap() error
type Options ¶ added in v1.261203.0
type Options struct {
// URL is the health endpoint URL (see ProbeURL).
URL string
// Timeout bounds the whole wait. Zero or negative means "no total
// timeout": wait until ready, the process exits, or ctx is cancelled.
Timeout time.Duration
// Exited, when non-nil, yields the process result when the process being
// waited for ends. A closed channel is treated as "exited, reason
// unknown". Pass nil when there is no process to watch (e.g. probing an
// already-running server).
Exited <-chan error
// InstanceToken, when set, is the value the server must echo in the
// constant.HealthInstanceHeader response header. It lets a restart
// distinguish the instance it just started from an older server that is
// still shutting down and answering on the same port. Empty accepts any
// tingly-box health response.
InstanceToken string
}
Options configures Wait.
type Outcome ¶ added in v1.261203.0
type Outcome struct {
State State
URL string
Elapsed time.Duration
Timeout time.Duration
// Port is the last observed port state.
Port PortState
// StatusCode is the last HTTP status seen (0 when no response).
StatusCode int
// LastErr is the last probe error (nil when the last probe got HTTP).
LastErr error
// ExitErr is the process result when State is StateFailed.
ExitErr error
// OtherInstance reports that the port answered 200 but was not the
// instance being waited for (a restart race with the old server).
OtherInstance bool
// CtxErr is set when State is StateCanceled.
CtxErr error
}
Outcome is what Wait observed. Err renders it as an error.
func Wait ¶
Wait polls the health endpoint and returns the outcome. It returns as soon as the endpoint answers 200 for this instance, as soon as the watched process exits, at the total timeout, or when ctx ends — whichever first.
type PortState ¶ added in v1.261203.0
type PortState string
PortState classifies the last probe's view of the port.
const ( // PortUntested means no probe completed. PortUntested PortState = "untested" // PortUnreachable means nothing accepted a TCP connection on the port. PortUnreachable PortState = "unreachable" // PortReachable means the port accepted a connection, but the health // endpoint did not answer 200 for this instance. PortReachable PortState = "reachable" )
type State ¶ added in v1.261203.0
type State string
State is the outcome of a readiness wait.
const ( // StateReady means the health endpoint answered 200 for this instance. StateReady State = "ready" // StateFailed means the process ended before it became ready. StateFailed State = "failed" // StateStillStarting means the total timeout elapsed while the process was // still alive (it may yet become ready). StateStillStarting State = "still_starting" // StateCanceled means the caller's context ended first. StateCanceled State = "canceled" )
type TimeoutError ¶
type TimeoutError struct {
URL string
Timeout time.Duration
Port PortState
StatusCode int
LastErr error
OtherInstance bool
}
TimeoutError means the server never answered the health endpoint with 200 for this instance before the total timeout.
func (*TimeoutError) Error ¶
func (e *TimeoutError) Error() string
func (*TimeoutError) Unwrap ¶ added in v1.261203.0
func (e *TimeoutError) Unwrap() error