Skip to content

Commit ce5e694

Browse files
committed
retry crates.io requests when requesting crate-details
1 parent 8b742cb commit ce5e694

File tree

3 files changed

+33
-28
lines changed

3 files changed

+33
-28
lines changed

src/build_queue.rs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@ use crate::db::{delete_crate, Pool};
33
use crate::docbuilder::PackageKind;
44
use crate::error::Result;
55
use crate::storage::Storage;
6-
use crate::utils::{get_config, get_crate_priority, report_error, set_config, ConfigName};
6+
use crate::utils::{get_config, get_crate_priority, report_error, retry, set_config, ConfigName};
77
use crate::{Config, Index, Metrics, RustwideBuilder};
88
use anyhow::Context;
99
use fn_error_context::context;
1010

11-
use tracing::{debug, error, info, warn};
11+
use tracing::{debug, error, info};
1212

1313
use std::collections::HashMap;
1414
use std::sync::Arc;
15-
use std::thread;
16-
use std::time::Duration;
1715

1816
#[derive(Debug, Clone, Eq, PartialEq, serde::Serialize)]
1917
pub(crate) struct QueuedCrate {
@@ -262,27 +260,6 @@ impl BuildQueue {
262260
}
263261
}
264262

265-
fn retry<T>(mut f: impl FnMut() -> Result<T>, max_attempts: u32) -> Result<T> {
266-
for attempt in 1.. {
267-
match f() {
268-
Ok(result) => return Ok(result),
269-
Err(err) => {
270-
if attempt > max_attempts {
271-
return Err(err);
272-
} else {
273-
let sleep_for = 2u32.pow(attempt);
274-
warn!(
275-
"got error on attempt {}, will try again after {}s:\n{:?}",
276-
attempt, sleep_for, err
277-
);
278-
thread::sleep(Duration::from_secs(sleep_for as u64));
279-
}
280-
}
281-
}
282-
}
283-
unreachable!()
284-
}
285-
286263
/// Index methods.
287264
impl BuildQueue {
288265
/// Updates registry index repository and adds new crates into build queue.

src/index/api.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
use crate::{error::Result, utils::retry};
12
use anyhow::{anyhow, Context};
23
use chrono::{DateTime, Utc};
34
use reqwest::header::{HeaderValue, ACCEPT, USER_AGENT};
45
use semver::Version;
56
use serde::Deserialize;
67
use url::Url;
78

8-
use crate::error::Result;
9-
109
const APP_USER_AGENT: &str = concat!(
1110
env!("CARGO_PKG_NAME"),
1211
" ",
@@ -119,7 +118,11 @@ impl Api {
119118
downloads: i32,
120119
}
121120

122-
let response: Response = self.client.get(url).send()?.error_for_status()?.json()?;
121+
let response: Response = retry(
122+
|| Ok(self.client.get(url.clone()).send()?.error_for_status()?),
123+
3,
124+
)?
125+
.json()?;
123126

124127
let version = Version::parse(version)?;
125128
let version = response

src/utils/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ use serde::Serialize;
2727
use tracing::error;
2828
pub(crate) mod sized_buffer;
2929

30+
use std::thread;
31+
use std::time::Duration;
32+
use tracing::warn;
33+
3034
pub(crate) const APP_USER_AGENT: &str = concat!(
3135
env!("CARGO_PKG_NAME"),
3236
" ",
@@ -112,6 +116,27 @@ where
112116
.context("failed to join thread")?
113117
}
114118

119+
pub(crate) fn retry<T>(mut f: impl FnMut() -> Result<T>, max_attempts: u32) -> Result<T> {
120+
for attempt in 1.. {
121+
match f() {
122+
Ok(result) => return Ok(result),
123+
Err(err) => {
124+
if attempt > max_attempts {
125+
return Err(err);
126+
} else {
127+
let sleep_for = 2u32.pow(attempt);
128+
warn!(
129+
"got error on attempt {}, will try again after {}s:\n{:?}",
130+
attempt, sleep_for, err
131+
);
132+
thread::sleep(Duration::from_secs(sleep_for as u64));
133+
}
134+
}
135+
}
136+
}
137+
unreachable!()
138+
}
139+
115140
#[cfg(test)]
116141
mod tests {
117142
use super::*;

0 commit comments

Comments
 (0)