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 Registry
- type RepliableRequest
- type RestartPolicy
- type Supervisor
- type SupervisorOption
- func WithActor(actor Actor) SupervisorOption
- func WithActors(actors ...Actor) 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 BaseActor ¶ added in v0.0.20
type BaseActor struct {
// contains filtered or unexported fields
}
func NewBaseActor ¶ added in v0.0.20
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 Registry ¶ added in v0.0.23
type Registry struct {
// contains filtered or unexported fields
}
func GlobalRegistry ¶ added in v0.0.23
func GlobalRegistry() *Registry
GlobalRegistry returns the default process registry.
func NewRegistry ¶ added in v0.0.23
func NewRegistry() *Registry
func (*Registry) Register ¶ added in v0.0.23
Register adds an actor to the registry. If an actor with the same name already exists, it will be overwritten.
func (*Registry) Unregister ¶ added in v0.0.23
Unregister removes an actor from the registry.
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 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 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.