Skip to main content

WorkerPool

Struct WorkerPool 

Source
pub struct WorkerPool<M: Message> { /* private fields */ }
Expand description

A supervision-aware, round-robin routing handle for a pool of identical worker actors.

Created by [EngineHandle::spawn_worker_pool]. Workers are spawned and supervised by the /user supervisor; this struct is a thin routing facade over N StableAddr<M>s that remain valid across individual worker restarts.

§Routing semantics

  • Round-robin: each call to send or ask selects the next worker in rotation.
  • Shared counter across clones: all clones share the rotation counter (Arc<AtomicUsize>), so concurrent senders interleave rather than duplicate.
  • Backpressure: SendError::MailboxFull is returned immediately when the selected worker’s mailbox is at capacity.
  • Restart window: SendError::Unroutable is returned during the brief interval between a worker crash and the supervisor refreshing its StableAddr. The window is transient; callers should yield and retry.

§In-flight message loss

Messages that were already in a crashed worker’s mailbox at crash time are lost (best-effort semantics). Full at-least-once delivery requires application-level acknowledgement — outside the scope of this primitive.

Implementations§

Source§

impl<M: Message> WorkerPool<M>

Source

pub fn new(workers: Vec<StableAddr<M>>) -> Self

Create a pool handle from a pre-built list of StableAddr<M>.

Called by the runtime builder; not intended for direct use in application code.

§Panics

Panics if workers is empty.

Source

pub fn send(&self, msg: M) -> Result<(), SendError>

Round-robin fire-and-forget send to one worker.

Advances the rotation counter and calls StableAddr::send on the selected worker. Does not retry on Unroutable; callers that need retry-on-crash semantics should yield and call send again.

§Errors
Source

pub async fn ask(&self, msg: M) -> Result<M::Response, AskError>

Round-robin request/response.

Selects one worker and awaits its reply. AskError::Send(Unroutable) may be returned during a crash window; callers decide retry.

Source

pub fn size(&self) -> usize

Number of worker slots in the pool.

Source

pub fn workers_ready_count(&self) -> usize

Number of workers whose StableAddr has been populated by the supervisor (i.e., the worker has been spawned at least once).

Useful in tests and health checks to wait until all workers are live before sending messages.

Source

pub fn worker_addr(&self, index: usize) -> Option<AddrHash>

Current AddrHash of the worker at index, or None if the worker has not yet been spawned or is in the crash/restart window.

Index is stable (slot 0 always refers to {prefix}-0); the hash changes on each restart as a new generation is assigned.

Trait Implementations§

Source§

impl<M: Message> Clone for WorkerPool<M>

Source§

fn clone(&self) -> Self

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

Auto Trait Implementations§

§

impl<M> Freeze for WorkerPool<M>

§

impl<M> RefUnwindSafe for WorkerPool<M>

§

impl<M> Send for WorkerPool<M>

§

impl<M> Sync for WorkerPool<M>

§

impl<M> Unpin for WorkerPool<M>

§

impl<M> UnsafeUnpin for WorkerPool<M>

§

impl<M> UnwindSafe for WorkerPool<M>

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