Skip to content

Commit

Permalink
Retry RPC errors (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
yeetfield authored Feb 10, 2023
1 parent a342e00 commit fe0dc45
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
12 changes: 5 additions & 7 deletions rozy/src/installers/tarball.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,11 @@ impl Installer for Tarball {

let downloaded_file = std::fs::File::open(file.path())?;
let reader: Box<dyn Read> = match infer::get_from_path(file.path())? {
Some(t) => {
match t.extension() {
"gz" => Box::new(GzDecoder::new(downloaded_file)),
"bz2" => Box::new(BzDecoder::new(downloaded_file)),
"xz" => Box::new(XzDecoder::new(downloaded_file)),
_ => return Err(anyhow!("unsupported archive compression type {}", t)),
}
Some(t) => match t.extension() {
"gz" => Box::new(GzDecoder::new(downloaded_file)),
"bz2" => Box::new(BzDecoder::new(downloaded_file)),
"xz" => Box::new(XzDecoder::new(downloaded_file)),
_ => return Err(anyhow!("unsupported archive compression type {}", t)),
},
_ => return Err(anyhow!("unable to determine archive compression type")),
};
Expand Down
32 changes: 30 additions & 2 deletions rozy/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
use anyhow::Result;

fn is_request_retryable_based_on_error(err: &reqwest::Error) -> bool {
if err.is_timeout() || err.is_connect() {
return true;
}

if err.is_decode() || err.is_body() || err.is_builder() || err.is_request() || err.is_redirect()
{
return false;
}

true
}

pub fn download_to(dest_path: &std::path::PathBuf, url: &str) -> Result<()> {
let tmp_dest_path = dest_path.clone().with_extension("tmp");
let mut dest_file = std::fs::File::create(&tmp_dest_path)?;
let response = reqwest::blocking::get(url)?;
let mut content = std::io::Cursor::new(response.bytes()?);

let mut num_tries = 5;
let mut wait_duration = std::time::Duration::from_millis(200);
let mut response = reqwest::blocking::get(url);
while let Err(err) = &response {
if num_tries < 0 || !is_request_retryable_based_on_error(err) {
break;
}

std::thread::sleep(wait_duration);
wait_duration *= 2;
num_tries -= 1;

response = reqwest::blocking::get(url);
}

let mut content = std::io::Cursor::new(response?.bytes()?);
std::io::copy(&mut content, &mut dest_file)?;
std::fs::rename(tmp_dest_path, dest_path)?;
Ok(())
Expand Down

0 comments on commit fe0dc45

Please sign in to comment.