Skip to content

Commit

Permalink
v0.2.9 all always retry
Browse files Browse the repository at this point in the history
  • Loading branch information
Mon-ius committed May 13, 2024
1 parent 808d37c commit 1f39b75
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ EOF
bench:
v0.2.6 Processing time: 4457.868372796s
v0.2.7 Processing time: 4441.127406732s
v0.2.8 Processing time: 4650.917674426s

- https://github.com/hyperium/hyper/issues/1358
- https://github.com/hyperium/hyper/issues/1358#issuecomment-366550636
- https://gist.github.com/klausi/f94b9aff7d36a1cb4ebbca746f0a099f
- https://gist.github.com/mustafaturan/47268d8ad6d56cadda357e4c438f51ca

2 changes: 1 addition & 1 deletion hfd-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hfd-cli"
version = "0.2.8"
version = "0.2.9"
edition = "2021"

[dependencies]
Expand Down
54 changes: 47 additions & 7 deletions hfd-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,36 @@ async fn download_chunk(
Ok(())
}

async fn download_chunk_with_retry(
headers: HeaderMap,
url: String,
path: PathBuf,
s: usize,
e: usize,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
const MAX_RETRIES: usize = 100;
let mut retries = 0;

let mut error: Option<Box<dyn std::error::Error + Send + Sync>> = None;

while retries < MAX_RETRIES {
match download_chunk(headers.clone(), url.clone(), path.clone(), s, e).await {
Ok(_) => return Ok(()),
Err(e) => {
println!("Retry {:#?} with {:?} times", e, retries);
error = Some(e);
retries += 1;
std::thread::sleep(Duration::from_millis(10));
}
}
}

Err(error.unwrap_or_else(|| Box::new(std::io::Error::new(
std::io::ErrorKind::Other,
"Exhausted all retries",
))))
}

async fn download(
headers: HeaderMap,
url: String,
Expand Down Expand Up @@ -135,14 +165,20 @@ async fn download(

for s in (0..length).step_by(chunk_size) {
let e = std::cmp::min(s + chunk_size - 1, length);
tasks.push(download_chunk(headers.clone(), url.clone(), path.clone(), s, e));
tasks.push(download_chunk_with_retry(headers.clone(), url.clone(), path.clone(), s, e));
}

while let Some(handle) = tasks.next().await {
let res = match handle {
Ok(socket) => socket,
Err(e) => println!("{:?}", e),
};
Ok(socket) => {
socket
},
Err(e) => {
println!("Chunk Error {:#?}", e);
std::thread::sleep(Duration::from_millis(10));
continue;
}
};
}
Ok(())
}
Expand Down Expand Up @@ -235,9 +271,13 @@ impl HfClient {

while let Some(handle) = tasks.next().await {
let res = match handle {
Ok(socket) => socket,
Err(e) => println!("{:?}", e),
};
Ok(socket) => socket,
Err(e) => {
println!("File Error {:#?}", e);
std::thread::sleep(Duration::from_millis(10));
continue;
}
};
}
}
Ok(())
Expand Down

0 comments on commit 1f39b75

Please sign in to comment.