Skip to content

Commit

Permalink
feat: Add PoisonPill message type
Browse files Browse the repository at this point in the history
  • Loading branch information
j5ik2o committed Jul 12, 2024
1 parent b649bbd commit d239af7
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 14 deletions.
1 change: 0 additions & 1 deletion src/actor/actor/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ pub trait Actor: Debug + Send + Sync + 'static {
AutoReceiveMessage::PostRestart => self.post_restart(context_handle).await,
AutoReceiveMessage::PreStop => self.pre_stop(context_handle).await,
AutoReceiveMessage::PostStop => self.post_stop(context_handle).await,
AutoReceiveMessage::PoisonPill => Ok(()),
AutoReceiveMessage::Terminated(t) => self.post_child_terminate(context_handle, &t).await,
},
_ => self.receive(context_handle.clone(), message_handle).await,
Expand Down
8 changes: 3 additions & 5 deletions src/actor/context/actor_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use crate::actor::message::message_or_envelope::{
unwrap_envelope_header, unwrap_envelope_sender, wrap_envelope, MessageEnvelope,
};
use crate::actor::message::not_influence_receive_timeout::NotInfluenceReceiveTimeoutHandle;
use crate::actor::message::poison_pill::PoisonPill;
use crate::actor::message::readonly_message_headers::ReadonlyMessageHeadersHandle;
use crate::actor::message::receive_timeout::ReceiveTimeout;
use crate::actor::message::response::ResponseHandle;
Expand Down Expand Up @@ -171,7 +172,7 @@ impl ActorContext {
async fn default_receive(&mut self) -> Result<(), ActorError> {
let message = self.get_message_handle_opt().await.expect("Failed to retrieve message");
tracing::debug!("ActorContext::default_receive: message = {:?}", message);
if let Some(AutoReceiveMessage::PoisonPill) = message.to_typed::<AutoReceiveMessage>() {
if message.to_typed::<PoisonPill>().is_some() {
let me = self.get_self_opt().await.unwrap();
self.stop(&me).await;
Ok(())
Expand Down Expand Up @@ -909,10 +910,7 @@ impl StopperPart for ActorContext {
async fn poison(&mut self, pid: &ExtendedPid) {
let inner_mg = self.inner.lock().await;
pid
.send_user_message(
inner_mg.actor_system.clone(),
MessageHandle::new(AutoReceiveMessage::PoisonPill),
)
.send_user_message(inner_mg.actor_system.clone(), MessageHandle::new(PoisonPill))
.await;
}

Expand Down
7 changes: 2 additions & 5 deletions src/actor/context/root_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ use crate::actor::context::{
InfoPart, MessagePart, SenderContext, SenderPart, SpawnerContext, SpawnerPart, StopperPart,
};
use crate::actor::future::{Future, FutureProcess};
use crate::actor::message::auto_receive_message::AutoReceiveMessage;
use crate::actor::message::message_handle::MessageHandle;
use crate::actor::message::message_headers::MessageHeaders;
use crate::actor::message::message_or_envelope::MessageEnvelope;
use crate::actor::message::poison_pill::PoisonPill;
use crate::actor::message::readonly_message_headers::ReadonlyMessageHeadersHandle;
use crate::actor::message::system_message::SystemMessage;
use crate::actor::message::watch::Watch;
Expand Down Expand Up @@ -249,10 +249,7 @@ impl StopperPart for RootContext {

async fn poison(&mut self, pid: &ExtendedPid) {
pid
.send_user_message(
self.get_actor_system().await.clone(),
MessageHandle::new(AutoReceiveMessage::PoisonPill),
)
.send_user_message(self.get_actor_system().await.clone(), MessageHandle::new(PoisonPill))
.await
}

Expand Down
1 change: 1 addition & 0 deletions src/actor/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod message_or_envelope;
mod message_or_envelope_test;
pub mod messages;
pub mod not_influence_receive_timeout;
pub mod poison_pill;
pub mod readonly_message_headers;
pub mod receive_timeout;
pub mod response;
Expand Down
3 changes: 0 additions & 3 deletions src/actor/message/auto_receive_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub enum AutoReceiveMessage {
PostRestart,
PreStop,
PostStop,
PoisonPill,
Terminated(TerminateInfo),
}

Expand All @@ -41,7 +40,6 @@ impl Display for AutoReceiveMessage {
AutoReceiveMessage::PostRestart => write!(f, "PostRestart"),
AutoReceiveMessage::PreStop => write!(f, "PreStop"),
AutoReceiveMessage::PostStop => write!(f, "PostStop"),
AutoReceiveMessage::PoisonPill => write!(f, "PoisonPill"),
AutoReceiveMessage::Terminated(_) => write!(f, "Terminated"),
}
}
Expand All @@ -57,7 +55,6 @@ impl Message for AutoReceiveMessage {
(AutoReceiveMessage::PostRestart, Some(&AutoReceiveMessage::PostRestart)) => true,
(AutoReceiveMessage::PreStop, Some(&AutoReceiveMessage::PreStop)) => true,
(AutoReceiveMessage::PostStop, Some(&AutoReceiveMessage::PostStop)) => true,
(AutoReceiveMessage::PoisonPill, Some(&AutoReceiveMessage::PoisonPill)) => true,
(AutoReceiveMessage::Terminated(me), Some(&AutoReceiveMessage::Terminated(ref you))) => *me == *you,
_ => false,
}
Expand Down
15 changes: 15 additions & 0 deletions src/actor/message/poison_pill.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use crate::actor::message::message::Message;
use std::any::Any;

#[derive(Debug, Clone, PartialEq)]
pub struct PoisonPill;

impl Message for PoisonPill {
fn eq_message(&self, other: &dyn Message) -> bool {
other.as_any().is::<PoisonPill>()
}

fn as_any(&self) -> &(dyn Any + Send + Sync + 'static) {
self
}
}

0 comments on commit d239af7

Please sign in to comment.