Documentation
¶
Overview ¶
Package lifecycle holds process-lifecycle state shared between a service's main function and its HTTP layer: the start-up gate (Starter) that holds a replica out of the pool until every module has started, and the graceful-drain signal (Drainer) that powers zero-downtime rolling deploys.
Ownership: main creates both and is the only caller of MarkReady (once start-up is complete) and StartDraining (from its shutdown path). Readers (e.g. a readiness handler) take them as the narrow ReadyChecker and DrainChecker interfaces, so a request handler never owns or mutates process-lifecycle state.
The package deliberately imports nothing beyond sync/atomic: a service that owns lifecycle state but runs no infra server can depend on it without pulling in an HTTP framework.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DrainChecker ¶
type DrainChecker interface {
// IsDraining reports whether the process has begun graceful shutdown.
IsDraining() bool
}
DrainChecker is the read-only view of drain state that request handlers depend on. Keeping handlers behind this interface means they can report "not ready" without knowing anything about how shutdown is orchestrated.
type Drainer ¶
type Drainer struct {
// contains filtered or unexported fields
}
Drainer is the single source of truth for whether this process is draining. The zero value is ready to use and safe for concurrent access.
func NewDrainer ¶
func NewDrainer() *Drainer
NewDrainer returns a Drainer in the not-draining state.
func (*Drainer) IsDraining ¶
IsDraining implements DrainChecker.
func (*Drainer) StartDraining ¶
func (d *Drainer) StartDraining()
StartDraining marks the process as draining. The transition is one-way: there is no un-drain, because draining only ever precedes process exit. Idempotent and safe for concurrent use.
type ReadyChecker ¶ added in v0.5.1
type ReadyChecker interface {
// IsReady reports whether main has declared start-up complete.
IsReady() bool
}
ReadyChecker is the read-only view of start-up state that request handlers depend on. It mirrors DrainChecker: a readiness handler can hold traffic back until start-up has finished without being able to declare it finished.
type Starter ¶ added in v0.5.1
type Starter struct {
// contains filtered or unexported fields
}
Starter is the single source of truth for whether this process has finished starting up. The zero value is ready to use, safe for concurrent access and NOT ready: a replica must opt in to traffic, never fall into it.
Why an explicit flag rather than deriving readiness from the probes: during start-up the probes either pass too early (a module that has no probe is still initializing, so the load balancer sends traffic to a half-started replica) or fail (500 plus an error log line on every health-check poll for the whole start-up). Neither tells the truth. Only main knows when every module has started, so only main can say so — and then readiness does not depend on the order in which modules initialize.
Ownership: main creates the Starter and is the only caller of MarkReady, once every module has started. Readers take it as a ReadyChecker.
It is a separate type from Drainer on purpose. Folding a "started" flag into Drainer would leave every existing Drainer user that never calls MarkReady reporting not-ready forever.
func NewStarter ¶ added in v0.5.1
func NewStarter() *Starter
NewStarter returns a Starter in the not-ready state.