Documentation
¶
Overview ¶
Package sup provides a small actor supervision toolkit for Go.
Actors own application behavior and mutable state. Supervisors own actor lifecycle, restart policy, and failure handling. Supervisors are actors too, so applications can build nested supervision trees from the same interface.
Getting started ¶
Define an Actor with an ID and a Run method, then add it to a Supervisor:
supervisor := sup.NewSupervisor("root", sup.Transient).AddActors(worker)
Run the root supervisor with the application's context. Context cancellation is a clean shutdown and should make Run return nil. Return a non-nil error only when the actor has failed and the configured supervisor policy should decide whether to restart it.
Each actor instance must belong to exactly one supervisor in a running tree. Complete all configuration before calling Run on the root supervisor. Supervisors invoke child Run methods and may invoke them again sequentially for restarts; the same actor instance must never run concurrently.
NewSupervisor requires a restart policy and defaults to a one-second restart delay with no restart limit. Configure optional restart behavior with SetRestartDelay, SetRestartDelayFunc, and SetRestartLimit before the first Run call.
Communication ¶
Use CastInbox[T] for fire-and-forget messages and CallInbox[T, R] for request/reply operations. Inboxes are unbuffered, so Cast and Call block until the actor receives the message or the context is canceled. Call handlers should reply at most once and should tolerate a caller that has already canceled its context.
Reactive state ¶
The optional rx package provides passive Signal values and supervised Derived values. A Signal can be read and subscribed to from any goroutine; a Derived value actively watches dependencies and must be added to a supervisor tree if it is expected to update.
Runtime debugging ¶
Actors implementing EventHandler observe lifecycle events. The optional hub package builds an HTTP debugging surface from explicitly registered actors and signals.
See the README for application-oriented examples and the package reference at https://pkg.go.dev/github.com/webermarci/sup for the complete API.
Index ¶
- type Actor
- type CallInbox
- type CallRequest
- type CastInbox
- type Event
- type EventHandler
- type EventType
- type RestartPolicy
- type Supervisor
- func (s *Supervisor) AddActors(actors ...Actor) *Supervisor
- func (s *Supervisor) ID() string
- func (s *Supervisor) Run(ctx context.Context) error
- func (s *Supervisor) SetRestartDelay(delay time.Duration) *Supervisor
- func (s *Supervisor) SetRestartDelayFunc(delay func(restartCount int) time.Duration) *Supervisor
- func (s *Supervisor) SetRestartLimit(maxRestarts int, window time.Duration) *Supervisor
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Actor ¶ added in v0.0.13
type Actor interface {
// ID returns the actor id.
ID() string
// Run starts the actor and blocks until it stops or fails. Context
// cancellation is a clean shutdown and must return nil.
Run(context.Context) error
}
Actor represents a concurrent entity that can be supervised.
Run must return nil when its context is canceled or the actor completes intentionally. It should return a non-nil error only for failures that may require a restart. Panics are recovered by supervisors and treated as failures.
Each actor instance must belong to exactly one supervisor in a running tree. Applications must finish configuring the tree before running only its root supervisor. Supervisors invoke child Run methods and may invoke them again sequentially when restarting actors. Run must never be called concurrently on the same actor instance.
type CallInbox ¶ added in v0.0.37
CallInbox delivers synchronous requests and replies to callers.
func NewCallInbox ¶ added in v0.0.37
NewCallInbox creates an unbuffered call inbox.
func (*CallInbox[T, R]) Call ¶ added in v0.0.37
Call sends a request and waits for a reply or context cancellation.
func (*CallInbox[T, R]) Receive ¶ added in v0.0.37
func (i *CallInbox[T, R]) Receive() <-chan CallRequest[T, R]
Receive returns the read-only request channel.
type CallRequest ¶ added in v0.0.8
CallRequest wraps a payload with a reply channel for synchronous calls.
func (CallRequest[T, R]) Context ¶ added in v0.0.54
func (r CallRequest[T, R]) Context() context.Context
Context returns the caller's context.
func (CallRequest[T, R]) Payload ¶ added in v0.0.8
func (r CallRequest[T, R]) Payload() T
Payload returns the request's payload.
func (CallRequest[T, R]) Reply ¶ added in v0.0.8
func (r CallRequest[T, R]) Reply(value R, err error) bool
Reply sends the response back to the caller. It returns false when the caller has canceled or a reply was already sent.
type CastInbox ¶ added in v0.0.37
type CastInbox[T any] struct { // contains filtered or unexported fields }
CastInbox delivers asynchronous messages to an actor.
func NewCastInbox ¶ added in v0.0.37
NewCastInbox creates an unbuffered cast inbox.
type Event ¶ added in v0.0.52
type Event struct {
Type EventType
Time time.Time
Actor Actor
Supervisor *Supervisor
Err error
RestartCount int
}
Event describes an actor lifecycle transition emitted by a supervisor.
type EventHandler ¶ added in v0.0.61
type EventHandler interface {
HandleEvent(Event)
}
EventHandler is an optional actor capability for handling runtime events emitted by its supervisor tree. Supervisors register the capability when the actor is added. HandleEvent may be called concurrently by different actors and must return promptly. A panic is recovered so that event handling cannot interrupt supervision.
type EventType ¶ added in v0.0.52
type EventType string
EventType identifies the kind of event that occurred.
const ( // EventActorRegistered is emitted when a supervisor begins supervising an // actor during a Run attempt. EventActorRegistered EventType = "actor:registered" // EventActorStarted is emitted when an actor starts running. EventActorStarted EventType = "actor:started" // EventActorStopped is emitted when an actor stops running. EventActorStopped EventType = "actor:stopped" // EventActorRestarting is emitted before a supervisor restarts an actor. EventActorRestarting EventType = "actor:restarting" )
type RestartPolicy ¶
type RestartPolicy uint8
RestartPolicy controls when a supervisor restarts a stopped actor.
const ( // Permanent always restarts actors, even after clean exits. Permanent RestartPolicy = iota // Transient restarts actors after errors or panics, but not after clean exits. Transient // Temporary never restarts actors. Temporary )
type Supervisor ¶
type Supervisor struct {
// contains filtered or unexported fields
}
Supervisor runs a fixed set of actors and restarts them according to its restart policy. A supervisor is itself an Actor and may be nested.
func NewSupervisor ¶ added in v0.0.7
func NewSupervisor(id string, policy RestartPolicy) *Supervisor
NewSupervisor creates a supervisor with the given id and restart policy. Optional configuration is added with methods before the first Run call.
func (*Supervisor) AddActors ¶ added in v0.0.61
func (s *Supervisor) AddActors(actors ...Actor) *Supervisor
AddActors adds actors that start with the supervisor and returns the supervisor for chaining. Actors that implement EventHandler observe runtime events from this supervisor and its descendants.
func (*Supervisor) ID ¶ added in v0.0.54
func (s *Supervisor) ID() string
ID returns the supervisor id.
func (*Supervisor) Run ¶ added in v0.0.14
func (s *Supervisor) Run(ctx context.Context) error
Run starts all configured actors and blocks until they have all stopped, a restart limit is exceeded, or ctx is canceled.
func (*Supervisor) SetRestartDelay ¶ added in v0.0.61
func (s *Supervisor) SetRestartDelay(delay time.Duration) *Supervisor
SetRestartDelay configures the exact delay between actor restart attempts and returns the supervisor for chaining.
func (*Supervisor) SetRestartDelayFunc ¶ added in v0.0.61
func (s *Supervisor) SetRestartDelayFunc( delay func(restartCount int) time.Duration, ) *Supervisor
SetRestartDelayFunc configures a function that calculates the delay before each actor restart. The restart count starts at one for the first restart. A non-positive result means that the actor should restart immediately. It may be called concurrently for different actors and must be safe for concurrent use. It returns the supervisor for chaining.
func (*Supervisor) SetRestartLimit ¶ added in v0.0.61
func (s *Supervisor) SetRestartLimit(maxRestarts int, window time.Duration) *Supervisor
SetRestartLimit limits restarts within a time window and returns the supervisor for chaining.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
dashboard
command
|
|
|
simple
command
|
|
|
Package httpserver adapts standard-library net/http servers to sup actors.
|
Package httpserver adapts standard-library net/http servers to sup actors. |
|
Package hub exposes selected sup actors and reactive signals over HTTP.
|
Package hub exposes selected sup actors and reactive signals over HTTP. |
|
mesh
module
|
|
|
modbus
module
|
|
|
Package process adapts standard-library os/exec commands to sup actors.
|
Package process adapts standard-library os/exec commands to sup actors. |
|
Package resource provides supervised, serialized access to acquired resources.
|
Package resource provides supervised, serialized access to acquired resources. |
|
Package rx provides shared reactive state and composable channel helpers.
|
Package rx provides shared reactive state and composable channel helpers. |
|
ws
module
|