use fastly::{Error, Request};
use std::io::Write;
use std::thread::sleep;
use std::time::Duration;
// Normally, we recommend the use of the #[fastly::main] macro in Rust code
// but in this case using the raw main() function provides the flexibility needed
// to control the delivery of the response more precisely.
fn main() -> Result<(), Error> {
let req = Request::from_client();
let mut resp = req.send("origin_0")?;
let body = resp.take_body();
let mut streamer = resp.stream_to_client();
// The following code will force the client to wait for 0.5 seconds
// before emitting each 1000 bytes of the response.
for chunk in body.into_bytes().as_slice().chunks(1_000) {
let _ = streamer.write(chunk)?;
streamer.flush()?;
sleep(Duration::from_millis(500));
}
Ok(())
}