Documentation
¶
Overview ¶
Package supervisor runs named background workers under a restart/backoff policy and surfaces fatal worker errors through a pluggable error handler instead of letting an unrecovered failure take down the app.
Index ¶
- Constants
- type Option
- type RestartPolicy
- type Supervisor
- func (s *Supervisor) Context() context.Context
- func (s *Supervisor) SetErrorHandler(handler func(name string, err error))
- func (s *Supervisor) Start(name string, fn func(context.Context) error, opts ...Option)
- func (s *Supervisor) Stop()
- func (s *Supervisor) StopWithTimeout(timeout time.Duration) bool
Constants ¶
const StopTimeout = 5 * time.Second
StopTimeout bounds how long Stop waits for workers to exit. A worker that ignores context cancellation must not be able to wedge app shutdown.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option func(*options)
Option configures supervisor worker behavior.
func WithBackoff ¶
WithBackoff sets the initial backoff between restarts.
func WithMaxBackoff ¶
WithMaxBackoff caps the backoff between restarts.
func WithMaxRestarts ¶
WithMaxRestarts limits the number of restarts (0 = unlimited).
func WithRestartPolicy ¶
func WithRestartPolicy(policy RestartPolicy) Option
WithRestartPolicy sets the restart policy.
type RestartPolicy ¶
type RestartPolicy int
RestartPolicy controls when a worker should be restarted.
const ( RestartNever RestartPolicy = iota RestartOnError RestartAlways )
type Supervisor ¶
type Supervisor struct {
// contains filtered or unexported fields
}
Supervisor manages worker lifecycles with restart policies.
func New ¶
func New(parent context.Context) *Supervisor
New creates a supervisor bound to the parent context.
func (*Supervisor) Context ¶
func (s *Supervisor) Context() context.Context
Context returns the supervisor context.
func (*Supervisor) SetErrorHandler ¶
func (s *Supervisor) SetErrorHandler(handler func(name string, err error))
SetErrorHandler registers a handler for worker errors.
func (*Supervisor) Stop ¶
func (s *Supervisor) Stop()
Stop cancels all workers and waits up to StopTimeout for them to exit, logging which workers failed to stop in time.
func (*Supervisor) StopWithTimeout ¶ added in v0.0.20
func (s *Supervisor) StopWithTimeout(timeout time.Duration) bool
StopWithTimeout cancels all workers and waits up to timeout for them to exit. On timeout it logs the workers still running and returns false.