Documentation
¶
Overview ¶
Package exitplan implements a simple mechanism for managing a lifetime of an application. It provides a way to register functions that will be called when the application is about to exit. It distinguishes between starting, running and teardown phases.
The application is considered to be starting before calling Exitplan.Run(). You can use Exitplan.StartingContext() to get a context that can be used to control the startup phase. Starting context is canceled when the startup phase is over.
The application is considered to be running after calling Exitplan.Run() and before calling Exitplan.Exit(). You can use Exitplan.Context() to get a context that can be used to control the running phase. It is canceled when the application is about to exit.
The application is considered to be tearing down after calling Exitplan.Exit(). You can use Exitplan.TeardownContext() to get a context that can be used to control the teardown phase. It is canceled when the teardown timeout is reached.
Ordering of shutdown callbacks Exitplan executes registered exit callbacks in LIFO order (last registered, first executed). Async only offloads the execution to a goroutine, but Exitplan still waits for all callbacks up to the teardown timeout.
Index ¶
- Variables
- func Async(c *callback)
- func PanicOnError(c *callback)
- func Timeout(timeout time.Duration) exitCallbackOpt
- func WithExitError(callback func(error)) opt
- func WithSignal(s1 os.Signal, sMany ...os.Signal) opt
- func WithStartupTimeout(timeout time.Duration) opt
- func WithTeardownTimeout(timeout time.Duration) opt
- type Exitplan
- func (l *Exitplan) Context() context.Context
- func (l *Exitplan) Exit(reason error)
- func (l *Exitplan) OnExit(callback func(), exitOpts ...exitCallbackOpt)
- func (l *Exitplan) OnExitWithContext(callback func(context.Context), exitOpts ...exitCallbackOpt)
- func (l *Exitplan) OnExitWithContextError(callback func(context.Context) error, exitOpts ...exitCallbackOpt)
- func (l *Exitplan) OnExitWithError(callback func() error, exitOpts ...exitCallbackOpt)
- func (l *Exitplan) Run() (exitCause error)
- func (l *Exitplan) StartingContext() context.Context
- func (l *Exitplan) TeardownContext() context.Context
Constants ¶
This section is empty.
Variables ¶
var ( // ErrSignaled indicates that the application received a termination signal ErrSignaled = errors.New("exitplan: received signal") // ErrGracefulShutdown indicates that the application was requested to shut down gracefully ErrGracefulShutdown = errors.New("exitplan: graceful shutdown requested") // ErrStartupTimeout indicates that the application failed to start within the configured timeout ErrStartupTimeout = errors.New("exitplan: startup phase timeout exceeded") )
Functions ¶
func Async ¶
func Async(c *callback)
Async sets the callback to be executed in a separate goroutine. Exitplan will wait for all Async callbacks to complete before exiting. Use Timeout to bound the callback.
func PanicOnError ¶
func PanicOnError(c *callback)
PanicOnError sets the callback to panic with the error returned by the callback.
func WithExitError ¶
func WithExitError(callback func(error)) opt
WithExitError sets the callback that will be called when an error occurs during the teardown phase. It will be called for every error that occurs during the teardown phase.
func WithSignal ¶
WithSignal calls Exitplan.Exit() when the specified signal is received.
func WithStartupTimeout ¶
WithStartupTimeout sets the timeout for the starting phase.
func WithTeardownTimeout ¶
WithTeardownTimeout sets the timeout for the teardown phase.
Types ¶
type Exitplan ¶
type Exitplan struct {
// contains filtered or unexported fields
}
func New ¶
func New(opts ...opt) *Exitplan
New creates a new instance of Exitplan. It will start in the starting phase. Use the With* options to configure it.
func (*Exitplan) Context ¶
Context returns a main context. IT will be canceled when the application is about to exit. It can be used to control the lifetime of the application. It will be canceled after calling Exitplan.Exit().
func (*Exitplan) Exit ¶
Exit stops the application. It will cause the application to exit with the specified reason. If the reason is nil, ErrGracefulShutdown will be used. Multiple calls to Exit are safe, but only the first one will have an effect.
func (*Exitplan) OnExit ¶
func (l *Exitplan) OnExit(callback func(), exitOpts ...exitCallbackOpt)
OnExit registers a callback that will be called when the application is about to exit. Has no effect after calling Exitplan.Run(). Use exitplan.Async option to execute the callback in a separate goroutine. exitplan.PanicOnError has no effect on this function. See also: OnExitWithError, OnExitWithContext, OnExitWithContextError.
func (*Exitplan) OnExitWithContext ¶
OnExitWithContext registers a callback that will be called when the application is about to exit. Has no effect after calling Exitplan.Run(). The callback will receive a context that will be canceled after the teardown timeout. Use exitplan.Async option to execute the callback in a separate goroutine. exitplan.PanicOnError has no effect on this function.
func (*Exitplan) OnExitWithContextError ¶
func (l *Exitplan) OnExitWithContextError(callback func(context.Context) error, exitOpts ...exitCallbackOpt)
OnExitWithContextError registers a callback that will be called when the application is about to exit. Has no effect after calling Exitplan.Run(). The callback will receive a context that will be canceled after the teardown timeout. The callback can return an error that will be passed to the error handler. Use exitplan.Async option to execute the callback in a separate goroutine. Use exitplan.PanicOnError to panic with the error returned by the callback.
func (*Exitplan) OnExitWithError ¶
OnExitWithError registers a callback that will be called when the application is about to exit. Has no effect after calling Exitplan.Run(). The callback can return an error that will be passed to the error handler. Use exitplan.Async option to execute the callback in a separate goroutine. Use exitplan.PanicOnError to panic with the error returned by the callback.
func (*Exitplan) Run ¶
Run starts the application. It will block until the application is stopped by calling Exit. It will also block until all the registered callbacks are executed. If the teardown timeout is set, it will be used to cancel the context passed to the callbacks. Returns the error that caused the application to stop.
func (*Exitplan) StartingContext ¶
StartingContext returns a context for a starting phase. It can be used to control the startup of the application. StartingContext will be canceled after the starting timeout or when Exitplan.Run() is called.
func (*Exitplan) TeardownContext ¶
TeardownContext returns a teardown context. It will be canceled after the teardown timeout. It can be used to control the shutdown of the application. This context is the same as the one passed to the callbacks registered with OnExit* methods.