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.WithPolicy(sup.Transient),
sup.WithActors(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.
NewSupervisor defaults to the Transient policy, a one-second restart delay, and no restart limit. Configure a fixed delay with WithRestartDelay or calculate one for each restart with WithRestartDelayFunc. A restart limit can be added with WithRestartLimit(maxRestarts, window).
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 inspection ¶
Inspectable actors can expose a Spec, and EventSink values can observe lifecycle events. The optional hub package builds an HTTP inspection 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 ¶
- Variables
- type Actor
- type ActorFuncOption
- type CallInbox
- type CallRequest
- type CastInbox
- type Event
- type EventSink
- type EventSinkFunc
- type EventType
- type Inspectable
- type RestartDelayFunc
- type RestartPolicy
- type Spec
- type Supervisor
- type SupervisorOption
- func WithActors(actors ...Actor) SupervisorOption
- func WithEventSink(sinks ...EventSink) SupervisorOption
- func WithPolicy(policy RestartPolicy) SupervisorOption
- func WithRestartDelay(delay time.Duration) SupervisorOption
- func WithRestartDelayFunc(delay RestartDelayFunc) SupervisorOption
- func WithRestartLimit(maxRestarts int, window time.Duration) SupervisorOption
Constants ¶
This section is empty.
Variables ¶
var ErrSupervisorRunning = errors.New("sup: supervisor is already running")
ErrSupervisorRunning is returned when Run is called while a supervisor is already running.
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.
type ActorFuncOption ¶ added in v0.0.54
type ActorFuncOption func(*actorFunc)
ActorFuncOption configures an actor created by ActorFunc.
func WithSpec ¶ added in v0.0.54
func WithSpec(spec Spec) ActorFuncOption
WithSpec configures an actor function's structured inspection spec.
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.
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, the request is invalid, 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 EventSink ¶ added in v0.0.54
type EventSink interface {
HandleEvent(Event)
}
EventSink consumes runtime events emitted by a supervisor tree. HandleEvent may be called concurrently by different actors and must return promptly. A panic is recovered so that event handling cannot interrupt supervision.
type EventSinkFunc ¶ added in v0.0.54
type EventSinkFunc func(Event)
EventSinkFunc adapts a function into an EventSink.
func (EventSinkFunc) HandleEvent ¶ added in v0.0.54
func (f EventSinkFunc) HandleEvent(event Event)
HandleEvent calls f with event.
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 Inspectable ¶ added in v0.0.43
type Inspectable interface {
Inspect() Spec
}
Inspectable is implemented by actors that expose structured metadata. Inspection is optional and does not affect actor execution or supervision.
type RestartDelayFunc ¶ added in v0.0.58
RestartDelayFunc calculates the delay before an actor restart. The restart count starts at one for the first restart. A non-positive duration means no delay.
A supervisor may call the function concurrently for different actors, so it must be safe for concurrent use.
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 Spec ¶ added in v0.0.43
type Spec struct {
Kind string `json:"kind"`
Dependencies []string `json:"dependencies"`
Metadata map[string]any `json:"metadata"`
}
Spec represents a structured description of an inspectable component. It captures the component's kind, its dependency names, and arbitrary configuration metadata for visualization and debugging purposes.
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, options ...SupervisorOption) *Supervisor
NewSupervisor creates a supervisor with the given id and options.
func (*Supervisor) ID ¶ added in v0.0.54
func (s *Supervisor) ID() string
ID returns the supervisor id.
func (*Supervisor) Inspect ¶ added in v0.0.43
func (s *Supervisor) Inspect() Spec
Inspect returns the supervisor spec.
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. A supervisor may be run again after Run returns. Concurrent calls return ErrSupervisorRunning.
type SupervisorOption ¶ added in v0.0.7
type SupervisorOption func(*Supervisor)
SupervisorOption configures a Supervisor during construction.
func WithActors ¶ added in v0.0.14
func WithActors(actors ...Actor) SupervisorOption
WithActors adds actors that start with the supervisor.
func WithEventSink ¶ added in v0.0.54
func WithEventSink(sinks ...EventSink) SupervisorOption
WithEventSink adds an event sink for runtime events from this supervisor and its descendants.
func WithPolicy ¶ added in v0.0.7
func WithPolicy(policy RestartPolicy) SupervisorOption
WithPolicy configures the supervisor restart policy.
func WithRestartDelay ¶ added in v0.0.7
func WithRestartDelay(delay time.Duration) SupervisorOption
WithRestartDelay configures the exact delay between actor restart attempts.
func WithRestartDelayFunc ¶ added in v0.0.58
func WithRestartDelayFunc(delay RestartDelayFunc) SupervisorOption
WithRestartDelayFunc 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.
func WithRestartLimit ¶ added in v0.0.7
func WithRestartLimit(maxRestarts int, window time.Duration) SupervisorOption
WithRestartLimit limits restarts within a time window.
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
|