Skip to content

Commit 153405c

Browse files
feat(process): add a method to retrieve the number of concurrent channels
This change is justified since this environment variable is read in more than one place.
1 parent d5c884e commit 153405c

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

src/cli/rustup_mode.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::{
33
env::consts::EXE_SUFFIX,
44
fmt,
55
io::{self, Write},
6-
num::NonZeroU64,
76
path::{Path, PathBuf},
87
process::ExitStatus,
98
str::FromStr,
@@ -800,11 +799,9 @@ async fn check_updates(cfg: &Cfg<'_>, opts: CheckOpts) -> Result<utils::ExitCode
800799
let use_colors = matches!(t.color_choice(), ColorChoice::Auto | ColorChoice::Always);
801800
let mut update_available = false;
802801
let channels = cfg.list_channels()?;
803-
let num_channels = match cfg.process.var("RUSTUP_CONCURRENT_DOWNLOADS") {
804-
Ok(s) => NonZeroU64::from_str(&s).context(
805-
"invalid value in RUSTUP_CONCURRENT_DOWNLOADS -- must be a natural number greater than zero"
806-
)?.get() as usize,
807-
Err(_) => channels.len(),
802+
let num_channels = match cfg.process.concurrent_downloads() {
803+
Some(n) => n,
804+
None => channels.len(),
808805
};
809806

810807
// Ensure that `.buffered()` is never called with 0 as this will cause a hang.

src/dist/manifestation.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
#[cfg(test)]
55
mod tests;
66

7-
use std::num::NonZeroU64;
87
use std::path::Path;
9-
use std::str::FromStr;
108

119
use anyhow::{Context, Result, anyhow, bail};
1210
use futures_util::stream::StreamExt;
@@ -156,11 +154,9 @@ impl Manifestation {
156154
let mut things_to_install: Vec<(Component, CompressionKind, File)> = Vec::new();
157155
let mut things_downloaded: Vec<String> = Vec::new();
158156
let components = update.components_urls_and_hashes(new_manifest)?;
159-
let num_channels = match download_cfg.process.var("RUSTUP_CONCURRENT_DOWNLOADS") {
160-
Ok(s) => NonZeroU64::from_str(&s).context(
161-
"invalid value in RUSTUP_CONCURRENT_DOWNLOADS -- must be a natural number greater than zero"
162-
)?.get() as usize,
163-
Err(_) => components.len(),
157+
let num_channels = match download_cfg.process.concurrent_downloads() {
158+
Some(n) => n,
159+
None => components.len(),
164160
};
165161

166162
const DEFAULT_MAX_RETRIES: usize = 3;

src/process.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::ffi::OsString;
22
use std::fmt::Debug;
33
use std::io;
44
use std::io::IsTerminal;
5+
use std::num::NonZeroU64;
56
use std::path::PathBuf;
67
use std::str::FromStr;
78
#[cfg(feature = "test")]
@@ -167,6 +168,15 @@ impl Process {
167168
_ => ProgressDrawTarget::hidden(),
168169
}
169170
}
171+
172+
pub fn concurrent_downloads(&self) -> Option<usize> {
173+
match self.var("RUSTUP_CONCURRENT_DOWNLOADS") {
174+
Ok(s) => Some(NonZeroU64::from_str(&s).context(
175+
"invalid value in RUSTUP_CONCURRENT_DOWNLOADS -- must be a natural number greater than zero"
176+
).ok()?.get() as usize),
177+
Err(_) => None,
178+
}
179+
}
170180
}
171181

172182
impl home::env::Env for Process {

0 commit comments

Comments
 (0)