Skip to main content

WorkerPoolBuilder

Struct WorkerPoolBuilder 

Source
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

Source

pub fn new(name: impl Into<String>) -> Self

Create a new builder with the given pool name.

Source

pub fn with_strategy(self, strategy: LoadBalancingStrategy) -> Self

Set the load balancing strategy.

§Arguments
  • strategy - The strategy to use for distributing messages
Source

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
Source

pub fn with_metrics_collector(self, collector: Arc<dyn WorkerMetrics>) -> Self

Set the metrics collector for the pool.

§Arguments
  • collector - An Arc to an object implementing WorkerMetrics.
Source

pub fn add_worker<W: Worker + 'static>(self, worker: W) -> Self

Add a single worker to the pool.

§Arguments
  • worker - The worker to add
Source

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
Source

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
Source

pub fn add_workers<W: Worker + 'static>(self, workers: Vec<W>) -> Self

Add multiple workers to the pool.

§Arguments
  • workers - Vector of workers to add
Source

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
Source

pub fn with_middlewares(self, middlewares: Vec<Arc<dyn Middleware>>) -> Self

Add multiple middleware to the pool.

Source

pub fn build(self) -> WorkerResult<WorkerPool>

Build the worker pool with the configured settings.

§Returns

A configured WorkerPool ready to accept messages

§Errors

Returns WorkerError::ConfigError if:

  • No workers were added to the pool
Source

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.

Auto Trait Implementations§

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