Expand description

Multi-threaded task processing in the Master-Worker pattern.

Features

  • Multi-threaded workers, each with a single-thread async runtime, that can work on !Send futures.
  • Async as an option–you don’t necessarily need an async runtime at all.
  • Load balanced, as long as Workload is properly defined.

Usage

First define the Request type and the WorkFn.

use futures::future::FutureExt;
use rework::{Request, WorkFn, Workload};

// Debug is a supertrait of Request
#[derive(Debug)]
enum HelloOrEcho {
    Hello,
    Echo(String),
}

impl Request for HelloOrEcho {
    fn workload(&self) -> Workload {
        1.into()    
    }
}

// WorkFn is implemented for async fns that take a Request type parameter and return a Send + 'static
async fn work(req: HelloOrEcho) -> String {
    match req {
        HelloOrEcho::Hello => "Hello".to_owned(),
        HelloOrEcho::Echo(s) => s,
    }
}

Then use builder() to get the system up and running:

use futures::channel::mpsc;
use futures::stream::StreamExt;
use tokio::runtime::Builder;

let msg = "Heeey".to_owned();
let (tx, mut rx) = mpsc::unbounded();

let handle = rework::builder(|| work).sink(tx).build();
handle.request(HelloOrEcho::Echo(msg.clone()));

let rt = Builder::new_current_thread().build().unwrap();
rt.block_on(async {
    let recv = rx.next().await;
    assert_eq!(recv, Some(msg));
});

Modules

Types for handling panics.
Structs for scheduling how work is dispatched amongst workers.
Helper structs that facilitate setting up a rework system.

Structs

Builder of a rework system.
A helper struct that allows sending Commands to the workers.
Handle to a dispatcher.
A helper struct that allows sending Requests to the workers.

Enums

Commands that can be sent to workers to control how they behave.
Workload of a Request.

Traits

A trait that the actual request type must implement.
Defines how workers should respond to a request.

Functions

Returns a pre-configured builder that can be used to set up a rework system.

Type Definitions

Shorthand for Pin<Box<dyn Future<Output = T>>>.