Help us shape the future of Go! Take the 2026 Go Developer Survey

lifecycle

package
v1.21.2 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package lifecycle runs the process's services as one group: started together and stopped once, in the reverse order they were added, with the log output closed last so the lines written while stopping reach the log files. A service does its own shutdown work in its Stop.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotReady = errors.New("startup work failed")

ErrNotReady is the failure Fail records when it is given none.

Functions

This section is empty.

Types

type Group

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

Group A ServiceGroup is a group of services. Attention: the starting order of the added services is not guaranteed.

func NewServiceGroup

func NewServiceGroup() *Group

NewServiceGroup returns a ServiceGroup.

func (*Group) Add

func (sg *Group) Add(service Service)

Add adds service into sg.

func (*Group) Start

func (sg *Group) Start()

Start starts the services together. It blocks until every service's Start returned.

func (*Group) Stop

func (sg *Group) Stop()

Stop stops the services in the reverse order they were added, then closes the log output as the final step. The file output buffers entries, so the lines written while stopping — typically the shutdown errors — only reach the files once the output is closed. The output closes even when a service panics while stopping.

func (*Group) StopOrder added in v1.21.0

func (sg *Group) StopOrder() []Service

StopOrder returns the services in the order Stop stops them: the reverse of the order they were added in.

type Readiness added in v1.21.0

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

Readiness is a one-shot signal that a piece of startup work is done. The service doing the work calls Ready, or Fail with the reason; the services that depend on it wait on Done and read the outcome from Err. The group starts its services together, so a service that needs another's startup work waits on the signal inside its own Start. The first call decides: later ones change nothing, so a restart re-running the work cannot revoke an outcome the waiters already acted on. A nil Readiness has no waiters.

func NewReadiness added in v1.21.0

func NewReadiness() *Readiness

NewReadiness returns a signal that has not fired yet.

func (*Readiness) Done added in v1.21.0

func (r *Readiness) Done() <-chan struct{}

Done is closed once Ready or Fail was called.

func (*Readiness) Err added in v1.21.0

func (r *Readiness) Err() error

Err is nil while the work is running and after Ready, and the failure after Fail.

func (*Readiness) Fail added in v1.21.0

func (r *Readiness) Fail(err error)

Fail marks the work failed; the waiters read err from Err.

func (*Readiness) Ready added in v1.21.0

func (r *Readiness) Ready()

Ready marks the work done.

type Service

type Service interface {
	Starter
	Stopper
}

Service is the interface that groups Start and Stop methods.

func WithStart

func WithStart(start func()) Service

WithStart wraps a start func as a Service.

func WithStarter

func WithStarter(start Starter) Service

WithStarter wraps a Starter as a Service.

func WithStop added in v1.21.0

func WithStop(stop func()) Service

WithStop wraps a stop func as a Service whose Start returns at once: a shutdown step, such as flushing the trace exporter, that has its place in the stop order but nothing to run.

type Starter

type Starter interface {
	Start()
}

Starter is the interface wraps the Start method.

type Stopper

type Stopper interface {
	Stop()
}

Stopper is the interface wraps the Stop method.

Jump to

Keyboard shortcuts

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