Skip to main content

WorkerPool

Struct WorkerPool 

Source
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

Source

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.

Source

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 name
  • strategy - Load balancing strategy
  • concurrency_limit - Maximum concurrent messages across all workers
  • metrics_collector - Metrics collector implementation
Source

pub fn add_worker(&mut self, worker: Arc<dyn Worker>)

Add a worker to the pool.

Source

pub fn add_workers(&mut self, workers: Vec<Arc<dyn Worker>>)

Add multiple workers to the pool.

Source

pub fn worker_count(&self) -> usize

Get the number of workers in the pool.

Source

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.

Source

pub fn name(&self) -> &str

Get the pool name.

Source

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.

Source

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.

Source

pub fn in_flight_count(&self) -> usize

Get the current number of in-flight tasks.

Trait Implementations§

Source§

impl Debug for WorkerPool

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl HealthCheck for WorkerPool

Source§

fn check_health(&self) -> HealthStatus

Performs a health check and returns the current status.
Source§

fn status_message(&self) -> String

Returns a descriptive message about the health status.

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