Parallelise <QuickInstall as Fetcher>::find

Check for signature in parallel to the package
This commit is contained in:
Jiahao XU 2024-06-22 06:41:50 +00:00
parent 2aeaa32c68
commit 9fbd62fe76
3 changed files with 67 additions and 16 deletions

View file

@ -1,6 +1,11 @@
use std::sync::{ #![allow(unused)]
atomic::{AtomicBool, Ordering::Relaxed},
Once, use std::{
future::Future,
sync::{
atomic::{AtomicBool, Ordering::Relaxed},
Once,
},
}; };
pub(super) use binstalk_downloader::{ pub(super) use binstalk_downloader::{
@ -76,3 +81,33 @@ pub(super) async fn does_url_exist(
Ok(Box::pin(client.remote_gettable(url.clone())).await?) Ok(Box::pin(client.remote_gettable(url.clone())).await?)
} }
#[derive(Debug)]
pub(super) struct AutoAbortJoinHandle<T>(JoinHandle<T>);
impl<T> AutoAbortJoinHandle<T>
where
T: Send + 'static,
{
pub(super) fn spawn<F>(future: F) -> Self
where
F: Future<Output = T> + Send + 'static,
{
Self(tokio::spawn(future))
}
}
impl<T> Drop for AutoAbortJoinHandle<T> {
fn drop(&mut self) {
self.0.abort();
}
}
impl<T, E> AutoAbortJoinHandle<Result<T, E>>
where
E: Into<FetchError>,
{
pub(super) async fn flattened_join(mut self) -> Result<T, FetchError> {
(&mut self.0).await?.map_err(Into::into)
}
}

View file

@ -6,7 +6,7 @@ use binstalk_downloader::{download::DownloadError, remote::Error as RemoteError}
use binstalk_git_repo_api::gh_api_client::{GhApiError, GhRepo}; use binstalk_git_repo_api::gh_api_client::{GhApiError, GhRepo};
use binstalk_types::cargo_toml_binstall::SigningAlgorithm; use binstalk_types::cargo_toml_binstall::SigningAlgorithm;
use thiserror::Error as ThisError; use thiserror::Error as ThisError;
use tokio::{sync::OnceCell, time::sleep}; use tokio::{sync::OnceCell, task::JoinError, time::sleep};
pub use url::ParseError as UrlParseError; pub use url::ParseError as UrlParseError;
mod gh_crate_meta; mod gh_crate_meta;
@ -70,6 +70,9 @@ pub enum FetchError {
#[error("Failed to verify signature")] #[error("Failed to verify signature")]
InvalidSignature, InvalidSignature,
#[error("Failed to wait for task: {0}")]
TaskJoinError(#[from] JoinError),
} }
impl From<RemoteError> for FetchError { impl From<RemoteError> for FetchError {

View file

@ -118,22 +118,35 @@ impl super::Fetcher for QuickInstall {
return Ok(false); return Ok(false);
} }
if self.signature_policy == SignaturePolicy::Require { let check_signature_policy_task = (self.signature_policy == SignaturePolicy::Require)
.then(|| {
let client = self.client.clone();
let gh_api_client = self.gh_api_client.clone();
let signature_url = self.signature_url.clone();
AutoAbortJoinHandle::spawn(async move {
match does_url_exist(client, gh_api_client, &signature_url).await {
Ok(true) => Ok(()),
_ => Err(FetchError::MissingSignature),
}
})
});
tokio::try_join!(
async {
if let Some(task) = check_signature_policy_task {
task.flattened_join().await
} else {
Ok(())
}
},
does_url_exist( does_url_exist(
self.client.clone(), self.client.clone(),
self.gh_api_client.clone(), self.gh_api_client.clone(),
&self.signature_url, &self.package_url,
) ),
.await
.map_err(|_| FetchError::MissingSignature)?;
}
does_url_exist(
self.client.clone(),
self.gh_api_client.clone(),
&self.package_url,
) )
.await .map(|(_, res)| res)
}) })
} }