Skip to content

Commit b42b6b2

Browse files
committed
dist: drop unnecessary Notifier layer
1 parent 614399b commit b42b6b2

File tree

10 files changed

+73
-82
lines changed

10 files changed

+73
-82
lines changed

src/cli/self_update.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,7 +1279,7 @@ pub(crate) async fn prepare_update(dl_cfg: &DownloadCfg<'_>) -> Result<Option<Pa
12791279
&download_url,
12801280
&setup_path,
12811281
None,
1282-
&dl_cfg.notifier,
1282+
&dl_cfg.tracker,
12831283
dl_cfg.process,
12841284
)
12851285
.await?;
@@ -1305,7 +1305,7 @@ async fn get_available_rustup_version(dl_cfg: &DownloadCfg<'_>) -> Result<String
13051305
&release_file_url,
13061306
&release_file,
13071307
None,
1308-
&dl_cfg.notifier,
1308+
&dl_cfg.tracker,
13091309
dl_cfg.process,
13101310
)
13111311
.await?;

src/cli/self_update/windows.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ pub(crate) async fn try_install_msvc(
277277
&visual_studio_url,
278278
&visual_studio,
279279
None,
280-
&dl_cfg.notifier,
280+
&dl_cfg.tracker,
281281
dl_cfg.process,
282282
)
283283
.await?;

src/diskio/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use std::{fmt::Debug, fs::OpenOptions};
6666

6767
use anyhow::Result;
6868

69-
use crate::dist::download::Notifier;
69+
use crate::dist::download::DownloadTracker;
7070
use crate::process::Process;
7171

7272
/// Carries the implementation specific data for complete file transfers into the executor.
@@ -443,13 +443,13 @@ pub(crate) fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
443443

444444
/// Get the executor for disk IO.
445445
pub(crate) fn get_executor<'a>(
446-
notifier: Option<&'a Notifier>,
446+
tracker: Option<&'a DownloadTracker>,
447447
ram_budget: usize,
448448
process: &Process,
449449
) -> anyhow::Result<Box<dyn Executor + 'a>> {
450450
// If this gets lots of use, consider exposing via the config file.
451451
Ok(match process.io_thread_count()? {
452452
0 | 1 => Box::new(immediate::ImmediateUnpacker::new()),
453-
n => Box::new(threaded::Threaded::new(notifier, n, ram_budget)),
453+
n => Box::new(threaded::Threaded::new(tracker, n, ram_budget)),
454454
})
455455
}

src/diskio/threaded.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use sharded_slab::pool::{OwnedRef, OwnedRefMut};
1515
use tracing::debug;
1616

1717
use super::{CompletedIo, Executor, Item, perform};
18-
use crate::dist::download::{Notification, Notifier};
18+
use crate::dist::download::{DownloadTracker, Notification};
1919

