Skip to content

Commit 7755a21

Browse files
committed
feat: auto-retry on file download
Signed-off-by: Izuna <izuna.seikatsu@ccbluex.net>
1 parent 7f265a6 commit 7755a21

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

src-tauri/src/utils/download.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@
2020
use std::path::Path;
2121

2222
use anyhow::Result;
23+
use backon::{ExponentialBuilder, Retryable};
2324
use tokio::fs;
24-
use tracing::debug;
25+
use tracing::{debug, warn};
2526

2627
use crate::HTTP_CLIENT;
2728

28-
/// Download file using HTTP_CLIENT without any progress tracking
29+
/// Download a file using HTTP_CLIENT without any progress tracking
2930
pub async fn download_file_untracked(url: &str, path: impl AsRef<Path>) -> Result<()> {
3031
let path = path.as_ref().to_owned();
3132
let response = HTTP_CLIENT.get(url).send().await?.error_for_status()?;
@@ -41,11 +42,17 @@ where
4142
{
4243
debug!("Downloading file {:?}", url);
4344

44-
let mut response = HTTP_CLIENT
45-
.get(url.trim())
46-
.send()
47-
.await?
48-
.error_for_status()?;
45+
let mut response = (|| async {
46+
HTTP_CLIENT
47+
.get(url.trim())
48+
.send()
49+
.await?
50+
.error_for_status()
51+
}).retry(ExponentialBuilder::default())
52+
.notify(|err, dur| {
53+
warn!("Failed to download file {}. Retrying in {:?}. Error: {}", url, dur, err);
54+
})
55+
.await?;
4956

5057
debug!("Response received from url");
5158

0 commit comments

Comments
 (0)