Documentation
¶
Index ¶
- Variables
- type Actor
- type BaseActor
- type BaseSignal
- func (b *BaseSignal[V]) Read() V
- func (b *BaseSignal[V]) SetBroadcasterBuffer(buffer int)
- func (b *BaseSignal[V]) SetEqual(eq func(a, b V) bool)
- func (b *BaseSignal[V]) SetInitialNotify(enabled bool)
- func (b *BaseSignal[V]) SetInitialValue(v V)
- func (b *BaseSignal[V]) Subscribe(ctx context.Context) <-chan V
- func (b *BaseSignal[V]) Watch(ctx context.Context) <-chan struct{}
- type CallInbox
- type CallRequest
- type CastInbox
- type CastRequest
- type ComputedSignal
- type DebouncedSignal
- type Inspectable
- type Outbox
- type PeriodicSignal
- type PushedSignal
- type ReadableSignal
- type ReaderSignal
- type RepliableRequest
- type RestartPolicy
- type Router
- type RouterStrategy
- type Signal
- type Spec
- type SubscriberSignal
- 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
- type ThrottledSignal
- type WatcherSignal
- type WritableSignal
- type WriterSignal
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 BaseSignal ¶ added in v0.0.40
BaseSignal centralizes broadcasting/subscription behavior for signals. It intentionally does NOT manage the value or its locking; concrete signals keep their own value + mutex and call the base's notify/subscribe helpers.
func NewBaseSignal ¶ added in v0.0.40
func NewBaseSignal[V any](name string) *BaseSignal[V]
NewBaseSignal creates a BaseSignal with sane defaults.
func (*BaseSignal[V]) Read ¶ added in v0.0.40
func (b *BaseSignal[V]) Read() V
Read returns the current stored value (thread-safe).
func (*BaseSignal[V]) SetBroadcasterBuffer ¶ added in v0.0.40
func (b *BaseSignal[V]) SetBroadcasterBuffer(buffer int)
SetBroadcasterBuffer configures the internal broadcaster buffer size. It acquires a lock to ensure thread-safe access to the broadcaster's buffer configuration.
func (*BaseSignal[V]) SetEqual ¶ added in v0.0.40
func (b *BaseSignal[V]) SetEqual(eq func(a, b V) bool)
SetEqual configures the equality function used to determine if a new value is different from the current value before notifying subscribers. This can help prevent unnecessary notifications when the value hasn't actually changed. It acquires a lock to ensure thread-safe access to the equal function.
func (*BaseSignal[V]) SetInitialNotify ¶ added in v0.0.40
func (b *BaseSignal[V]) SetInitialNotify(enabled bool)
SetInitialNotify configures whether to notify subscribers immediately with the current value upon subscription. It acquires a lock to ensure thread-safe access to the initialNotify flag.
func (*BaseSignal[V]) SetInitialValue ¶ added in v0.0.40
func (b *BaseSignal[V]) SetInitialValue(v V)
SetInitialValue sets the initial value of the Signal before any updates occur. It acquires a lock to ensure thread-safe access to the value.
func (*BaseSignal[V]) Subscribe ¶ added in v0.0.40
func (b *BaseSignal[V]) Subscribe(ctx context.Context) <-chan V
Subscribe returns a channel that receives updates whenever the Signal's value changes. If initialNotify is enabled, the current value is sent to the channel immediately upon subscription. It acquires a read lock to ensure thread-safe access to the current value when subscribing.
func (*BaseSignal[V]) Watch ¶ added in v0.0.40
func (b *BaseSignal[V]) Watch(ctx context.Context) <-chan struct{}
Watch allows clients to subscribe to notifications whenever the Signal's value is updated, without receiving the value itself. If initialNotify is enabled, a notification is sent to the channel immediately upon subscription.
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 ComputedSignal ¶ added in v0.0.40
type ComputedSignal[V any] struct { *BaseSignal[V] // contains filtered or unexported fields }
ComputedSignal is a reactive value that updates itself based on its dependencies. It implements both Subscribable and Notifyable interfaces.
func NewComputedSignal ¶ added in v0.0.40
func NewComputedSignal[V any](name string, update func() V, deps ...WatcherSignal) *ComputedSignal[V]
NewComputedSignal creates a new ComputedSignal with the given name, update function, and dependencies. The update function is called whenever any of the dependencies notify a change, and the result is broadcast to subscribers.
func (*ComputedSignal[V]) Inspect ¶ added in v0.0.43
func (s *ComputedSignal[V]) Inspect() Spec
Inspect returns the specification.
func (*ComputedSignal[V]) Run ¶ added in v0.0.40
func (s *ComputedSignal[V]) Run(ctx context.Context) error
Run is the main loop for the Computed actor. It subscribes to all dependencies and listens for notifications. Whenever any dependency notifies a change, it calls the update function to compute the new value, updates its internal state, and broadcasts the new value to subscribers. The loop continues until the context is canceled, at which point it cleans up and exits.
func (*ComputedSignal[V]) SetCoalesceWindow ¶ added in v0.0.40
func (s *ComputedSignal[V]) SetCoalesceWindow(window time.Duration)
SetCoalesceWindow configures the duration to wait after receiving a notification from any dependency before triggering an update. This allows for coalescing multiple rapid updates into a single update, improving efficiency. It acquires a lock to ensure thread-safe access to the coalesceWindow configuration.
type DebouncedSignal ¶ added in v0.0.40
type DebouncedSignal[V any] struct { *BaseSignal[V] // contains filtered or unexported fields }
DebouncedSignal is a reactive value that delays broadcasting updates from its source until the source has stopped changing for a specified wait duration.
func NewDebouncedSignal ¶ added in v0.0.40
func NewDebouncedSignal[V any](name string, src ReadableSignal[V], wait time.Duration) *DebouncedSignal[V]
NewDebouncedSignal creates a new DebouncedSignal actor attached to a source provider.
func (*DebouncedSignal[V]) Inspect ¶ added in v0.0.43
func (s *DebouncedSignal[V]) Inspect() Spec
Inspect returns the specification.
func (*DebouncedSignal[V]) Run ¶ added in v0.0.40
func (s *DebouncedSignal[V]) Run(ctx context.Context) error
Run is the main actor loop. It subscribes to the source and manages the sliding window.
func (*DebouncedSignal[V]) SetMaxWait ¶ added in v0.0.40
func (s *DebouncedSignal[V]) SetMaxWait(maxWait time.Duration)
SetWait configures the duration to wait after receiving an update from the source before broadcasting the new value. This allows for coalescing multiple rapid updates into a single update, improving efficiency. It acquires a lock to ensure thread-safe access to the wait configuration.
type Inspectable ¶ added in v0.0.43
type Inspectable interface {
Inspect() Spec
}
Inspectable is an interface for components that can provide a structured specification describing their type, dependencies, and configuration. Implement this interface to enable introspection, visualization, or debugging of actor system components.
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 PeriodicSignal ¶ added in v0.0.42
type PeriodicSignal[V any] struct { *BaseSignal[V] // contains filtered or unexported fields }
PeriodicSignal represents a value that is periodically updated by a function and can be subscribed to for updates.
func NewPeriodicSignal ¶ added in v0.0.42
func NewPeriodicSignal[V any](name string, update func(context.Context) (V, error), interval time.Duration) *PeriodicSignal[V]
NewPeriodicSignal creates a new PeriodicSignal with the given name and update function.
func (*PeriodicSignal[V]) Inspect ¶ added in v0.0.43
func (s *PeriodicSignal[V]) Inspect() Spec
Inspect returns the specification.
func (*PeriodicSignal[V]) Run ¶ added in v0.0.42
func (s *PeriodicSignal[V]) Run(ctx context.Context) error
Run starts the Signal's update loop, which periodically calls the update function to refresh the Signal's value and notifies subscribers of any changes. The loop continues until the provided context is canceled, at which point it will clean up all subscriber channels.
func (*PeriodicSignal[V]) Trigger ¶ added in v0.0.42
func (s *PeriodicSignal[V]) Trigger()
Trigger requests an immediate refresh of the signal value.
type PushedSignal ¶ added in v0.0.40
type PushedSignal[V any] struct { *BaseSignal[V] // contains filtered or unexported fields }
PushedSignal represents a value that can be updated by a function and subscribed to for updates.
func NewPushedSignal ¶ added in v0.0.40
NewPushedSignal creates a new PushedSignal with the given name and update function.
func (*PushedSignal[V]) Inspect ¶ added in v0.0.43
func (s *PushedSignal[V]) Inspect() Spec
Inspect returns the specification.
func (*PushedSignal[V]) Run ¶ added in v0.0.40
func (s *PushedSignal[V]) Run(ctx context.Context) error
Run starts the PushedSignal's main loop, which waits for the context to be canceled. When the context is canceled, it cleans up all subscriber channels. This method should be run in a separate goroutine.
func (*PushedSignal[V]) Write ¶ added in v0.0.40
func (s *PushedSignal[V]) Write(ctx context.Context, value V) error
Write updates the PushedSignal's value by calling the update function with the provided value. If the update is successful, it notifies all subscribers of the new value. It acquires a lock to ensure thread-safe updates to the value.
type ReadableSignal ¶ added in v0.0.40
type ReadableSignal[V any] interface { ReaderSignal[V] SubscriberSignal[V] WatcherSignal }
ReadableSignal represents a value that can be read and subscribed to for updates. It also supports watching for changes. ReadableSignal is a sup.Actor, so it can be run as a goroutine and can be stopped by canceling its context.
type ReaderSignal ¶ added in v0.0.40
ReaderSignal represents a value that can be read. The Read method returns the current value.
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 Router ¶ added in v0.0.39
type Router[F any] struct { // contains filtered or unexported fields }
Router is a generic type that manages a set of routees and provides methods to select one based on the configured strategy. It also supports broadcasting messages to all routees and retrying operations with a limit.
func NewRouter ¶ added in v0.0.39
func NewRouter[F any](strategy RouterStrategy, routees ...F) *Router[F]
NewRouter creates a new Router with the specified routing strategy and routees. It panics if no routees are provided or if an unknown strategy is specified.
func (*Router[F]) Broadcast ¶ added in v0.0.39
func (r *Router[F]) Broadcast(fn func(F))
Broadcast applies the provided function to all routees sequentially. This is useful for sending the same message or performing the same action on all routees.
func (*Router[F]) FanOut ¶ added in v0.0.39
func (r *Router[F]) FanOut(fn func(F))
FanOut applies the provided function to all routees concurrently. This is useful for performing actions on all routees in parallel, but it does not wait for the operations to complete.
func (*Router[F]) FanOutWait ¶ added in v0.0.39
func (r *Router[F]) FanOutWait(fn func(F))
FanOutWait applies the provided function to all routees concurrently and waits for all operations to complete before returning. This is useful when you need to ensure that all routees have processed the message or action before proceeding.
func (*Router[F]) Next ¶ added in v0.0.39
func (r *Router[F]) Next() F
Next returns the next routee based on the configured routing strategy.
func (*Router[F]) Retry ¶ added in v0.0.39
Retry attempts to execute the provided function with a routee up to the specified limit. If the function returns an error, it will retry with the next routee until the limit is reached. It returns the last error encountered if all attempts fail.
type RouterStrategy ¶ added in v0.0.39
type RouterStrategy uint8
Router provides various strategies for routing messages to a set of routees (e.g., actors, workers, etc.). It supports round-robin and random routing, as well as sticky routing based on a key. Additionally, it offers methods for broadcasting messages to all routees and retrying operations with a specified limit.
const ( RoundRobin RouterStrategy = iota Random )
type Signal ¶ added in v0.0.43
type Signal interface {
Actor
Inspectable
}
Signal represents a stream of values that can be observed, transformed, debounced, or combined within the actor system.
Signals implement the Actor interface and serve as a foundational primitive for reactive data flows, enabling declarative composition of asynchronous value streams across distributed actors.
type Spec ¶ added in v0.0.43
type Spec struct {
Kind string `json:"kind"`
Dependencies []string `json:"dependencies"`
Metadata map[string]string `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 SubscriberSignal ¶ added in v0.0.40
SubscriberSignal represents a value that can be subscribed to for updates. The Subscribe method returns a channel that will receive the updated value whenever it changes. The channel will be closed when the context is canceled.
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) Children ¶ added in v0.0.43
func (s *Supervisor) Children() []Actor
func (*Supervisor) Inspect ¶ added in v0.0.43
func (s *Supervisor) Inspect() Spec
Inspect returns the specification.
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.
type ThrottledSignal ¶ added in v0.0.40
type ThrottledSignal[V any] struct { *BaseSignal[V] // contains filtered or unexported fields }
ThrottledSignal is a reactive value that limits the rate at which updates from its source are broadcast. It ensures that updates are sent at most once per interval, always emitting the most recent (trailing) value from that interval.
func NewThrottledSignal ¶ added in v0.0.40
func NewThrottledSignal[V any](name string, src ReadableSignal[V], interval time.Duration) *ThrottledSignal[V]
NewThrottledSignal creates a new ThrottledSignal actor attached to a source provider.
func (*ThrottledSignal[V]) Inspect ¶ added in v0.0.43
func (s *ThrottledSignal[V]) Inspect() Spec
Inspect returns the specification.
type WatcherSignal ¶ added in v0.0.40
WatcherSignal represents a value that can be watched for changes. The Watch method returns a channel that will receive a notification whenever the value changes. The channel will be closed when the context is canceled.
type WritableSignal ¶ added in v0.0.40
type WritableSignal[V any] interface { ReadableSignal[V] WriterSignal[V] }
WritableSignal represents a value that can be read, updated, and subscribed to for updates. It also supports watching for changes. WritableSignal is a sup.Actor, so it can be run as a goroutine and can be stopped by canceling its context.