pub struct WorkerPoolBuilder { /* private fields */ }Expand description
Builder for configuring and creating worker pools.
Provides a fluent API for setting up worker pools with custom configurations.
§Example
use foxtive_worker::builder::WorkerPoolBuilder;
use foxtive_worker::strategies::LoadBalancingStrategy;
use foxtive_worker::metrics::NoOpMetrics;
use foxtive_worker::{Worker, ReceivedMessage};
use foxtive_worker::error::WorkerResult;
use std::sync::Arc;
struct MyWorker;
#[async_trait::async_trait]
impl Worker for MyWorker {
fn id(&self) -> &str { "my-worker" }
async fn process(&self, _msg: ReceivedMessage<serde_json::Value>) -> WorkerResult<()> {
Ok(())
}
}
let pool = WorkerPoolBuilder::new("my-pool")
.with_strategy(LoadBalancingStrategy::RoundRobin)
.with_concurrency_limit(100)
.with_metrics_collector(Arc::new(NoOpMetrics))
.add_worker(MyWorker)
.build();Implementations§
Source§impl WorkerPoolBuilder
impl WorkerPoolBuilder
Sourcepub fn with_strategy(self, strategy: LoadBalancingStrategy) -> Self
pub fn with_strategy(self, strategy: LoadBalancingStrategy) -> Self
Set the load balancing strategy.
§Arguments
strategy- The strategy to use for distributing messages
Sourcepub fn with_concurrency_limit(self, limit: usize) -> Self
pub fn with_concurrency_limit(self, limit: usize) -> Self
Set the concurrency limit for the pool.
This limits how many messages can be processed concurrently across all workers.
§Arguments
limit- Maximum concurrent messages
Sourcepub fn with_metrics_collector(self, collector: Arc<dyn WorkerMetrics>) -> Self
pub fn with_metrics_collector(self, collector: Arc<dyn WorkerMetrics>) -> Self
Set the metrics collector for the pool.
§Arguments
collector- AnArcto an object implementingWorkerMetrics.
Sourcepub fn add_worker<W: Worker + 'static>(self, worker: W) -> Self
pub fn add_worker<W: Worker + 'static>(self, worker: W) -> Self
Sourcepub fn add_boxed_worker(self, worker: Box<dyn Worker>) -> Self
pub fn add_boxed_worker(self, worker: Box<dyn Worker>) -> Self
Add a boxed worker to the pool.
Useful when adding heterogeneous workers.
§Arguments
worker- The boxed worker to add
Sourcepub fn add_arc_worker(self, worker: Arc<dyn Worker>) -> Self
pub fn add_arc_worker(self, worker: Arc<dyn Worker>) -> Self
Add an Arc-wrapped worker to the pool.
This is the most efficient way if you already have an Arc.
§Arguments
worker- The Arc-wrapped worker to add
Sourcepub fn add_workers<W: Worker + 'static>(self, workers: Vec<W>) -> Self
pub fn add_workers<W: Worker + 'static>(self, workers: Vec<W>) -> Self
Sourcepub fn with_middleware<M: Middleware + 'static>(self, middleware: M) -> Self
pub fn with_middleware<M: Middleware + 'static>(self, middleware: M) -> Self
Add a middleware to the pool.
Middleware will be executed in the order they are added, forming a chain that processes messages before they reach the workers.
§Arguments
middleware- The middleware to add
Sourcepub fn with_middlewares(self, middlewares: Vec<Arc<dyn Middleware>>) -> Self
pub fn with_middlewares(self, middlewares: Vec<Arc<dyn Middleware>>) -> Self
Add multiple middleware to the pool.
Sourcepub fn build(self) -> WorkerResult<WorkerPool>
pub fn build(self) -> WorkerResult<WorkerPool>
Sourcepub fn build_allow_empty(self) -> WorkerPool
pub fn build_allow_empty(self) -> WorkerPool
Build the worker pool, returning the pool even if no workers were added.
This is useful for dynamic worker addition later.