Skip to content

Commit fd53671

Browse files
committed
dist: install while downloading
1 parent 6c7b9f1 commit fd53671

File tree

4 files changed

+36
-45
lines changed

4 files changed

+36
-45
lines changed

src/dist/download.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl<'a> DownloadCfg<'a> {
117117
}
118118
}
119119

120-
pub(crate) fn clean(&self, hashes: &[String]) -> Result<()> {
120+
pub(crate) fn clean(&self, hashes: &[impl AsRef<Path>]) -> Result<()> {
121121
for hash in hashes.iter() {
122122
let used_file = self.download_dir.join(hash);
123123
if self.download_dir.join(&used_file).exists() {

src/dist/manifestation.rs

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ mod tests;
77
use std::path::Path;
88

99
use anyhow::{Context, Result, anyhow, bail};
10-
use futures_util::stream::StreamExt;
11-
use std::sync::Arc;
12-
use tokio::sync::Semaphore;
10+
use futures_util::stream::{FuturesUnordered, StreamExt};
1311
use tracing::{info, warn};
1412

1513
use crate::dist::component::{Components, DirectoryPackage, Transaction};
@@ -151,8 +149,6 @@ impl Manifestation {
151149
}
152150

153151
// Download component packages and validate hashes
154-
let mut things_to_install = Vec::new();
155-
let mut things_downloaded = Vec::new();
156152
let components = update
157153
.components_urls_and_hashes(new_manifest)
158154
.map(|res| {
@@ -166,7 +162,6 @@ impl Manifestation {
166162
})
167163
.collect::<Result<Vec<_>>>()?;
168164

169-
let components_len = components.len();
170165
const DEFAULT_CONCURRENT_DOWNLOADS: usize = 2;
171166
let concurrent_downloads = download_cfg
172167
.process
@@ -181,27 +176,6 @@ impl Manifestation {
181176
.and_then(|s| s.parse().ok())
182177
.unwrap_or(DEFAULT_MAX_RETRIES);
183178

184-
info!("downloading component(s)");
185-
let semaphore = Arc::new(Semaphore::new(concurrent_downloads));
186-
let component_stream = tokio_stream::iter(components.into_iter()).map(|bin| {
187-
let sem = semaphore.clone();
188-
async move {
189-
let _permit = sem.acquire().await.unwrap();
190-
bin.download(max_retries).await
191-
}
192-
});
193-
if components_len > 0 {
194-
let results = component_stream
195-
.buffered(components_len)
196-
.collect::<Vec<_>>()
197-
.await;
198-
for result in results {
199-
let (bin, downloaded_file) = result?;
200-
things_downloaded.push(bin.binary.hash.clone());
201-
things_to_install.push((bin, downloaded_file));
202-
}
203-
}
204-
205179
// Begin transaction
206180
let mut tx = Transaction::new(prefix.clone(), tmp_cx, download_cfg.process);
207181

@@ -240,15 +214,34 @@ impl Manifestation {
240214
tx = self.uninstall_component(component, new_manifest, tx, download_cfg.process)?;
241215
}
242216

243-
// Install components
244-
for (component_bin, installer_file) in things_to_install {
245-
tx = component_bin.install(installer_file, tx, self)?;
217+
info!("downloading component(s)");
218+
let mut downloads = FuturesUnordered::new();
219+
let mut component_iter = components.iter();
220+
let mut cleanup_downloads = vec![];
221+
loop {
222+
if downloads.is_empty() && component_iter.len() == 0 {
223+
break;
224+
}
225+
226+
let installable = downloads.next().await.transpose()?;
227+
while component_iter.len() > 0 && downloads.len() < concurrent_downloads {
228+
if let Some(bin) = component_iter.next() {
229+
downloads.push(bin.download(max_retries));
230+
}
231+
}
232+
233+
if let Some((bin, downloaded)) = installable {
234+
cleanup_downloads.push(&bin.binary.hash);
235+
tx = bin.install(downloaded, tx, self)?;
236+
}
246237
}
247238

248239
// Install new distribution manifest
249240
let new_manifest_str = new_manifest.clone().stringify()?;
250241
tx.modify_file(rel_installed_manifest_path)?;
251242
utils::write_file("manifest", &installed_manifest_path, &new_manifest_str)?;
243+
download_cfg.clean(&cleanup_downloads)?;
244+
drop(downloads);
252245

253246
// Write configuration.
254247
//
@@ -269,8 +262,6 @@ impl Manifestation {
269262
// End transaction
270263
tx.commit();
271264

272-
download_cfg.clean(&things_downloaded)?;
273-
274265
Ok(UpdateStatus::Changed)
275266
}
276267

@@ -684,7 +675,7 @@ struct ComponentBinary<'a> {
684675
}
685676

686677
impl<'a> ComponentBinary<'a> {
687-
async fn download(self, max_retries: usize) -> Result<(Self, File)> {
678+
async fn download(&self, max_retries: usize) -> Result<(&Self, File)> {
688679
use tokio_retry::{RetryIf, strategy::FixedInterval};
689680

690681
let url = self.download_cfg.url(&self.binary.url)?;

tests/suite/cli_rustup.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ async fn rustup_stable() {
3535
.with_stderr(snapbox::str![[r#"
3636
info: syncing channel updates for stable-[HOST_TRIPLE]
3737
info: latest update on 2015-01-02 for version 1.1.0 (hash-stable-1.1.0)
38-
info: downloading component[..]
38+
info: removing previous version of component cargo
3939
...
4040
info: cleaning up downloads & tmp directories
4141
@@ -131,15 +131,15 @@ async fn rustup_all_channels() {
131131
.with_stderr(snapbox::str![[r#"
132132
info: syncing channel updates for stable-[HOST_TRIPLE]
133133
info: latest update on 2015-01-02 for version 1.1.0 (hash-stable-1.1.0)
134-
info: downloading component[..]
134+
info: removing previous version of component cargo
135135
...
136136
info: syncing channel updates for beta-[HOST_TRIPLE]
137137
info: latest update on 2015-01-02 for version 1.2.0 (hash-beta-1.2.0)
138-
info: downloading component[..]
138+
info: removing previous version of component cargo
139139
...
140140
info: syncing channel updates for nightly-[HOST_TRIPLE]
141141
info: latest update on 2015-01-02 for version 1.3.0 (hash-nightly-2)
142-
info: downloading component[..]
142+
info: removing previous version of component cargo
143143
...
144144
info: cleaning up downloads & tmp directories
145145
@@ -208,12 +208,12 @@ async fn rustup_some_channels_up_to_date() {
208208
.with_stderr(snapbox::str![[r#"
209209
info: syncing channel updates for stable-[HOST_TRIPLE]
210210
info: latest update on 2015-01-02 for version 1.1.0 (hash-stable-1.1.0)
211-
info: downloading component[..]
211+
info: removing previous version of component cargo
212212
...
213213
info: syncing channel updates for beta-[HOST_TRIPLE]
214214
info: syncing channel updates for nightly-[HOST_TRIPLE]
215215
info: latest update on 2015-01-02 for version 1.3.0 (hash-nightly-2)
216-
info: downloading component[..]
216+
info: removing previous version of component cargo
217217
...
218218
info: cleaning up downloads & tmp directories
219219

tests/suite/cli_rustup_ui/rustup_update_updated.stderr.term.svg

Lines changed: 5 additions & 5 deletions
Loading

0 commit comments

Comments
 (0)