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 addressnode— optional ownedNode(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: AddrThis 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
impl ActorContext
Sourcepub fn new(peer_id: String) -> Self
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.
Sourcepub fn child_actor_count(&self) -> usize
pub fn child_actor_count(&self) -> usize
Returns the number of child actors spawned by this context.
Sourcepub fn start_actor(&self, actor: Box<dyn Actor>) -> Addr
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.
Sourcepub fn start_actor_bounded(&self, actor: Box<dyn Actor>, bound: usize) -> Addr
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.
Sourcepub fn start_router(&self, actor: Box<dyn Actor>) -> Addr
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).
Sourcepub fn start_router_bounded(&self, actor: Box<dyn Actor>, bound: usize) -> Addr
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.
Sourcepub fn child_task<T>(&self, task: T)
pub fn child_task<T>(&self, task: T)
Spawns a child async task (non-blocking).
The task’s JoinHandle is tracked so it can be aborted on stop.
Sourcepub fn blocking_child_task<F>(&self, task: F)
pub fn blocking_child_task<F>(&self, task: F)
Spawns a blocking child task via spawn_blocking.
Use for CPU-intensive work that should not block the async runtime.
Trait Implementations§
Source§impl Clone for ActorContext
impl Clone for ActorContext
Source§fn clone(&self) -> ActorContext
fn clone(&self) -> ActorContext
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more