Documentation
¶
Index ¶
- Variables
- func Call[T any, R any](mb *Mailbox, payload T) (R, error)
- func CallContext[T any, R any](ctx context.Context, mb *Mailbox, payload T) (R, error)
- func Cast[T any](mb *Mailbox, payload T) error
- func CastContext[T any](ctx context.Context, mb *Mailbox, payload T) error
- func TryCall[T any, R any](mb *Mailbox, payload T) (R, error)
- func TryCallContext[T any, R any](ctx context.Context, mb *Mailbox, payload T) (R, error)
- func TryCast[T any](mb *Mailbox, payload T) error
- func TryCastContext[T any](ctx context.Context, mb *Mailbox, payload T) error
- type Actor
- type BaseActor
- type CallRequest
- type CastRequest
- type Mailbox
- 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 ( // ErrMailboxFull is returned by TryCast when the mailbox buffer is full. ErrMailboxFull = errors.New("mailbox is full") // ErrMailboxClosed is returned when trying to send to a closed mailbox. ErrMailboxClosed = errors.New("mailbox is closed") )
Functions ¶
func CallContext ¶ added in v0.0.2
CallContext sends a message to an actor and waits for a reply until the context expires.
func Cast ¶ added in v0.0.8
Cast sends an asynchronous typed envelope, waiting until it can be enqueued or the mailbox is closed. It returns ErrMailboxClosed if the mailbox is closed.
func CastContext ¶ added in v0.0.8
CastContext sends an asynchronous typed envelope with context for enqueue cancellation. It returns ErrMailboxClosed if the mailbox is closed, or ctx.Err() if the context expires before the message is enqueued.
func TryCallContext ¶ added in v0.0.6
TryCallContext attempts to enqueue a request without blocking and waits for reply until ctx expires.
func TryCast ¶ added in v0.0.8
TryCast attempts to send an envelope without blocking. It returns ErrMailboxClosed if the mailbox is closed, or ErrMailboxFull immediately if the mailbox buffer is full.
func TryCastContext ¶ added in v0.0.8
TryCastContext attempts to send an envelope without blocking, but returns ctx.Err() if ctx is done. It returns ErrMailboxClosed if the mailbox is closed, or ErrMailboxFull immediately if the mailbox buffer is full.
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 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 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 Mailbox ¶
type Mailbox struct {
// contains filtered or unexported fields
}
Mailbox is a thread-safe message queue for actors.
func NewMailbox ¶
NewMailbox creates a new mailbox with the specified buffer size. A size of 0 means unbuffered.
func (*Mailbox) Close ¶
func (m *Mailbox) Close()
Close safely closes the mailbox. Subsequent sends fail.
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.