Purpose
The framework publishes lifecycle and cluster events so that external components (monitoring, logging, dashboards) can observe the actor system without coupling to internal implementation. Subscribers receive events as they occur: actor started, actor stopped, node joined, dead letter, and so on.API
- ActorSystem.Subscribe: Create a subscriber. Subscribes to the internal events topic. Returns
eventstream.Subscriber. - ActorSystem.Unsubscribe: Release the subscriber. Call to avoid leaks.
eventstream.Subscriber interface:
- ID: Unique subscriber identifier.
- Topics: Topics this subscriber is subscribed to (includes the internal topic).
- Iterator: Channel of buffered messages; drains and closes. Messages are
*eventstream.Message. - Shutdown: Mark subscriber inactive.
Message structure
Each message has:- Topic:
msg.Topic()returns the topic (e.g.topic.events, the internal topic). - Payload:
msg.Payload()returns the event. Use a type assertion to handle specific events.
Event types
The system publishes these events to the event stream:- ActorStarted: Actor has started.
- ActorStopped: Actor has stopped.
- ActorChildCreated: A child actor was spawned.
- ActorPassivated: Actor was passivated (idle timeout).
- ActorRestarted: Actor was restarted by supervisor.
- ActorSuspended: Actor was suspended after a failure.
- ActorReinstated: Actor was reinstated by supervisor.
- Deadletter: Message was not handled by its receiver (
ctx.Unhandled) or could not be delivered (for example aPipeTotask that failed), or a grain failed to handle a one-way message. For a grain the receiver is named by the grain identity,kind/name. A send to a stopped actor returnsErrDeadto the sender and produces no dead letter. - NodeJoined: A node joined the cluster (cluster mode only).
- NodeLeft: A node left the cluster (cluster mode only).
- LeaderChanged: The cluster coordinator moved to a different node (cluster mode only).
- RelocationFailed: A departed node’s actors or grains could not be relocated (cluster mode only).
- RelocationStarted: The leader started relocating a departed node’s actors and grains;
BestEffortis true when the set was reconstructed after a crash (cluster mode only).
Usage
Usage
Iterator() returns a channel that holds the messages buffered at the time of the call and is already closed, so
ranging over it ends once that snapshot is consumed. Messages enqueued concurrently with (or after) the call may not be
included. For continuous observation, call Iterator() repeatedly, for example from a goroutine that polls on a ticker,
and drain each returned channel in turn.
Implementation notes
- The event stream is backed by the
eventstreampackage. The actor system creates anEventsStreamwhen it is constructed (NewActorSystem), keeps it acrossStop/Startcycles, and subscribes callers to the internal topictopic.events. - Lifecycle events (ActorStarted, ActorStopped, etc.) are published by the actor’s PID as it starts, stops, restarts,
passivates, is suspended or reinstated; the death watch publishes nothing to the stream. Cluster events
(NodeJoined, NodeLeft) are published by the cluster events loop once the cluster’s routing table has converged on the
membership change, that is once the coordinator has redistributed partitions for the new member set, so subscribers
and relocation act on a stable table. If no convergence is announced within the convergence timeout, 10 seconds by
default (see
WithConvergenceTimeout), for example because a member fails to acknowledge the rebalance, the event is published anyway.LeaderChangedis published from the same loop, on every node, whenever a topology event reveals that the coordinator moved to a different node; the initial coordinator election on startup is not published, only subsequent transitions. RelocationFailedis published by the relocator when a departed node’s actors or grains cannot be re-deployed. Each item is retried a bounded number of times with backoff before it is reported, and the report is terminal: the relocation run does not revisit failed items, so subscribe to this event to drive your own recovery (for example, re-spawning the affected actors or alerting).RelocationStartedis published by the leader whenever it starts relocating a departed node’s actors and grains, for graceful shutdowns and crashes alike.BestEffort()distinguishes the two: false means the set comes from the departed node’s graceful-shutdown snapshot and is complete; true means the node crashed and the set was reconstructed from the replicated cluster registry, so records lost with the crashed node’s partitions cannot be listed, and an empty or unexpectedly small set signals suspected record loss. See the relocation page for the replication settings that govern completeness.- Dead letters are published by the dead-letter actor when messages cannot be delivered.
- The event stream is always present; no option is required. It is independent of
WithPubSub()and cluster mode.
Related
- Observability: Metrics and dead letters
- PubSub: Application-level topic-based pub/sub (separate mechanism)