I have a program which generates data slowly (we can say it's computationally intensive, like computing digits of pi). It produces a lot of data; each response can be 1GiB, will not fit in memory, and must be generated on demand. I'm using hyper to write a web service to generate the content when requested.
Let's skip the boilerplate (service_fn
, Server::bind
).
The API which generates the data slowly might be something like
use std::io;
impl SlowData {
fn new(initial: &str) -> SlowData {
unimplemented!()
}
fn next_block(&self) -> io::Result<&[u8]> {
unimplemented!()
}
}
type ResponseFuture = Box<Future<Item = Response, Error = GenericError> + Send>;
fn run(req: Request) -> ResponseFuture {
// spawn a thread and:
// initialize the generator
// SlowData::new(&req.uri().path());
// spawn a thread and call slow.next_block() until len()==0
// each byte which comes from next_block should go to the client
// as part of the Body
}
Note that SlowData::new
is also computationally intensive.
Optimally, we'd minimize the copies and send that &[u8]
directly to hyper without having to copy it into a Vec
or something.
How do I fulfill a hyper Request's body from a side thread?