Documentation
¶
Index ¶
- Variables
- type Actor
- type BaseActor
- type CallInbox
- type CallRequest
- type CastInbox
- type CastRequest
- type Outbox
- type RepliableRequest
- type RestartPolicy
- type Supervisor
- type SupervisorObserver
- type SupervisorOption
- func WithActor(actor Actor) SupervisorOption
- func WithActors(actors ...Actor) SupervisorOption
- func WithLogger(logger *slog.Logger) SupervisorOption
- func WithObserver(observer *SupervisorObserver) SupervisorOption
- func WithOnError(handler func(actor Actor, err error)) SupervisorOption
- func WithPolicy(policy RestartPolicy) SupervisorOption
- func WithRestartDelay(d time.Duration) SupervisorOption
- func WithRestartLimit(maxRestarts int, window time.Duration) SupervisorOption
Constants ¶
This section is empty.
Variables ¶
var ( ErrCallInboxFull = errors.New("sup: call inbox is full") ErrCallInboxClosed = errors.New("sup: call inbox is closed") )
var ( ErrCastInboxFull = errors.New("sup: cast inbox is full") ErrCastInboxClosed = errors.New("sup: cast inbox is closed") )
Functions ¶
This section is empty.
Types ¶
type Actor ¶ added in v0.0.13
type Actor interface {
Name() string
Run(context.Context) error
// contains filtered or unexported methods
}
Actor represents a concurrent entity that can be supervised. It has a name and a Run method that executes its logic. The Run method should return an error if the actor needs to be restarted, or nil if it can exit cleanly. Panics will also trigger a restart. The setLogger method is used internally by the supervisor to inject a logger into the actor.
type BaseActor ¶ added in v0.0.20
type BaseActor struct {
// contains filtered or unexported fields
}
BaseActor provides a simple implementation of the Actor interface with a name and a logger. It can be embedded in other structs to create more complex actors. The Name and Logger methods are safe to call from inside Run(), and the setLogger method is used internally by the supervisor to inject a logger into the actor.
func NewBaseActor ¶ added in v0.0.20
NewBaseActor creates a new BaseActor with the given name. The logger is initialized to a no-op logger and will be set by the supervisor when the actor is spawned.
type CallInbox ¶ added in v0.0.37
CallInbox manages request-response communication.
func NewCallInbox ¶ added in v0.0.37
NewCastInbox creates a new inbox for a specific message type.
func (*CallInbox[T, R]) Close ¶ added in v0.0.37
func (i *CallInbox[T, R]) Close()
Close safely shuts down the inbox.
func (*CallInbox[T, R]) Len ¶ added in v0.0.37
Len returns the number of messages currently in the inbox.
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 channel for the actor's internal loop.
type CallRequest ¶ added in v0.0.8
CallRequest wraps a payload with a reply channel for synchronous calls.
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)
Reply sends the response back to the caller. The actor should call this exactly once per request.
type CastInbox ¶ added in v0.0.37
type CastInbox[T any] struct { // contains filtered or unexported fields }
CastInbox is a type-safe, write-only entry point for fire-and-forget messages.
func NewCastInbox ¶ added in v0.0.37
NewCastInbox creates a new inbox for a specific message type.
func (*CastInbox[T]) Cast ¶ added in v0.0.37
Cast pushes a message into the inbox with context for cancellation. It blocks if the inbox is full, but returns ctx.Err() if the context expires before the message is enqueued.
func (*CastInbox[T]) Close ¶ added in v0.0.37
func (i *CastInbox[T]) Close()
Close safely shuts down the inbox.
func (*CastInbox[T]) Len ¶ added in v0.0.37
Len returns the number of messages currently in the inbox.
type CastRequest ¶ added in v0.0.8
type CastRequest[T any] struct { // contains filtered or unexported fields }
CastRequest wraps a payload for asynchronous calls without expecting a reply.
func (CastRequest[T]) Payload ¶ added in v0.0.8
func (r CastRequest[T]) Payload() T
Payload returns the request's payload.
type Outbox ¶ added in v0.0.37
type Outbox[T any] struct { // contains filtered or unexported fields }
Outbox provides a type-safe asynchronous broadcast mechanism. T is the message type emitted to all subscribers.
func (*Outbox[T]) Emit ¶ added in v0.0.37
Emit sends a message to all registered handlers. It is the caller's responsibility to ensure that handlers do not block indefinitely, as this will affect the responsiveness of the system. Handlers should ideally use non-blocking calls or manage their own goroutines if they need to perform longer work.
type RepliableRequest ¶ added in v0.0.9
RepliableRequest represents a request that can be replied to.
type RestartPolicy ¶
type RestartPolicy uint8
const ( Permanent RestartPolicy = iota // Always restart, even on clean exits Transient // Restart on errors/panics, but not on clean exits (nil) Temporary // Never restart )
type Supervisor ¶
type Supervisor struct {
*BaseActor
// contains filtered or unexported fields
}
Supervisor manages the lifecycle of actor Run loops.
func NewSupervisor ¶ added in v0.0.7
func NewSupervisor(name string, opts ...SupervisorOption) *Supervisor
NewSupervisor creates a new Supervisor with the given options. Panics if the provided options are invalid.
func (*Supervisor) Run ¶ added in v0.0.14
func (s *Supervisor) Run(ctx context.Context) error
Run starts all actors under supervision and blocks until the context is canceled or all actors have stopped.
func (*Supervisor) Spawn ¶ added in v0.0.14
func (s *Supervisor) Spawn(ctx context.Context, actor Actor)
Spawn starts the given actor under supervision. It will be restarted according to the supervisor's policy if it returns an error or panics.
func (*Supervisor) Wait ¶
func (s *Supervisor) Wait()
Wait blocks until all supervised actors have stopped.
type SupervisorObserver ¶ added in v0.0.28
type SupervisorObserver struct {
OnActorRegistered func(actor Actor)
OnActorStarted func(actor Actor)
OnActorStopped func(actor Actor, err error)
OnActorRestarting func(actor Actor, restartCount int, lastErr error)
OnSupervisorTerminal func(err error)
}
SupervisorObserver allows observing lifecycle events of supervised actors and the supervisor itself. This can be used for logging, monitoring, or triggering side effects based on actor behavior.
type SupervisorOption ¶ added in v0.0.7
type SupervisorOption func(*Supervisor)
SupervisorOption configures a Supervisor.
func WithActor ¶ added in v0.0.14
func WithActor(actor Actor) SupervisorOption
WithActor adds an actor to be supervised. Can be called multiple times to add multiple actors.
func WithActors ¶ added in v0.0.14
func WithActors(actors ...Actor) SupervisorOption
WithActors adds multiple actors to be supervised.
func WithLogger ¶ added in v0.0.33
func WithLogger(logger *slog.Logger) SupervisorOption
WithLogger sets a logger for the supervisor.
func WithObserver ¶ added in v0.0.28
func WithObserver(observer *SupervisorObserver) SupervisorOption
WithObserver sets a SupervisorObserver to receive lifecycle event notifications for supervised actors and the supervisor itself. This allows external monitoring of actor behavior and supervisor actions.
func WithOnError ¶ added in v0.0.7
func WithOnError(handler func(actor Actor, err error)) SupervisorOption
WithOnError sets a callback function that will be called whenever a supervised actor returns an error or panics. The callback receives the actor and the error as arguments.
func WithPolicy ¶ added in v0.0.7
func WithPolicy(policy RestartPolicy) SupervisorOption
WithPolicy sets the restart policy.
func WithRestartDelay ¶ added in v0.0.7
func WithRestartDelay(d time.Duration) SupervisorOption
WithRestartDelay sets the delay between restarts.
func WithRestartLimit ¶ added in v0.0.7
func WithRestartLimit(maxRestarts int, window time.Duration) SupervisorOption
WithRestartLimit sets the maximum number of restarts allowed within a window. Both maxRestarts and window must be positive; otherwise NewSupervisor panics.