use std::time::Duration;
use hyperfile_reactor::{FairPriority, Reactor, Task, TaskBuilder};
#[derive(Debug)]
struct FileReq {
id: usize,
}
#[derive(Debug)]
struct FileResp {
id: usize,
}
#[derive(Debug)]
struct FileCtx {
req: FileReq,
resp: FileResp,
}
struct FileTask {
id: usize,
}
impl FileTask {
fn new(id: usize) -> Self {
Self { id }
}
}
impl Task<FileCtx> for FileTask {
async fn handle(&mut self, ctx: FileCtx) {
let id = self.id;
tokio::task::spawn(async move {
tokio::time::sleep(Duration::from_millis(100)).await;
println!("file id {}, req: {}, resp: {}", id, ctx.req.id, ctx.resp.id);
});
}
}
fn main() {
let reactor = Reactor::<FileCtx, FileTask>::new_current().expect("build reactor");
let mut b1 = TaskBuilder::<FileCtx>::new();
let cb1 = b1.add_bounded(0, 8);
let hi1 = b1.add_unbounded(1);
let lo1 = b1.add_unbounded(2);
let handler1 = reactor.spawn(FileTask::new(1), b1).expect("spawn 1");
let mut b2 = TaskBuilder::<FileCtx>::new();
let _cb2 = b2.add_bounded(0, 4);
let _hi2 = b2.add_unbounded(1);
let lo2 = b2.add_unbounded(2);
let handler2 = reactor
.spawn_with(FileTask::new(2), b2, FairPriority::new(4))
.expect("spawn 2");
let mut producers = Vec::new();
for x in 0..10 {
let fh = handler1.clone();
let ch = if x % 3 == 0 {
cb1
} else if x % 3 == 1 {
hi1
} else {
lo1
};
producers.push(std::thread::spawn(move || {
let ctx = FileCtx {
req: FileReq { id: x },
resp: FileResp { id: x },
};
fh.send(ch, ctx).expect("send");
}));
}
for x in 10..20 {
let fh = handler2.clone();
producers.push(std::thread::spawn(move || {
let ctx = FileCtx {
req: FileReq { id: x },
resp: FileResp { id: x },
};
fh.send(lo2, ctx).expect("send");
}));
}
for p in producers {
p.join().unwrap();
}
std::thread::sleep(Duration::from_millis(500));
{
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
let fh = handler1.clone();
rt.block_on(async move {
for x in 100..108u32 {
let ctx = FileCtx {
req: FileReq { id: x as usize },
resp: FileResp { id: x as usize },
};
fh.send_async(cb1, ctx).await.expect("backpressured send");
}
});
}
std::thread::sleep(Duration::from_millis(1000));
drop(handler1);
drop(handler2);
reactor.shutdown().expect("shutdown");
println!("reactor shut down cleanly");
}