pub struct Context<E> { /* private fields */ }Expand description
Runtime-provided context for an actor to interact with the system.
Use it to:
send(event): emit events into the broker tagged with this actor’s IDsend_child_event(event, parent_id): emit an event linked to a parent eventstop(): stop this actor (other actors continue running)stop_runtime(): initiate shutdown of the entire runtimeactor_id(): retrieve the actor’s identityis_sender_full(): check channel congestion before sending
See also: Envelope, crate::Meta, crate::Supervisor.
Implementations§
Source§impl<E> Context<E>
impl<E> Context<E>
Sourcepub async fn send<IE: Into<IntoEnvelope<E>>>(&self, into_envelope: IE) -> Result
pub async fn send<IE: Into<IntoEnvelope<E>>>(&self, into_envelope: IE) -> Result
Send an event to the broker. Accepts any type that converts into E.
The envelope will carry this actor’s name.
This awaits channel capacity (backpressure) to avoid silent drops.
§Errors
Returns Error::MailboxClosed if the broker
channel is closed.
Sourcepub async fn send_child_event<IE: Into<IntoEnvelope<E>>>(
&self,
into_envelope: IE,
parent_id: EventId,
) -> Result
pub async fn send_child_event<IE: Into<IntoEnvelope<E>>>( &self, into_envelope: IE, parent_id: EventId, ) -> Result
Emit a child event linked to the given parent event ID.
§Errors
Returns Error::MailboxClosed if the broker
channel is closed.
Sourcepub fn stop(&self) -> Result
pub fn stop(&self) -> Result
Stop this actor.
The actor’s event loop will exit after the current tick completes,
and on_shutdown will be called.
Other actors continue running.
To shut down the entire runtime instead, use stop_runtime().
§Errors
Returns Error::Internal if the command
channel is closed (runtime already shut down).
Sourcepub fn stop_runtime(&self) -> Result
pub fn stop_runtime(&self) -> Result
Initiate shutdown of the entire runtime.
All actors will be cancelled and the supervisor’s
join() or run()
call will return.
To stop only this actor, use stop().
§Errors
Returns Error::Internal if the command
channel is closed (runtime already shut down).
Sourcepub fn actor_name(&self) -> &str
pub fn actor_name(&self) -> &str
The actor’s name as registered with the supervisor.
Sourcepub fn is_sender_full(&self) -> bool
pub fn is_sender_full(&self) -> bool
Whether this actor’s channel to the broker has no remaining capacity.
Producers can use this to skip sending non-essential events when the channel is congested (stage 1). This is a cooperative mechanism - the producer decides what to skip.
// Skip telemetry when the channel is busy
if !ctx.is_sender_full() {
ctx.send(Event::Telemetry(stats)).await?;
}Note: each actor has its own channel to the broker (stage 1). This reflects the sending actor’s individual backlog, not global system pressure or subscriber-side congestion (stage 2).