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
F: The type of the service factory used inServiceCommands.LF: The type of the listener factory used inServiceCommands.
§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 individualServiceExecutors.
§Worker Thread Management
The manager spawns worker threads based on the runtime_config. Each worker thread:
- Runs its own
ServiceExecutorinstance - 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>
impl<F, LF> WorkerManager<F, LF>
Sourcepub fn spawn_workers_async<A>(&mut self) -> Vec<(JoinHandle<()>, OSender<()>)>
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
JoinHandlefor the spawned worker thread - An
OSenderthat can be used to signal the worker to shut down
Sourcepub 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,
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.
Sourcepub fn spawn_workers_inner<S, SO, FN, FNL, FNO>(
&mut self,
fn_lambda: S,
pre_f: FN,
) -> JoinHandlesWithOutput<FNO>
pub fn spawn_workers_inner<S, SO, FN, FNL, FNO>( &mut self, fn_lambda: S, pre_f: FN, ) -> JoinHandlesWithOutput<FNO>
Start workers according to runtime config. Threads JoinHandle will be returned and each factory Sender will be saved for config updating.
Sourcepub async fn dispatch_service_command(
&mut self,
cmd: ServiceCommand<F, LF>,
) -> ResultGroup<(), AnyError>where
ServiceCommand<F, LF>: Clone,
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
cmd- TheServiceCommandto be dispatched to all workers.
§Type Parameters
F- The service factory type, typically implementingAsyncMakeService.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)).