use fastly::{Error, Request, Response};
use fastly::http::request::PollResult;
use fastly::http::StatusCode;

#[fastly::main]
fn main(req: Request) -> Result<Response, Error> {
  // Define the backend request
  let backend_resp = req.send_async("origin_0")?;

  // Define the timer
  std::thread::sleep(std::time::Duration::from_millis(250));

  match backend_resp.poll() {
    PollResult::Done(Ok(upstream_resp)) => Ok(upstream_resp),
    PollResult::Done(Err(e)) => return Err(e.into()),
    PollResult::Pending(_) => return Ok(Response::from_body("backendResponse took more than 250ms to receive so we ignored it").with_status(StatusCode::GATEWAY_TIMEOUT)),
  }
}