Repository: astral-sh/uv
File: crates/uv-client/src/cached_client.rs (Rust)

Code before:
    ///
    /// See: <https://github.com/TrueLayer/reqwest-middleware/blob/8a494c165734e24c62823714843e1c9347027e8a/reqwest-retry/src/middleware.rs#L137>
    #[instrument(skip_all)]
    pub async fn get_cacheable_with_retry<
        Payload: Cacheable,
        CallBackError: std::error::Error + 'static,
        Callback: AsyncFn(Response) -> Result<Payload, CallBackError>,
    >(
        &self,
        req: Request,
        cache_entry: &CacheEntry,
        cache_control: CacheControl<'_>,
        response_callback: Callback,
    ) -> Result<Payload::Target, CachedClientError<CallBackError>> {
        let mut total_retries = 0;
        let start_time = SystemTime::now();
        let retry_policy = self.uncached().retry_policy();
        loop {
            let fresh_req = req.try_clone().expect("HTTP request must be cloneable");
            let result = self
                .get_cacheable(fresh_req, cache_entry, cache_control, &response_callback)
                .await;

            match result {
                Ok(ok) => return Ok(ok),
                Err(err) => {
                    // Check if the middleware already performed retries
                    total_retries += err.retries().unwrap_or_default();
                    if is_transient_network_error(err.error()) {
                        // If middleware already retried, consider that in our retry budget
                        let retry_decision = retry_policy.should_retry(start_time, total_retries);
                        if let reqwest_retry::RetryDecision::Retry { execute_after } =
                            retry_decision
                        {
                            let duration = execute_after
                                .duration_since(SystemTime::now())
                                .unwrap_or_else(|_| Duration::default());

                            debug!(
                                "Transient failure while handling response from {}; retrying after {:.1}s...",
                                req.url(),
                                duration.as_secs_f32(),
                            );
                            tokio::time::sleep(duration).await;
                            total_retries += 1;
                            continue;
                        }
                    }

                    return if total_retries > 0 {
                        Err(err.with_retries(total_retries))

Review comment (suggestion):
nitpick: I think the second comment doesn't make much sense now the addition (consideration) has moved.

```suggestion
                Err(err) => {
                    // Check if the middleware already performed retries and consider it in our retry budget
                    total_retries += err.retries().unwrap_or_default();
                    if is_transient_network_error(err.error()) {
                        let retry_decision = retry_policy.should_retry(start_time, total_retries);
```

Code after:
        CallBackError: std::error::Error + 'static,
        Callback: AsyncFn(Response) -> Result<Payload, CallBackError>,
    >(
        &self,
        req: Request,
        cache_entry: &CacheEntry,
        cache_control: CacheControl<'_>,
        response_callback: Callback,
    ) -> Result<Payload::Target, CachedClientError<CallBackError>> {
        let mut total_retries = 0;
        let start_time = SystemTime::now();
        let retry_policy = self.uncached().retry_policy();
        loop {
            let fresh_req = req.try_clone().expect("HTTP request must be cloneable");
            let result = self
                .get_cacheable(fresh_req, cache_entry, cache_control, &response_callback)
                .await;

            match result {
                Ok(ok) => return Ok(ok),
                Err(err) => {
                    // Check if the middleware already performed retries
                    total_retries += err.retries();
                    if is_transient_network_error(err.error()) {
                        // If middleware already retried, consider that in our retry budget
                        let retry_decision = retry_policy.should_retry(start_time, total_retries);
                        if let reqwest_retry::RetryDecision::Retry { execute_after } =
                            retry_decision
                        {
                            let duration = execute_after
                                .duration_since(SystemTime::now())
                                .unwrap_or_else(|_| Duration::default());

                            debug!(
                                "Transient failure while handling response from {}; retrying after {:.1}s...",
                                req.url(),
                                duration.as_secs_f32(),
                            );
                            tokio::time::sleep(duration).await;
                            total_retries += 1;
                            continue;
                        }
                    }

                    return if total_retries > 0 {
                        Err(err.with_retries(total_retries))
                    } else {
                        Err(err)
                    };
                }
            }