Skip to main content

ActorContext

Struct ActorContext 

Source
pub struct ActorContext {
    pub peer_id: Arc<RwLock<String>>,
    pub router: Arc<RwLock<Addr>>,
    pub addr: Addr,
    pub is_stopped: Arc<RwLock<bool>>,
    pub shutdown_rx: Receiver<bool>,
    pub node: Arc<RwLock<Option<Node>>>,
    pub metrics: Arc<Metrics>,
    /* private fields */
}
Expand description

Per-actor context providing access to runtime services.

Each actor receives an ActorContext in pre_start, handle, and stopping. The context is clonable — clones share the same underlying state via Arc.

§Key Fields

  • peer_id — this node’s peer identifier (shared across all actors)
  • router — the router actor’s address (for forwarding messages)
  • addr — this actor’s own address
  • node — optional owned Node (set for the root actor)

Fields§

§peer_id: Arc<RwLock<String>>

This node’s peer ID, shared across all actors.

§router: Arc<RwLock<Addr>>

The router actor’s address for message forwarding. Arc<RwLock<…>> so it can be set after construction (the root node’s router is created after the node itself).

§addr: Addr

This actor’s own address.

§is_stopped: Arc<RwLock<bool>>

Whether this actor has been stopped.

§shutdown_rx: Receiver<bool>

Shutdown signal receiver — set to true when graceful shutdown begins. Long-running child tasks select on this to break their loops.

§node: Arc<RwLock<Option<Node>>>

Optional owned Node (set for the root actor).

§metrics: Arc<Metrics>

Shared metrics handle — lock-free counters for the relay hot path.

Cloned from the root Node’s Metrics so all actors in the tree observe the same atomic counters. Set in Node::new_with_config and propagated through child_context.

Implementations§

Source§

impl ActorContext

Source

pub fn new(peer_id: String) -> Self

Creates a new ActorContext with the given peer ID.

The addr and router fields are initialized to Addr::noop() and should be set before use.

Source

pub fn child_actor_count(&self) -> usize

Returns the number of child actors spawned by this context.

Source

pub fn start_actor(&self, actor: Box<dyn Actor>) -> Addr

Spawns a child actor with an unbounded channel and returns its address.

The actor runs in a tokio task. Its lifecycle is managed by this context — calling stop() will send a stop signal and abort the task.

Source

pub fn start_actor_bounded(&self, actor: Box<dyn Actor>, bound: usize) -> Addr

Spawns a child actor with a bounded channel and returns its address.

The bound parameter sets the channel capacity. When full, send returns Err(()), applying backpressure to senders. Use for write-heavy actors where unbounded queue growth is undesirable.

Source

pub fn start_router(&self, actor: Box<dyn Actor>) -> Addr

Spawns a router actor. The router’s context will have its router field set to its own address (so messages forwarded to router come back to itself).

Source

pub fn start_router_bounded(&self, actor: Box<dyn Actor>, bound: usize) -> Addr

Spawns a router actor with a bounded mailbox.

Same as start_router but with an explicit backpressure ceiling. Use when the default DEFAULT_MAILBOX_CAPACITY is not appropriate for the deployment.

Source

pub fn child_task<T>(&self, task: T)
where T: Future<Output = ()> + Send + 'static,

Spawns a child async task (non-blocking).

The task’s JoinHandle is tracked so it can be aborted on stop.

Source

pub fn blocking_child_task<F>(&self, task: F)
where F: FnOnce() + Send + 'static,

Spawns a blocking child task via spawn_blocking.

Use for CPU-intensive work that should not block the async runtime.

Source

pub fn stop(&self)

Stops this actor and all its children.

Aborts all child tasks and sends stop signals to all child actors. Sets is_stopped to true.

Trait Implementations§

Source§

impl Clone for ActorContext

Source§

fn clone(&self) -> ActorContext

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more

Auto Trait Implementations§

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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, 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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V