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§
- panic
- Types for handling panics.
- scheduler
- Structs for scheduling how work is dispatched amongst workers.
- util
- Helper structs that facilitate setting up a
rework
system.
Structs§
- Builder
- Builder of a
rework
system. - Command
Agent - A helper struct that allows sending
Command
s to the workers. - Handle
- Handle to a dispatcher.
- Request
Agent - A helper struct that allows sending
Request
s to the workers.
Enums§
- Command
- Commands that can be sent to workers to control how they behave.
- Workload
- Workload of a
Request
.
Traits§
- Request
- A trait that the actual request type must implement.
- WorkFn
- Defines how workers should respond to a request.
Functions§
- builder
- Returns a pre-configured builder that can be used to set up a
rework
system.
Type Aliases§
- Async
- Shorthand for
Pin<Box<dyn Future<Output = T>>>
.