mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-21 04:58:42 +00:00
Run fetchers in parallel in MultiFetcher.first_available
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
d373ad5145
commit
c393270899
2 changed files with 45 additions and 13 deletions
|
@ -4,6 +4,7 @@ use std::sync::Arc;
|
||||||
pub use gh_crate_meta::*;
|
pub use gh_crate_meta::*;
|
||||||
pub use log::debug;
|
pub use log::debug;
|
||||||
pub use quickinstall::*;
|
pub use quickinstall::*;
|
||||||
|
use tokio::task::JoinHandle;
|
||||||
|
|
||||||
use crate::{BinstallError, PkgFmt, PkgMeta};
|
use crate::{BinstallError, PkgFmt, PkgMeta};
|
||||||
|
|
||||||
|
@ -53,22 +54,53 @@ impl MultiFetcher {
|
||||||
self.fetchers.push(fetcher);
|
self.fetchers.push(fetcher);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn first_available(&self) -> Option<&dyn Fetcher> {
|
pub async fn first_available(&self) -> Option<Arc<dyn Fetcher>> {
|
||||||
for fetcher in &self.fetchers {
|
let handles: Vec<_> = self
|
||||||
let available = fetcher.check().await.unwrap_or_else(|err| {
|
.fetchers
|
||||||
debug!(
|
.iter()
|
||||||
"Error while checking fetcher {}: {}",
|
.cloned()
|
||||||
fetcher.source_name(),
|
.map(|fetcher| {
|
||||||
err
|
let fetcher_cloned = fetcher.clone();
|
||||||
);
|
|
||||||
false
|
|
||||||
});
|
|
||||||
|
|
||||||
if available {
|
(
|
||||||
return Some(&**fetcher);
|
AutoAbortJoinHandle(tokio::spawn(async move {
|
||||||
|
fetcher.check().await.unwrap_or_else(|err| {
|
||||||
|
debug!(
|
||||||
|
"Error while checking fetcher {}: {}",
|
||||||
|
fetcher.source_name(),
|
||||||
|
err
|
||||||
|
);
|
||||||
|
false
|
||||||
|
})
|
||||||
|
})),
|
||||||
|
fetcher_cloned,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
for (mut handle, fetcher) in handles {
|
||||||
|
match (&mut handle.0).await {
|
||||||
|
Ok(true) => return Some(fetcher),
|
||||||
|
Err(join_err) => {
|
||||||
|
debug!(
|
||||||
|
"Error while checking fetcher {}: {}",
|
||||||
|
fetcher.source_name(),
|
||||||
|
join_err
|
||||||
|
);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct AutoAbortJoinHandle(JoinHandle<bool>);
|
||||||
|
|
||||||
|
impl Drop for AutoAbortJoinHandle {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.0.abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -234,7 +234,7 @@ async fn entry() -> Result<()> {
|
||||||
Some(fetcher) => {
|
Some(fetcher) => {
|
||||||
install_from_package(
|
install_from_package(
|
||||||
binaries,
|
binaries,
|
||||||
fetcher,
|
&*fetcher,
|
||||||
install_path,
|
install_path,
|
||||||
meta,
|
meta,
|
||||||
opts,
|
opts,
|
||||||
|
|
Loading…
Add table
Reference in a new issue