1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
use std::{collections::HashMap, mem, sync::Arc};
use futures::stream::AbortHandle;
use tokio::sync::{mpsc, Mutex};
use crate::{err::SendError, internal::Signal, reason::ActorStopReason};
tokio::task_local! {
#[doc(hidden)]
pub static CURRENT_ACTOR: GenericActorRef;
}
/// Provides functionality to stop and wait for an actor to complete based on an actor ref.
///
/// This trait is automatically implemented by the [`#[actor]`](macro@crate::actor) macro.
#[allow(async_fn_in_trait)]
pub trait ActorRef: Clone {
/// Returns the actor identifier.
fn id(&self) -> u64;
/// Returns whether the actor is currently alive.
fn is_alive(&self) -> bool;
/// Links this actor with a child, notifying the child actor if the parent dies through
/// [Actor::on_link_died](crate::actor::Actor::on_link_died), but not visa versa.
async fn link_child<R: ActorRef>(&self, child: &R);
/// Unlinks the actor with a previously linked actor.
async fn unlink_child<R: ActorRef>(&self, child: &R);
/// Links two actors with one another, notifying eachother if either actor dies through [Actor::on_link_died](crate::actor::Actor::on_link_died).
///
/// This operation is atomic.
async fn link_together<R: ActorRef>(&self, actor_ref: &R);
/// Unlinks two previously linked actors.
///
/// This operation is atomic.
async fn unlink_together<R: ActorRef>(&self, actor_ref: &R);
/// Notifies the actor that one of its links died.
///
/// This is called automatically when an actor dies.
fn notify_link_died(&self, id: u64, reason: ActorStopReason) -> Result<(), SendError>;
/// Signals the actor to stop after processing all messages currently in its mailbox.
///
/// This method sends a special stop message to the end of the actor's mailbox, ensuring
/// that the actor will process all preceding messages before stopping. Any messages sent
/// after this stop signal will be ignored and dropped. This approach allows for a graceful
/// shutdown of the actor, ensuring all pending work is completed before termination.
fn stop_gracefully(&self) -> Result<(), SendError>;
/// Kills the actor immediately.
///
/// This method aborts the actor immediately. Messages in the mailbox will be ignored and dropped.
///
/// The actors on_stop hook will still be called.
///
/// Note: If the actor is in the middle of processing a message, it will abort processing of that message.
fn kill(&self);
/// Waits for the actor to finish processing and stop.
///
/// This method suspends execution until the actor has stopped, ensuring that any ongoing
/// processing is completed and the actor has fully terminated. This is particularly useful
/// in scenarios where it's necessary to wait for an actor to clean up its resources or
/// complete its final tasks before proceeding.
///
/// Note: This method does not initiate the stop process; it only waits for the actor to
/// stop. You should signal the actor to stop using `stop_gracefully` or `kill`
/// before calling this method.
///
/// # Examples
///
/// ```ignore
/// // Assuming `actor.stop_gracefully().await` has been called earlier
/// actor.wait_for_stop().await;
/// ```
async fn wait_for_stop(&self);
#[doc(hidden)]
fn into_generic(self) -> GenericActorRef;
#[doc(hidden)]
fn from_generic(actor_ref: GenericActorRef) -> Self;
}
#[doc(hidden)]
#[derive(Clone, Debug)]
pub struct GenericActorRef {
id: u64,
mailbox: mpsc::UnboundedSender<Signal<()>>,
abort_handle: AbortHandle,
links: Arc<Mutex<HashMap<u64, GenericActorRef>>>,
}
impl GenericActorRef {
pub fn current() -> Self {
CURRENT_ACTOR.with(|actor_ref| actor_ref.clone())
}
pub fn try_current() -> Option<Self> {
CURRENT_ACTOR.try_with(|actor_ref| actor_ref.clone()).ok()
}
pub unsafe fn from_parts<M>(
id: u64,
channel: mpsc::UnboundedSender<Signal<M>>,
abort_handle: AbortHandle,
links: Arc<Mutex<HashMap<u64, GenericActorRef>>>,
) -> Self {
GenericActorRef {
id,
mailbox: mem::transmute(channel),
abort_handle,
links,
}
}
pub unsafe fn into_parts<M>(
self,
) -> (
u64,
mpsc::UnboundedSender<Signal<M>>,
AbortHandle,
Arc<Mutex<HashMap<u64, GenericActorRef>>>,
) {
(
self.id,
mem::transmute(self.mailbox),
self.abort_handle,
self.links,
)
}
fn signal(&self, signal: Signal<()>) -> Result<(), SendError> {
self.mailbox
.send(signal)
.map_err(|_| SendError::ActorNotRunning(()))
}
}
impl ActorRef for GenericActorRef {
fn id(&self) -> u64 {
self.id
}
fn is_alive(&self) -> bool {
!self.mailbox.is_closed()
}
async fn link_child<R: ActorRef>(&self, child: &R) {
if self.id == child.id() {
return;
}
let child: GenericActorRef = child.clone().into_generic();
self.links.lock().await.insert(child.id, child);
}
async fn unlink_child<R: ActorRef>(&self, child: &R) {
if self.id == child.id() {
return;
}
self.links.lock().await.remove(&child.id());
}
async fn link_together<R: ActorRef>(&self, actor_ref: &R) {
if self.id == actor_ref.id() {
return;
}
let actor_ref: GenericActorRef = actor_ref.clone().into_generic();
let acotr_ref_links = actor_ref.links.clone();
let (mut this_links, mut other_links) =
tokio::join!(self.links.lock(), acotr_ref_links.lock());
this_links.insert(actor_ref.id, actor_ref);
other_links.insert(self.id, self.clone());
}
async fn unlink_together<R: ActorRef>(&self, actor_ref: &R) {
if self.id == actor_ref.id() {
return;
}
let actor_ref: GenericActorRef = actor_ref.clone().into_generic();
let (mut this_links, mut other_links) =
tokio::join!(self.links.lock(), actor_ref.links.lock());
this_links.remove(&actor_ref.id);
other_links.remove(&self.id);
}
fn notify_link_died(&self, id: u64, reason: ActorStopReason) -> Result<(), SendError> {
self.signal(Signal::LinkDied(id, reason))
}
fn stop_gracefully(&self) -> Result<(), SendError> {
self.signal(Signal::Stop)
}
fn kill(&self) {
self.abort_handle.abort()
}
async fn wait_for_stop(&self) {
self.mailbox.closed().await;
}
fn into_generic(self) -> GenericActorRef {
self
}
fn from_generic(actor_ref: GenericActorRef) -> Self {
actor_ref
}
}