Skip to content

Commit 41fe546

Browse files
committed
Retry on some download errors in lintcheck
1 parent 871ad80 commit 41fe546

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

lintcheck/src/main.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ use std::{
1515
env, fmt,
1616
fs::write,
1717
path::{Path, PathBuf},
18+
thread,
19+
time::Duration,
1820
};
1921

2022
use clap::{App, Arg, ArgMatches};
@@ -109,6 +111,22 @@ impl std::fmt::Display for ClippyWarning {
109111
}
110112
}
111113

114+
fn get(path: &str) -> Result<ureq::Response, ureq::Error> {
115+
const MAX_RETRIES: u8 = 4;
116+
let mut retries = 0;
117+
loop {
118+
match ureq::get(path).call() {
119+
Ok(res) => return Ok(res),
120+
Err(e) if retries >= MAX_RETRIES => return Err(e),
121+
Err(ureq::Error::Transport(e)) => eprintln!("Error: {}", e),
122+
Err(e) => return Err(e),
123+
}
124+
eprintln!("retrying in {} seconds...", retries);
125+
thread::sleep(Duration::from_secs(retries as u64));
126+
retries += 1;
127+
}
128+
}
129+
112130
impl CrateSource {
113131
/// Makes the sources available on the disk for clippy to check.
114132
/// Clones a git repo and checks out the specified commit or downloads a crate from crates.io or
@@ -129,7 +147,7 @@ impl CrateSource {
129147
if !krate_file_path.is_file() {
130148
// create a file path to download and write the crate data into
131149
let mut krate_dest = std::fs::File::create(&krate_file_path).unwrap();
132-
let mut krate_req = ureq::get(&url).call().unwrap().into_reader();
150+
let mut krate_req = get(&url).unwrap().into_reader();
133151
// copy the crate into the file
134152
std::io::copy(&mut krate_req, &mut krate_dest).unwrap();
135153

0 commit comments

Comments
 (0)