2020
#[derive(Copy, Clone, Debug, Enum)]
2121
pub(crate) enum Bucket {
@@ -99,7 +99,7 @@ impl fmt::Debug for Pool {
9999
pub(crate) struct Threaded<'a> {
100100
n_files: Arc<AtomicUsize>,
101101
pool: threadpool::ThreadPool,
102-
notifier: Option<&'a Notifier>,
102+
tracker: Option<&'a DownloadTracker>,
103103
rx: Receiver<Task>,
104104
tx: Sender<Task>,
105105
vec_pools: EnumMap<Bucket, Pool>,
@@ -109,7 +109,7 @@ pub(crate) struct Threaded<'a> {
109109
impl<'a> Threaded<'a> {
110110
/// Construct a new Threaded executor.
111111
pub(crate) fn new(
112-
notify_handler: Option<&'a Notifier>,
112+
tracker: Option<&'a DownloadTracker>,
113113
thread_count: usize,
114114
ram_budget: usize,
115115
) -> Self {
@@ -168,7 +168,7 @@ impl<'a> Threaded<'a> {
168168
Self {
169169
n_files: Arc::new(AtomicUsize::new(0)),
170170
pool,
171-
notifier: notify_handler,
171+
tracker,
172172
rx,
173173
tx,
174174
vec_pools,
@@ -261,9 +261,9 @@ impl Executor for Threaded<'_> {
261261
// actual handling of data today, we synthesis a data buffer and
262262
// pretend to have bytes to deliver.
263263
let mut prev_files = self.n_files.load(Ordering::Relaxed);
264-
if let Some(notifier) = self.notifier {
265-
notifier.handle(Notification::DownloadFinished(None));
266-
notifier.handle(Notification::DownloadContentLengthReceived(
264+
if let Some(tracker) = self.tracker {
265+
tracker.handle(Notification::DownloadFinished(None));
266+
tracker.handle(Notification::DownloadContentLengthReceived(
267267
prev_files as u64,
268268
None,
269269
));
@@ -282,16 +282,16 @@ impl Executor for Threaded<'_> {
282282
prev_files = current_files;
283283
current_files = self.n_files.load(Ordering::Relaxed);
284284
let step_count = prev_files - current_files;
285-
if let Some(notifier) = self.notifier {
286-
notifier.handle(Notification::DownloadDataReceived(
285+
if let Some(tracker) = self.tracker {
286+
tracker.handle(Notification::DownloadDataReceived(
287287
&buf[0..step_count],
288288
None,
289289
));
290290
}
291291
}
292292
self.pool.join();
293-
if let Some(notifier) = self.notifier {
294-
notifier.handle(Notification::DownloadFinished(None));
293+
if let Some(tracker) = self.tracker {
294+
tracker.handle(Notification::DownloadFinished(None));
295295
}
296296
// close the feedback channel so that blocking reads on it can
297297
// complete. send is atomic, and we know the threads completed from the

src/dist/component/package.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ fn unpack_without_first_dir<R: Read>(
300300
};
301301
let unpack_ram = unpack_ram(IO_CHUNK_SIZE, effective_max_ram, dl_cfg);
302302
let mut io_executor: Box<dyn Executor> =
303-
get_executor(Some(&dl_cfg.notifier), unpack_ram, dl_cfg.process)?;
303+
get_executor(Some(&dl_cfg.tracker), unpack_ram, dl_cfg.process)?;
304304

305305
let mut directories: HashMap<PathBuf, DirStatus> = HashMap::new();
306306
// Path is presumed to exist. Call it a precondition.

src/dist/download.rs

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const UPDATE_HASH_LEN: usize = 20;
2323
pub struct DownloadCfg<'a> {
2424
pub tmp_cx: &'a temp::Context,
2525
pub download_dir: &'a PathBuf,
26-
pub(crate) notifier: Notifier,
26+
pub(crate) tracker: DownloadTracker,
2727
pub process: &'a Process,
2828
}
2929

@@ -33,7 +33,7 @@ impl<'a> DownloadCfg<'a> {
3333
DownloadCfg {
3434
tmp_cx: &cfg.tmp_cx,
3535
download_dir: &cfg.download_dir,
36-
notifier: Notifier::new(cfg.quiet, cfg.process),
36+
tracker: DownloadTracker::new(cfg.quiet, cfg.process),
3737
process: cfg.process,
3838
}
3939
}
@@ -47,7 +47,7 @@ impl<'a> DownloadCfg<'a> {
4747
let target_file = self.download_dir.join(Path::new(hash));
4848

4949
if target_file.exists() {
50-
let cached_result = file_hash(&target_file, &self.notifier)?;
50+
let cached_result = file_hash(&target_file, &self.tracker)?;
5151
if hash == cached_result {
5252
debug!("reusing previously downloaded file");
5353
debug!(url = url.as_ref(), "checksum passed");
@@ -76,7 +76,7 @@ impl<'a> DownloadCfg<'a> {
7676
&partial_file_path,
7777
Some(&mut hasher),
7878
true,
79-
&self.notifier,
79+
&self.tracker,
8080
self.process,
8181
)
8282
.await
@@ -125,7 +125,7 @@ impl<'a> DownloadCfg<'a> {
125125
let hash_url = utils::parse_url(&(url.to_owned() + ".sha256"))?;
126126
let hash_file = self.tmp_cx.new_file()?;
127127

128-
download_file(&hash_url, &hash_file, None, &self.notifier, self.process).await?;
128+
download_file(&hash_url, &hash_file, None, &self.tracker, self.process).await?;
129129

130130
utils::read_file("hash", &hash_file).map(|s| s[0..64].to_owned())
131131
}
@@ -166,7 +166,7 @@ impl<'a> DownloadCfg<'a> {
166166
let file = self.tmp_cx.new_file_with_ext("", ext)?;
167167

168168
let mut hasher = Sha256::new();
169-
download_file(&url, &file, Some(&mut hasher), &self.notifier, self.process).await?;
169+
download_file(&url, &file, Some(&mut hasher), &self.tracker, self.process).await?;
170170
let actual_hash = format!("{:x}", hasher.finalize());
171171

172172
if hash != actual_hash {
@@ -185,22 +185,6 @@ impl<'a> DownloadCfg<'a> {
185185
}
186186
}
187187

188-
pub(crate) struct Notifier {
189-
tracker: Mutex<DownloadTracker>,
190-
}
191-
192-
impl Notifier {
193-
pub(crate) fn new(quiet: bool, process: &Process) -> Self {
194-
Self {
195-
tracker: Mutex::new(DownloadTracker::new(!quiet, process)),
196-
}
197-
}
198-
199-
pub(crate) fn handle(&self, n: Notification<'_>) {
200-
self.tracker.lock().unwrap().handle_notification(&n);
201-
}
202-
}
203-
204188
/// Tracks download progress and displays information about it to a terminal.
205189
///
206190
/// *not* safe for tracking concurrent downloads yet - it is basically undefined
@@ -214,7 +198,7 @@ pub(crate) struct DownloadTracker {
214198
/// the message "retrying download" for at least a second.
215199
/// Without it, the progress bar would reappear immediately, not allowing the user to
216200
/// correctly see the message, before the progress bar starts again.
217-
file_progress_bars: HashMap<String, (ProgressBar, Option<Instant>)>,
201+
file_progress_bars: Mutex<HashMap<String, (ProgressBar, Option<Instant>)>>,
218202
}
219203

220204
impl DownloadTracker {
@@ -228,12 +212,12 @@ impl DownloadTracker {
228212

229213
Self {
230214
multi_progress_bars,
231-
file_progress_bars: HashMap::new(),
215+
file_progress_bars: Mutex::new(HashMap::new()),
232216
}
233217
}
234218

235-
pub(crate) fn handle_notification(&mut self, n: &Notification<'_>) {
236-
match *n {
219+
pub(crate) fn handle(&self, n: Notification<'_>) {
220+
match n {
237221
Notification::DownloadContentLengthReceived(content_len, url) => {
238222
if let Some(url) = url {
239223
self.content_length_received(content_len, url);
@@ -263,7 +247,7 @@ impl DownloadTracker {
263247
}
264248

265249
/// Creates a new ProgressBar for the given component.
266-
pub(crate) fn create_progress_bar(&mut self, component: String, url: String) {
250+
pub(crate) fn create_progress_bar(&self, component: String, url: String) {
267251
let pb = ProgressBar::hidden();
268252
pb.set_style(
269253
ProgressStyle::with_template(
@@ -274,20 +258,24 @@ impl DownloadTracker {
274258
);
275259
pb.set_message(component);
276260
self.multi_progress_bars.add(pb.clone());
277-
self.file_progress_bars.insert(url, (pb, None));
261+
self.file_progress_bars
262+
.lock()
263+
.unwrap()
264+
.insert(url, (pb, None));
278265
}
279266

280267
/// Sets the length for a new ProgressBar and gives it a style.
281-
pub(crate) fn content_length_received(&mut self, content_len: u64, url: &str) {
282-
if let Some((pb, _)) = self.file_progress_bars.get(url) {
268+
pub(crate) fn content_length_received(&self, content_len: u64, url: &str) {
269+
if let Some((pb, _)) = self.file_progress_bars.lock().unwrap().get(url) {
283270
pb.reset();
284271
pb.set_length(content_len);
285272
}
286273
}
287274

288275
/// Notifies self that data of size `len` has been received.
289-
pub(crate) fn data_received(&mut self, len: usize, url: &str) {
290-
let Some((pb, retry_time)) = self.file_progress_bars.get_mut(url) else {
276+
pub(crate) fn data_received(&self, len: usize, url: &str) {
277+
let mut map = self.file_progress_bars.lock().unwrap();
278+
let Some((pb, retry_time)) = map.get_mut(url) else {
291279
return;
292280
};
293281
pb.inc(len as u64);
@@ -305,8 +293,9 @@ impl DownloadTracker {
305293
}
306294

307295
/// Notifies self that the download has finished.
308-
pub(crate) fn download_finished(&mut self, url: &str) {
309-
let Some((pb, _)) = self.file_progress_bars.get(url) else {
296+
pub(crate) fn download_finished(&self, url: &str) {
297+
let map = self.file_progress_bars.lock().unwrap();
298+
let Some((pb, _)) = map.get(url) else {
310299
return;
311300
};
312301
pb.set_style(
@@ -317,8 +306,9 @@ impl DownloadTracker {
317306
}
318307

319308
/// Notifies self that the download has failed.
320-
pub(crate) fn download_failed(&mut self, url: &str) {
321-
let Some((pb, _)) = self.file_progress_bars.get(url) else {
309+
pub(crate) fn download_failed(&self, url: &str) {
310+
let map = self.file_progress_bars.lock().unwrap();
311+
let Some((pb, _)) = map.get(url) else {
322312
return;
323313
};
324314
pb.set_style(
@@ -329,8 +319,9 @@ impl DownloadTracker {
329319
}
330320

331321
/// Notifies self that the download is being retried.
332-
pub(crate) fn retrying_download(&mut self, url: &str) {
333-
let Some((pb, retry_time)) = self.file_progress_bars.get_mut(url) else {
322+
pub(crate) fn retrying_download(&self, url: &str) {
323+
let mut map = self.file_progress_bars.lock().unwrap();
324+
let Some((pb, retry_time)) = map.get_mut(url) else {
334325
return;
335326
};
336327
*retry_time = Some(Instant::now());
@@ -354,9 +345,9 @@ pub(crate) enum Notification<'a> {
354345
DownloadFailed(&'a str),
355346
}
356347

357-
fn file_hash(path: &Path, notifier: &Notifier) -> Result<String> {
348+
fn file_hash(path: &Path, tracker: &DownloadTracker) -> Result<String> {
358349
let mut hasher = Sha256::new();
359-
let mut downloaded = utils::FileReaderWithProgress::new_file(path, notifier)?;
350+
let mut downloaded = utils::FileReaderWithProgress::new_file(path, tracker)?;
360351
use std::io::Read;
361352
let mut buf = vec![0; 32768];
362353
while let Ok(n) = downloaded.read(&mut buf) {

src/dist/manifestation.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl Manifestation {
178178
info!("downloading component(s)");
179179
for bin in &components {
180180
download_cfg
181-
.notifier
181+
.tracker
182182
.handle(Notification::DownloadingComponent(
183183
&bin.component.short_name(new_manifest),
184184
&bin.binary.url,
@@ -278,7 +278,7 @@ impl Manifestation {
278278
}
279279

280280
let reader =
281-
utils::FileReaderWithProgress::new_file(&installer_file, &download_cfg.notifier)?;
281+
utils::FileReaderWithProgress::new_file(&installer_file, &download_cfg.tracker)?;
282282
let package = match format {
283283
CompressionKind::GZip => &TarGzPackage::new(reader, download_cfg)? as &dyn Package,
284284
CompressionKind::XZ => &TarXzPackage::new(reader, download_cfg)?,
@@ -444,7 +444,7 @@ impl Manifestation {
444444
.replace(DEFAULT_DIST_SERVER, dl_cfg.tmp_cx.dist_server.as_str());
445445

446446
dl_cfg
447-
.notifier
447+
.tracker
448448
.handle(Notification::DownloadingComponent("rust", &url));
449449

450450
let dl = dl_cfg
@@ -468,7 +468,7 @@ impl Manifestation {
468468
}
469469

470470
// Install all the components in the installer
471-
let reader = utils::FileReaderWithProgress::new_file(&installer_file, &dl_cfg.notifier)?;
471+
let reader = utils::FileReaderWithProgress::new_file(&installer_file, &dl_cfg.tracker)?;
472472
let package: &dyn Package = &TarGzPackage::new(reader, dl_cfg)?;
473473
for component in package.components() {
474474
tx = package.install(&self.installation, &component, None, tx)?;
@@ -750,7 +750,7 @@ impl<'a> ComponentBinary<'a> {
750750
Some(RustupError::BrokenPartialFile)
751751
| Some(RustupError::DownloadingFile { .. }) => {
752752
download_cfg
753-
.notifier
753+
.tracker
754754
.handle(Notification::RetryingDownload(url.as_str()));
755755
true
756756
}

src/dist/manifestation/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use url::Url;
1616
use crate::{
1717
dist::{
1818
DEFAULT_DIST_SERVER, Profile, TargetTriple, ToolchainDesc,
19-
download::{DownloadCfg, Notifier},
19+
download::{DownloadCfg, DownloadTracker},
2020
manifest::{Component, Manifest},
2121
manifestation::{Changes, Manifestation, UpdateStatus},
2222
prefix::InstallPrefix,
@@ -482,7 +482,7 @@ impl TestContext {
482482
let dl_cfg = DownloadCfg {
483483
tmp_cx: &self.tmp_cx,
484484
download_dir: &self.download_dir,
485-
notifier: Notifier::new(false, &self.tp.process),
485+
tracker: DownloadTracker::new(false, &self.tp.process),
486486
process: &self.tp.process,
487487
};
488488

@@ -493,7 +493,7 @@ impl TestContext {
493493
&manifest_url,
494494
&manifest_file,
495495
None,
496-
&dl_cfg.notifier,
496+
&dl_cfg.tracker,
497497
dl_cfg.process,
498498
)
499499
.await?;

0 commit comments

Comments
 (0)