WorkerManager

Struct WorkerManager 

Source
pub struct WorkerManager<F, LF> { /* private fields */ }
Expand description

Orchestrates and manages a fleet of worker threads, each running a ServiceExecutor.

The WorkerManager is responsible for:

  • Spawning and initializing worker threads
  • Distributing ServiceCommands to all workers
  • Collecting and aggregating results from worker operations
  • Managing the lifecycle of worker threads

It acts as the central coordinator in a multi-threaded service deployment system, bridging the gap between the main application thread and individual worker threads.

§Type Parameters

§Fields

  • runtime_config: Configuration for the runtime environment of worker threads.
  • thread_pool: An optional thread pool for executing blocking operations.
  • workers: A collection of channels to communicate with individual ServiceExecutors.

§Worker Thread Management

The manager spawns worker threads based on the runtime_config. Each worker thread:

  • Runs its own ServiceExecutor instance
  • Can be optionally bound to a specific CPU core for improved performance
  • Receives ServiceCommands through a dedicated channel

§Usage

Typically, a WorkerManager is created once at application startup.

After initialization, ServiceCommands can be broadcast to all workers.

§Thread Safety

While the WorkerManager itself is not thread-safe and should be used from a single thread, it manages communication with multiple worker threads in a thread-safe manner using channels.

Implementations§

Source§

impl<F, LF> WorkerManager<F, LF>
where F: Send + 'static, LF: Send + 'static,

Source

pub fn spawn_workers_async<A>(&mut self) -> Vec<(JoinHandle<()>, OSender<()>)>

Spawns worker threads asynchronously, each running a ServiceExecutor.

This method initializes the worker threads based on the runtime_config and returns handles to these threads along with channels to signal their termination.

§Type Parameters
  • A: The type of the argument passed to the service.
§Returns

A vector of tuples, each containing:

  • A JoinHandle for the spawned worker thread
  • An OSender that can be used to signal the worker to shut down
Source

pub fn spawn_workers_async_with_fn<A, FN, FNL, FNO>( &mut self, f: FN, ) -> JoinHandlesWithOutput<FNO>
where F: AsyncMakeService, ServiceCommand<F, LF>: Execute<A, F::Service>, FN: Fn(usize) -> (FNL, FNO), FNL: Fn() + Send + 'static,

Spawns worker threads with custom initialization functions.

Similar to spawn_workers_async, but allows specifying a custom function to be executed at the start of each worker thread.

§Type Parameters
  • A: The type of the argument passed to the service.
  • FN: The type of the function that generates initialization functions and outputs.
  • FNL: The type of the initialization function.
  • FNO: The type of the output from the initialization function.
§Arguments
  • f: A function that takes a worker ID and returns a tuple of (initialization function, output).
§Returns

A tuple containing:

  • A vector of (JoinHandle, OSender) pairs for each worker thread.
  • A vector of outputs from the initialization functions.
Source

pub fn spawn_workers_inner<S, SO, FN, FNL, FNO>( &mut self, fn_lambda: S, pre_f: FN, ) -> JoinHandlesWithOutput<FNO>
where S: Fn(OReceiver<()>, Receiver<ServiceCommandTask<F, LF>>, usize, FNL) -> SO, SO: FnOnce(RuntimeWrapper) + Send + 'static, FN: Fn(usize) -> (FNL, FNO), FNL: Fn() + Send + 'static,

Start workers according to runtime config. Threads JoinHandle will be returned and each factory Sender will be saved for config updating.

Source

pub async fn dispatch_service_command( &mut self, cmd: ServiceCommand<F, LF>, ) -> ResultGroup<(), AnyError>
where ServiceCommand<F, LF>: Clone,

Dispatches a ServiceCommand to all managed workers and collects their results.

This method is a crucial part of worker coordination, enabling synchronized operations across all worker threads. It demonstrates how the WorkerManager orchestrates actions defined by ServiceCommands across multiple worker threads.

§Arguments
§Type Parameters
  • F - The service factory type, typically implementing AsyncMakeService.
  • LF - The listener factory type.
§Returns

Returns a ResultGroup containing the results from all workers. Each result is either a success (Ok(())) or an error (Err(AnyError)).

Source§

impl<F, LF> WorkerManager<F, LF>

Source

pub fn new(runtime_config: RuntimeConfig) -> Self

Source

pub fn config(&self) -> &RuntimeConfig

Auto Trait Implementations§

§

impl<F, LF> Freeze for WorkerManager<F, LF>

§

impl<F, LF> !RefUnwindSafe for WorkerManager<F, LF>

§

impl<F, LF> Send for WorkerManager<F, LF>
where F: Send, LF: Send,

§

impl<F, LF> Sync for WorkerManager<F, LF>
where F: Send, LF: Send,

§

impl<F, LF> Unpin for WorkerManager<F, LF>

§

impl<F, LF> !UnwindSafe for WorkerManager<F, LF>

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> 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> Same for T

Source§

type Output = T

Should always be Self
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