How actor linking works
mainActor linking provides peer-to-peer monitoring between actors, independent of the parent-child supervision hierarchy. This is useful when actors need to monitor each other or when you need cross-node monitoring.
By default, if a linked actor dies, the linked peer will also stop. However, you can intercept this by implementing the on_link_died method in your Actor implementation. Returning ControlFlow::Continue(()) allows the actor to keep running, while ControlFlow::Break(reason) causes it to stop.
async fn on_link_died(
&mut self,
_actor_ref: WeakActorRef<Self>,
id: ActorId,
reason: ActorStopReason,
) -> Result<ControlFlow<ActorStopReason>, Self::Error> {
tracing::warn!("linked actor {id} died: {reason:?}");
Ok(ControlFlow::Continue(())) // Keep running
}