Skip to main content

Context

Struct Context 

Source
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 ID
  • send_child_event(event, parent_id): emit an event linked to a parent event
  • stop(): stop this actor (other actors continue running)
  • stop_runtime(): initiate shutdown of the entire runtime
  • actor_id(): retrieve the actor’s identity
  • is_sender_full(): check channel congestion before sending

See also: Envelope, crate::Meta, crate::Supervisor.

Implementations§

Source§

impl<E> Context<E>

Source

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.

Source

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.

Source

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).

Source

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).

Source

pub fn actor_id(&self) -> &ActorId

The identity of this actor.

Source

pub fn actor_name(&self) -> &str

The actor’s name as registered with the supervisor.

Source

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).

Trait Implementations§

Source§

impl<E: Clone> Clone for Context<E>

Source§

fn clone(&self) -> Context<E>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<E> Debug for Context<E>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<E> Freeze for Context<E>

§

impl<E> RefUnwindSafe for Context<E>

§

impl<E> Send for Context<E>
where E: Sync + Send,

§

impl<E> Sync for Context<E>
where E: Sync + Send,

§

impl<E> Unpin for Context<E>

§

impl<E> UnsafeUnpin for Context<E>

§

impl<E> UnwindSafe for Context<E>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more