async fn handle_cache_miss(
    uid: &str,
    gs: &Arc<GlobalState>,
    key: ImageKey,
    req_start: Timer,
) -> HttpResponse {
    // poll upstream, finding the total time of the request
    let res = {
        let timer = Timer::start();
        let res = start_poll_upstream_retry(&gs.backend, &key, 3).await;
        log::debug!("({}) upstream TTFB: {}", uid, timer);
        gs.metrics
            .upstream_ttfb_seconds
            .observe(timer.elapsed_secs() as f64);
        res
    };
    // handle any errors that happen with res
    let res = match res {
        Ok(res) => res,
        Err(e) => {
            log::error!("unexpected upstream error before download ({})", e);
            gs.metrics.failed_requests_total.inc();
            return HttpResponse::BadGateway().body("unexpected upstream response");
        }
    };

    // error handling for the status, make sure it's 200 OK
    match res.status {
        StatusCode::OK => {}
        StatusCode::NOT_FOUND => return HttpResponse::NotFound().finish(),
        status => {
            log::error!("unexpected upstream status ({})", status);
            gs.metrics.failed_requests_total.inc();
            return HttpResponse::BadGateway()
                .body(format!("invalid upstream status code: {}", status));
        }
    }

    // create the chunk stream
    let chunked = ChunkedUpstreamPoll::new(
        gs,
        key,
        res.content_type.clone(),
        res.stream,
        res.size_hint.unwrap_or(0),
        req_start,
    );

    // proxy the image to the client
    HttpResponse::Ok()
        .append_header(header::ContentType(res.content_type))
        .append_header(header::LastModified(res.last_modified))
        .streaming(chunked)
}
/// Handles a cache MISS by requesting the image from the upstream and streaming the image to the
/// user using [`ChunkedUpstreamPoll`]
///
/// If polling from upstream fails, then it will automatically return 502 BAD GATEWAY to the user
/// with the error as the body.
Handles a cache MISS by requesting the image from the upstream and streaming the image to the
user using [`ChunkedUpstreamPoll`]

If polling from upstream fails, then it will automatically return 502 BAD GATEWAY to the user
with the error as the body.
Handles a cache MISS by requesting the image from the upstream and streaming the image to the user using [ ` ChunkedUpstreamPoll ` ] If polling from upstream fails then it will automatically return 502 BAD GATEWAY to the user with the error as the body .
async fn handle_cache_miss(
    uid: &str,
    gs: &Arc<GlobalState>,
    key: ImageKey,
    req_start: Timer,
) -> HttpResponse {
    // poll upstream, finding the total time of the request
    let res = {
        let timer = Timer::start();
        let res = start_poll_upstream_retry(&gs.backend, &key, 3).await;
        log::debug!("({}) upstream TTFB: {}", uid, timer);
        gs.metrics
            .upstream_ttfb_seconds
            .observe(timer.elapsed_secs() as f64);
        res
    };
    // handle any errors that happen with res
    let res = match res {
        Ok(res) => res,
        Err(e) => {
            log::error!("unexpected upstream error before download ({})", e);
            gs.metrics.failed_requests_total.inc();
            return HttpResponse::BadGateway().body("unexpected upstream response");
        }
    };
    // error handling for the status, make sure it's 200 OK
    match res.status {
        StatusCode::OK => {}
        StatusCode::NOT_FOUND => return HttpResponse::NotFound().finish(),
        status => {
            log::error!("unexpected upstream status ({})", status);
            gs.metrics.failed_requests_total.inc();
            return HttpResponse::BadGateway()
                .body(format!("invalid upstream status code: {}", status));
        }
    }
    // create the chunk stream
    let chunked = ChunkedUpstreamPoll::new(
        gs,
        key,
        res.content_type.clone(),
        res.stream,
        res.size_hint.unwrap_or(0),
        req_start,
    );
    // proxy the image to the client
    HttpResponse::Ok()
        .append_header(header::ContentType(res.content_type))
        .append_header(header::LastModified(res.last_modified))
        .streaming(chunked)
}
async fn handle_cache_miss ( uid : & str , gs : & Arc < GlobalState > , key : ImageKey , req_start : Timer , ) -> HttpResponse { // poll upstream, finding the total time of the request let res = { let timer = Timer :: start ( ) ; let res = start_poll_upstream_retry ( & gs . backend , & key , 3 ) . await ; log :: debug ! ( " " uid timer ) ; gs . metrics . upstream_ttfb_seconds . observe ( timer . elapsed_secs ( ) as f64 ) ; res } ; // handle any errors that happen with res let res = match res { Ok ( res ) => res , Err ( e ) => { log :: error ! ( " " e ) ; gs . metrics . failed_requests_total . inc ( ) ; return HttpResponse :: BadGateway ( ) . body ( " " ) ; } } ; // error handling for the status, make sure it's 200 OK match res . status { StatusCode :: OK => { } StatusCode :: NOT_FOUND => return HttpResponse :: NotFound ( ) . finish ( ) , status => { log :: error ! ( " " status ) ; gs . metrics . failed_requests_total . inc ( ) ; return HttpResponse :: BadGateway ( ) . body ( format ! ( " " status ) ) ; } } // create the chunk stream let chunked = ChunkedUpstreamPoll :: new ( gs , key , res . content_type . clone ( ) , res . stream , res . size_hint . unwrap_or ( 0 ) , req_start , ) ; // proxy the image to the client HttpResponse :: Ok ( ) . append_header ( header :: ContentType ( res . content_type ) ) . append_header ( header :: LastModified ( res . last_modified ) ) . streaming ( chunked ) }
Handles a cache MISS by requesting the image from the upstream and streaming the image to the
user using [`ChunkedUpstreamPoll`]
Handles a cache MISS by requesting the image from the upstream and streaming the image to the user using [ ` ChunkedUpstreamPoll ` ]
uid
&str
gs
&Arc<GlobalState>
key
ImageKey
req_start
Timer