Skip to content

Commit

Permalink
download_sysext: retry to download 20 times at max
Browse files Browse the repository at this point in the history
If HTTP client fails to download, retry to download once in 1000 msec,
up to 20 times.
  • Loading branch information
dongsupark committed Dec 21, 2023
1 parent d85afc2 commit b58a052
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use reqwest::blocking::Client;

use sha2::{Sha256, Digest};

const MAX_DOWNLOAD_RETRY: u32 = 20;

pub struct DownloadResult {
pub hash: omaha::Hash<omaha::Sha256>,
pub data: File,
Expand Down Expand Up @@ -57,17 +59,17 @@ pub fn hash_on_disk_sha256(path: &Path, maxlen: Option<usize>) -> Result<omaha::
Ok(omaha::Hash::from_bytes(hasher.finalize().into()))
}

pub fn download_and_hash<U>(client: &Client, url: U, path: &Path, print_progress: bool) -> Result<DownloadResult>
fn do_download_and_hash<U>(client: &Client, url: U, path: &Path, print_progress: bool) -> Result<DownloadResult>
where
U: reqwest::IntoUrl + Clone,
Url: From<U>,
{
let client_url = url.clone();

#[rustfmt::skip]
let mut res = client.get(url)
let mut res = client.get(url.clone())
.send()
.context(format!("client get and send({:?}) failed", client_url.as_str()))?;
.context(format!("client get & send{:?} failed ", client_url.as_str()))?;

// Redirect was already handled at this point, so there is no need to touch
// response or url again. Simply print info and continue.
Expand Down Expand Up @@ -98,3 +100,14 @@ where
data: file,
})
}

pub fn download_and_hash<U>(client: &Client, url: U, path: &Path, print_progress: bool) -> Result<DownloadResult>
where
U: reqwest::IntoUrl + Clone,
Url: From<U>,
{
crate::retry_loop(
|| do_download_and_hash(client, url.clone(), path, print_progress),
MAX_DOWNLOAD_RETRY,
)
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ pub use download::DownloadResult;
pub use download::download_and_hash;
pub use download::hash_on_disk_sha256;

mod util;
pub use util::retry_loop;

pub mod request;
25 changes: 25 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use core::time::Duration;
use std::thread::sleep;

const RETRY_INTERVAL_MSEC: u64 = 1000;

pub fn retry_loop<F, T, E>(mut func: F, max_tries: u32) -> Result<T, E>
where
F: FnMut() -> Result<T, E>,
{
let mut tries = 0;

loop {
match func() {
ok @ Ok(_) => return ok,
err @ Err(_) => {
tries += 1;

if tries >= max_tries {
return err;
}
sleep(Duration::from_millis(RETRY_INTERVAL_MSEC));
}
}
}
}

0 comments on commit b58a052

Please sign in to comment.