From e7c9d83434fd31391b977a20abdeb19e274d49ae Mon Sep 17 00:00:00 2001 From: ApfelTeeSaft <91074565+ApfelTeeSaft@users.noreply.github.com> Date: Wed, 19 Nov 2025 12:05:37 +0100 Subject: [PATCH] update retry logic --- src/downloader.rs | 51 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/src/downloader.rs b/src/downloader.rs index fd4ec54..4448ddb 100644 --- a/src/downloader.rs +++ b/src/downloader.rs @@ -164,10 +164,53 @@ async fn download_file_content( ); } - let mut response = if current_file_size > 0 { - client.get(url).headers(headers).send().await? + // Try to send the request with retry logic + let mut response = match if current_file_size > 0 { + client.get(url).headers(headers).send().await } else { - client.get(url).send().await? + client.get(url).send().await + } { + Ok(resp) => resp, + Err(e) => { + // Request failed before we even got a response + retry_count += 1; + + if retry_count > MAX_RETRIES { + println!( + "{} {} {} Maximum retries ({}) exceeded", + "├╼".cyan().dimmed(), + "Failed".red().bold(), + "✘".red().bold(), + MAX_RETRIES + ); + return Err(e.into()); + } + + let delay = INITIAL_RETRY_DELAY_MS * 2u64.pow(retry_count - 1); + println!( + "{} {} {} Connection error (attempt {}/{}): {}", + "├╼".cyan().dimmed(), + "Retry".yellow().bold(), + "⟳".yellow().bold(), + retry_count, + MAX_RETRIES, + e + ); + println!( + "{} {} Waiting {:.1}s before retry...", + "├╼".cyan().dimmed(), + "Wait".white(), + delay as f64 / 1000.0 + ); + + tokio::time::sleep(tokio::time::Duration::from_millis(delay)).await; + + // Ensure file is ready for next attempt + file.flush()?; + file.seek(SeekFrom::End(0))?; + + continue; // Retry from the top of the loop + } }; let content_length = response.content_length().unwrap_or(0); @@ -261,7 +304,7 @@ async fn download_file_content( let delay = INITIAL_RETRY_DELAY_MS * 2u64.pow(retry_count - 1); println!( - "{} {} {} Network error (attempt {}/{}): {}", + "{} {} {} Download error (attempt {}/{}): {}", "├╼".cyan().dimmed(), "Retry".yellow().bold(), "⟳".yellow().bold(),