diff --git a/src/cargo/core/package.rs b/src/cargo/core/package.rs index 952b7cba2..98d18c2ef 100644 --- a/src/cargo/core/package.rs +++ b/src/cargo/core/package.rs @@ -28,7 +28,7 @@ use crate::ops; use crate::util::config::PackageCacheLock; use crate::util::errors::{CargoResult, HttpNotSuccessful}; use crate::util::interning::InternedString; -use crate::util::network::Retry; +use crate::util::network::retry::Retry; use crate::util::{self, internal, Config, Progress, ProgressStyle}; pub const MANIFEST_PREAMBLE: &str = "\ diff --git a/src/cargo/sources/git/oxide.rs b/src/cargo/sources/git/oxide.rs index 56d7f820b..0270579da 100644 --- a/src/cargo/sources/git/oxide.rs +++ b/src/cargo/sources/git/oxide.rs @@ -29,7 +29,7 @@ pub fn with_retry_and_progress( ) -> CargoResult<()> { std::thread::scope(|s| { let mut progress_bar = Progress::new("Fetch", config); - network::with_retry(config, || { + network::retry::with_retry(config, || { let progress_root: Arc = gix::progress::tree::root::Options { initial_capacity: 10, diff --git a/src/cargo/sources/git/utils.rs b/src/cargo/sources/git/utils.rs index 17da5e595..c7fce1f5e 100644 --- a/src/cargo/sources/git/utils.rs +++ b/src/cargo/sources/git/utils.rs @@ -739,7 +739,7 @@ pub fn with_fetch_options( let ssh_config = config.net_config()?.ssh.as_ref(); let config_known_hosts = ssh_config.and_then(|ssh| ssh.known_hosts.as_ref()); let diagnostic_home_config = config.diagnostic_home_config(); - network::with_retry(config, || { + network::retry::with_retry(config, || { with_authentication(config, url, git_config, |f| { let port = Url::parse(url).ok().and_then(|url| url.port()); let mut last_update = Instant::now(); diff --git a/src/cargo/sources/registry/http_remote.rs b/src/cargo/sources/registry/http_remote.rs index e41c385a6..45c45be72 100644 --- a/src/cargo/sources/registry/http_remote.rs +++ b/src/cargo/sources/registry/http_remote.rs @@ -8,7 +8,7 @@ use crate::sources::registry::download; use crate::sources::registry::MaybeLock; use crate::sources::registry::{LoadResponse, RegistryConfig, RegistryData}; use crate::util::errors::{CargoResult, HttpNotSuccessful}; -use crate::util::network::Retry; +use crate::util::network::retry::Retry; use crate::util::{auth, Config, Filesystem, IntoUrl, Progress, ProgressStyle}; use anyhow::Context; use cargo_util::paths; diff --git a/src/cargo/util/network/mod.rs b/src/cargo/util/network/mod.rs new file mode 100644 index 000000000..76dba470a --- /dev/null +++ b/src/cargo/util/network/mod.rs @@ -0,0 +1,37 @@ +//! Utilities for networking. + +use std::task::Poll; + +pub mod retry; + +pub trait PollExt { + fn expect(self, msg: &str) -> T; +} + +impl PollExt for Poll { + #[track_caller] + fn expect(self, msg: &str) -> T { + match self { + Poll::Ready(val) => val, + Poll::Pending => panic!("{}", msg), + } + } +} + +// When dynamically linked against libcurl, we want to ignore some failures +// when using old versions that don't support certain features. +#[macro_export] +macro_rules! try_old_curl { + ($e:expr, $msg:expr) => { + let result = $e; + if cfg!(target_os = "macos") { + if let Err(e) = result { + warn!("ignoring libcurl {} error: {}", $msg, e); + } + } else { + result.with_context(|| { + anyhow::format_err!("failed to enable {}, is curl not built right?", $msg) + })?; + } + }; +} diff --git a/src/cargo/util/network.rs b/src/cargo/util/network/retry.rs similarity index 83% rename from src/cargo/util/network.rs rename to src/cargo/util/network/retry.rs index 70c38b6d4..be29c0074 100644 --- a/src/cargo/util/network.rs +++ b/src/cargo/util/network/retry.rs @@ -1,22 +1,9 @@ +//! Utilities for retrying a network operation. + use anyhow::Error; use crate::util::errors::{CargoResult, HttpNotSuccessful}; use crate::util::Config; -use std::task::Poll; - -pub trait PollExt { - fn expect(self, msg: &str) -> T; -} - -impl PollExt for Poll { - #[track_caller] - fn expect(self, msg: &str) -> T { - match self { - Poll::Ready(val) => val, - Poll::Pending => panic!("{}", msg), - } - } -} pub struct Retry<'a> { config: &'a Config, @@ -105,7 +92,7 @@ fn maybe_spurious(err: &Error) -> bool { /// # let download_something = || return Ok(()); /// # let config = Config::default().unwrap(); /// use cargo::util::network; -/// let cargo_result = network::with_retry(&config, || download_something()); +/// let cargo_result = network::retry::with_retry(&config, || download_something()); /// ``` pub fn with_retry(config: &Config, mut callback: F) -> CargoResult where @@ -119,24 +106,6 @@ where } } -// When dynamically linked against libcurl, we want to ignore some failures -// when using old versions that don't support certain features. -#[macro_export] -macro_rules! try_old_curl { - ($e:expr, $msg:expr) => { - let result = $e; - if cfg!(target_os = "macos") { - if let Err(e) = result { - warn!("ignoring libcurl {} error: {}", $msg, e); - } - } else { - result.with_context(|| { - anyhow::format_err!("failed to enable {}, is curl not built right?", $msg) - })?; - } - }; -} - #[test] fn with_retry_repeats_the_call_then_works() { use crate::core::Shell;