pub struct WorkerPool { /* private fields */ }Expand description
A pool of workers with load balancing for message distribution.
The pool manages multiple worker instances and distributes incoming messages based on the configured load balancing strategy.
§Example
use foxtive_worker::pool::WorkerPool;
use foxtive_worker::strategies::LoadBalancingStrategy;
use foxtive_worker::metrics::NoOpMetrics;
use std::sync::Arc;
let pool = WorkerPool::new("my-pool", LoadBalancingStrategy::RoundRobin, Arc::new(NoOpMetrics));
// Add workers...
// Dispatch messages...Implementations§
Source§impl WorkerPool
impl WorkerPool
Sourcepub fn new(
name: impl Into<String>,
strategy: LoadBalancingStrategy,
metrics_collector: Arc<dyn WorkerMetrics>,
) -> Self
pub fn new( name: impl Into<String>, strategy: LoadBalancingStrategy, metrics_collector: Arc<dyn WorkerMetrics>, ) -> Self
Create a new worker pool with the given name, load balancing strategy, and metrics collector.
Sourcepub fn with_concurrency(
name: impl Into<String>,
strategy: LoadBalancingStrategy,
concurrency_limit: usize,
metrics_collector: Arc<dyn WorkerMetrics>,
) -> Self
pub fn with_concurrency( name: impl Into<String>, strategy: LoadBalancingStrategy, concurrency_limit: usize, metrics_collector: Arc<dyn WorkerMetrics>, ) -> Self
Create a new worker pool with custom concurrency limit.
§Arguments
name- Pool namestrategy- Load balancing strategyconcurrency_limit- Maximum concurrent messages across all workersmetrics_collector- Metrics collector implementation
Sourcepub fn add_worker(&mut self, worker: Arc<dyn Worker>)
pub fn add_worker(&mut self, worker: Arc<dyn Worker>)
Add a worker to the pool.
Sourcepub fn add_workers(&mut self, workers: Vec<Arc<dyn Worker>>)
pub fn add_workers(&mut self, workers: Vec<Arc<dyn Worker>>)
Add multiple workers to the pool.
Sourcepub fn worker_count(&self) -> usize
pub fn worker_count(&self) -> usize
Get the number of workers in the pool.
Sourcepub fn with_middlewares(self, middlewares: Vec<Arc<dyn Middleware>>) -> Self
pub fn with_middlewares(self, middlewares: Vec<Arc<dyn Middleware>>) -> Self
Set middleware for the pool.
This allows you to inject middleware processing into the message flow. Middleware will be executed in order before messages reaches workers.
Sourcepub async fn dispatch(
&self,
message: ReceivedMessage<Value>,
) -> WorkerResult<()>
pub async fn dispatch( &self, message: ReceivedMessage<Value>, ) -> WorkerResult<()>
Dispatch a message to a worker based on the load balancing strategy.
Spawns an async task to process the message, respecting concurrency limits. If middleware is configured, the message flows through the chain before reaching the worker.
Sourcepub async fn shutdown(&self) -> WorkerResult<()>
pub async fn shutdown(&self) -> WorkerResult<()>
Shutdown the pool gracefully.
This prevents new messages from being dispatched, cancels in-flight tasks, and waits for completion with a timeout using efficient notification.
Sourcepub fn in_flight_count(&self) -> usize
pub fn in_flight_count(&self) -> usize
Get the current number of in-flight tasks.