Add error handling and retry attempts

Handles cases where the segments fail to download and sometimes get stuck by introducing a timeout and retrying on failure.
This commit is contained in:
Alexandru Dracea 2022-12-28 15:44:45 +02:00 committed by GitHub
parent c5940a240c
commit 240e5563a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -76,8 +76,55 @@ pub async fn download_segments(
let thread_count = count.clone(); let thread_count = count.clone();
join_set.spawn(async move { join_set.spawn(async move {
for (i, segment) in thread_segments.into_iter().enumerate() { for (i, segment) in thread_segments.into_iter().enumerate() {
let response = thread_client.get(&segment.url).send().await?; let response_res = thread_client
let mut buf = response.bytes().await?.to_vec(); .get(&segment.url)
.timeout(Duration::from_secs(60u64))
.send()
.await;
let verfified_response = match response_res {
Ok(x) => x,
Err(y) => panic!("This is likely a netowrking error: {}", y),
};
let possible_error_in_response = verfified_response.bytes().await;
let mut buf = if let Ok(r) = possible_error_in_response {
r.to_vec()
} else {
debug!(
"Segment Failed to download: {}, retrying.",
num + (i * cpus)
);
let mut resp = thread_client
.get(&segment.url)
.timeout(Duration::from_secs(60u64))
.send()
.await
.unwrap()
.bytes()
.await;
if resp.is_err() {
let mut retry_ctr = 1;
loop {
debug!(
"Segment Failed to download: {}, retry {}.",
num + (i * cpus),
retry_ctr
);
resp = thread_client
.get(&segment.url)
.timeout(Duration::from_secs(60u64))
.send()
.await
.unwrap()
.bytes()
.await;
if resp.is_ok() {
break;
}
retry_ctr += 1;
}
}
resp.unwrap().to_vec()
};
buf = VariantSegment::decrypt(buf.borrow_mut(), segment.key)?.to_vec(); buf = VariantSegment::decrypt(buf.borrow_mut(), segment.key)?.to_vec();
debug!( debug!(