Optimize Fetcher::find and fix quickinstall reporting (#579)

* Optimize `Fetcher::find`: Make it non-async to avoid boxing
   and return `AutoAbortJoinHandle<...>` instead.
   
   Since spawning a new task in tokio box the future anyway, boxing the
   returned future again in `Fetcher::find` merely adds unnecessary
   overheads.
* Optimize `QuickInstall::report`: Make it async fn instead of ret `JoinHandle`
   
   This provides several benefits:
    - On debug build, no task will be spawned
    - The calls to `self.stats_url()` can be evaluated in parallel.
    - The task now returns `()` so that the task spawned is smaller.
* Fix `QuickInstall::find`: `warn!` if quickinstall report fails

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-11-30 14:15:22 +11:00 committed by GitHub
parent ff737730f4
commit ab3e47c42b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 93 additions and 84 deletions

View file

@ -1,8 +1,7 @@
use std::{path::Path, sync::Arc};
use compact_str::CompactString;
use tokio::task::JoinHandle;
use tracing::debug;
use tracing::{debug, warn};
use url::Url;
use crate::{
@ -11,6 +10,7 @@ use crate::{
download::Download,
remote::{Client, Method},
signal::wait_on_cancellation_signal,
tasks::AutoAbortJoinHandle,
},
manifests::cargo_toml_binstall::{PkgFmt, PkgMeta},
};
@ -43,14 +43,29 @@ impl super::Fetcher for QuickInstall {
})
}
async fn find(&self) -> Result<bool, BinstallError> {
let url = self.package_url();
self.report();
debug!("Checking for package at: '{url}'");
Ok(self
.client
.remote_exists(Url::parse(&url)?, Method::HEAD)
.await?)
fn find(self: Arc<Self>) -> AutoAbortJoinHandle<Result<bool, BinstallError>> {
AutoAbortJoinHandle::spawn(async move {
if cfg!(debug_assertions) {
debug!("Not sending quickinstall report in debug mode");
} else {
let this = self.clone();
tokio::spawn(async move {
if let Err(err) = this.report().await {
warn!(
"Failed to send quickinstall report for package {}: {err}",
this.package
)
}
});
}
let url = self.package_url();
debug!("Checking for package at: '{url}'");
Ok(self
.client
.remote_exists(Url::parse(&url)?, Method::HEAD)
.await?)
})
}
async fn fetch_and_extract(&self, dst: &Path) -> Result<(), BinstallError> {
@ -110,22 +125,12 @@ impl QuickInstall {
)
}
pub fn report(&self) -> JoinHandle<Result<(), BinstallError>> {
let stats_url = self.stats_url();
let client = self.client.clone();
pub async fn report(&self) -> Result<(), BinstallError> {
let url = Url::parse(&self.stats_url())?;
debug!("Sending installation report to quickinstall ({url})");
tokio::spawn(async move {
if cfg!(debug_assertions) {
debug!("Not sending quickinstall report in debug mode");
return Ok(());
}
self.client.remote_exists(url, Method::HEAD).await?;
let url = Url::parse(&stats_url)?;
debug!("Sending installation report to quickinstall ({url})");
client.remote_exists(url, Method::HEAD).await?;
Ok(())
})
Ok(())
}
}