pub struct ActorContext<A: Actor> { /* private fields */ }Implementations§
Source§impl<A: Actor> ActorContext<A>
impl<A: Actor> ActorContext<A>
pub fn spawn_ctx(&self) -> &SpawnContext
Sourcepub async fn sleep(&self, duration: Duration)
pub async fn sleep(&self, duration: Duration)
Sleeps for a given amount of time.
That sleep is measured by the universe scheduler, which means that it can be
shortened if Universe::simulate_sleep(..) is used.
While sleeping, an actor is NOT protected from its supervisor.
It is up to the user to call ActorContext::protect_future(..).
pub fn mailbox(&self) -> &Mailbox<A>
pub fn actor_instance_id(&self) -> &str
Sourcepub fn protect_zone(&self) -> ProtectedZoneGuard
pub fn protect_zone(&self) -> ProtectedZoneGuard
This function returns a guard that prevents any supervisor from identifying the
actor as dead.
The protection ends when the ProtectZoneGuard is dropped.
In an ideal world, you should never need to call this function. It is only useful in some corner cases, like calling a long blocking from an external library that you trust.
Sourcepub async fn protect_future<Fut, T>(&self, future: Fut) -> Twhere
Fut: Future<Output = T>,
pub async fn protect_future<Fut, T>(&self, future: Fut) -> Twhere
Fut: Future<Output = T>,
Executes a future in a protected zone.
Sourcepub fn kill_switch(&self) -> &KillSwitch
pub fn kill_switch(&self) -> &KillSwitch
Gets a copy of the actor kill switch. This should rarely be used.
For instance, when quitting from the process_message function, prefer simply
returning Error(ActorExitStatus::Failure(..))
pub fn progress(&self) -> &Progress
pub fn spawn_actor<SpawnedActor: Actor>(&self) -> SpawnBuilder<SpawnedActor>
Sourcepub fn record_progress(&self)
pub fn record_progress(&self)
Records some progress.
This function is only useful when implementing actors that may take more than
HEARTBEAT to process a single message.
In that case, you can call this function in the middle of the process_message method
to prevent the actor from being identified as blocked or dead.
Sourcepub async fn send_message<DestActor, M>(
&self,
mailbox: &Mailbox<DestActor>,
msg: M,
) -> Result<Receiver<DestActor::Reply>, SendError>
pub async fn send_message<DestActor, M>( &self, mailbox: &Mailbox<DestActor>, msg: M, ) -> Result<Receiver<DestActor::Reply>, SendError>
Posts a message in an actor’s mailbox.
This method does not wait for the message to be handled by the
target actor. However, it returns a oneshot receiver that the caller
that makes it possible to .await it.
If the reply is important, chances are the .ask(...) method is
more indicated.
Droppping the receiver channel will not cancel the processing of the message. It is a very common usage. In fact most actors are expected to send message in a fire-and-forget fashion.
Regular messages (as opposed to commands) are queued and guaranteed to be processed in FIFO order.
This method hides logic to prevent an actor from being identified as frozen if the destination actor channel is saturated, and we are simply experiencing back pressure.
pub async fn ask<DestActor, M, T>( &self, mailbox: &Mailbox<DestActor>, msg: M, ) -> Result<T, AskError<Infallible>>
Sourcepub async fn ask_for_res<DestActor, M, T, E>(
&self,
mailbox: &Mailbox<DestActor>,
msg: M,
) -> Result<T, AskError<E>>
pub async fn ask_for_res<DestActor, M, T, E>( &self, mailbox: &Mailbox<DestActor>, msg: M, ) -> Result<T, AskError<E>>
Similar to send_message, except this method
waits asynchronously for the actor reply.
Sourcepub async fn send_exit_with_success<Dest: Actor>(
&self,
mailbox: &Mailbox<Dest>,
) -> Result<(), SendError>
pub async fn send_exit_with_success<Dest: Actor>( &self, mailbox: &Mailbox<Dest>, ) -> Result<(), SendError>
Send the Success message to terminate the destination actor with the Success exit status.
The message is queued like any regular message, so that pending messages will be processed first.
Sourcepub async fn send_self_message<M>(
&self,
msg: M,
) -> Result<Receiver<A::Reply>, SendError>
pub async fn send_self_message<M>( &self, msg: M, ) -> Result<Receiver<A::Reply>, SendError>
Sends a message to an actor’s own mailbox.
Warning: This method is dangerous as it can very easily cause a deadlock.
Sourcepub fn try_send_self_message<M>(
&self,
msg: M,
) -> Result<Receiver<A::Reply>, TrySendError<M>>
pub fn try_send_self_message<M>( &self, msg: M, ) -> Result<Receiver<A::Reply>, TrySendError<M>>
Attempts to send a message to itself. The message will be queue to self’s low_priority queue.
Warning: This method will always fail if an actor has a capacity of 0.
Sourcepub async fn schedule_self_msg<M>(&self, after_duration: Duration, message: M)
pub async fn schedule_self_msg<M>(&self, after_duration: Duration, message: M)
Schedules a message that will be sent to the high-priority
queue of the actor Mailbox once after_duration has elapsed.