Crate work_dispatcher

Crate work_dispatcher 

Source
Expand description

§WorkDispatcher

A simple yet powerful concurrent data processing dispatch framework based on the Actor model.

This crate provides a generic WorkDispatcher to set up a dispatcher that reads items from a Producer and processes them concurrently using a pool of Processor actors.

§Example

// 1. Define your data item
struct MyDataItem(i32);

// 2. Implement the Producer
use work_dispatcher::{Producer, Processor};
use flume::Sender;
use anyhow::Result;
use async_trait::async_trait;

struct MyProducer;
#[async_trait]
impl Producer for MyProducer {
    type Item = MyDataItem;
    async fn run(self, sender: Sender<Self::Item>) {
        for i in 0..100 {
            sender.send_async(MyDataItem(i))?;
        }
        Ok(())
    }
}

// 3. Implement the Processor
#[derive(Clone)]
struct MyProcessor;
#[async_trait]
impl Processor for MyProcessor {
    type Item = MyDataItem;
    type Context = String; // Example context

    async fn process(&self, item: Self::Item, context: &Self::Context) {
        println!("Processing item #{} with context '{}'", item.0, context);
        tokio::time::sleep(std::time::Duration::from_millis(10)).await;
        Ok(())
    }
}

// 4. Run the dispatcher
#[tokio::main]
async fn main() -> Result<()> {
    let producer = MyProducer;
    let processor = MyProcessor;
    let context = "MyAppContext".to_string();

    work_dispatcher::WorkDispatcher::new(producer, processor, context)
        .workers(4)
        .run()
        .await?;

    Ok(())
}

Structs§

WorkDispatcher
The WorkDispatcher orchestrates the entire concurrent dispatcher.

Traits§

Processor
Processor defines the logic for processing a single item. It runs concurrently in multiple async tasks (actors).
Producer
Producer defines how to generate items for the dispatcher. It runs in a dedicated blocking thread.