mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-20 20:48:43 +00:00
Merge pull request #162 from NobodyXu/feature/parallelize-MultiFetcher
This commit is contained in:
commit
20ec8d6359
4 changed files with 54 additions and 21 deletions
|
@ -1,8 +1,10 @@
|
|||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub use gh_crate_meta::*;
|
||||
pub use log::debug;
|
||||
pub use quickinstall::*;
|
||||
use tokio::task::JoinHandle;
|
||||
|
||||
use crate::{BinstallError, PkgFmt, PkgMeta};
|
||||
|
||||
|
@ -10,9 +12,9 @@ mod gh_crate_meta;
|
|||
mod quickinstall;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
pub trait Fetcher {
|
||||
pub trait Fetcher: Send + Sync {
|
||||
/// Create a new fetcher from some data
|
||||
async fn new(data: &Data) -> Box<Self>
|
||||
async fn new(data: &Data) -> Arc<Self>
|
||||
where
|
||||
Self: Sized;
|
||||
|
||||
|
@ -44,30 +46,59 @@ pub struct Data {
|
|||
|
||||
#[derive(Default)]
|
||||
pub struct MultiFetcher {
|
||||
fetchers: Vec<Box<dyn Fetcher>>,
|
||||
fetchers: Vec<Arc<dyn Fetcher>>,
|
||||
}
|
||||
|
||||
impl MultiFetcher {
|
||||
pub fn add(&mut self, fetcher: Box<dyn Fetcher>) {
|
||||
pub fn add(&mut self, fetcher: Arc<dyn Fetcher>) {
|
||||
self.fetchers.push(fetcher);
|
||||
}
|
||||
|
||||
pub async fn first_available(&self) -> Option<&dyn Fetcher> {
|
||||
for fetcher in &self.fetchers {
|
||||
let available = fetcher.check().await.unwrap_or_else(|err| {
|
||||
debug!(
|
||||
"Error while checking fetcher {}: {}",
|
||||
fetcher.source_name(),
|
||||
err
|
||||
);
|
||||
false
|
||||
});
|
||||
pub async fn first_available(&self) -> Option<Arc<dyn Fetcher>> {
|
||||
let handles: Vec<_> = self
|
||||
.fetchers
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(|fetcher| {
|
||||
let fetcher_cloned = fetcher.clone();
|
||||
|
||||
if available {
|
||||
return Some(&**fetcher);
|
||||
(
|
||||
AutoAbortJoinHandle(tokio::spawn(async move { fetcher.check().await })),
|
||||
fetcher_cloned,
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
|
||||
for (mut handle, fetcher) in handles {
|
||||
match (&mut handle.0).await {
|
||||
Ok(Ok(true)) => return Some(fetcher),
|
||||
Ok(Ok(false)) => (),
|
||||
Ok(Err(err)) => {
|
||||
debug!(
|
||||
"Error while checking fetcher {}: {}",
|
||||
fetcher.source_name(),
|
||||
err
|
||||
);
|
||||
}
|
||||
Err(join_err) => {
|
||||
debug!(
|
||||
"Error while checking fetcher {}: {}",
|
||||
fetcher.source_name(),
|
||||
join_err
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct AutoAbortJoinHandle(JoinHandle<Result<bool, BinstallError>>);
|
||||
|
||||
impl Drop for AutoAbortJoinHandle {
|
||||
fn drop(&mut self) {
|
||||
self.0.abort();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
||||
use log::{debug, info, warn};
|
||||
use reqwest::Method;
|
||||
|
@ -22,8 +23,8 @@ impl GhCrateMeta {
|
|||
|
||||
#[async_trait::async_trait]
|
||||
impl super::Fetcher for GhCrateMeta {
|
||||
async fn new(data: &Data) -> Box<Self> {
|
||||
Box::new(Self { data: data.clone() })
|
||||
async fn new(data: &Data) -> Arc<Self> {
|
||||
Arc::new(Self { data: data.clone() })
|
||||
}
|
||||
|
||||
async fn check(&self) -> Result<bool, BinstallError> {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
||||
use log::info;
|
||||
use reqwest::Method;
|
||||
|
@ -17,11 +18,11 @@ pub struct QuickInstall {
|
|||
|
||||
#[async_trait::async_trait]
|
||||
impl super::Fetcher for QuickInstall {
|
||||
async fn new(data: &Data) -> Box<Self> {
|
||||
async fn new(data: &Data) -> Arc<Self> {
|
||||
let crate_name = &data.name;
|
||||
let version = &data.version;
|
||||
let target = &data.target;
|
||||
Box::new(Self {
|
||||
Arc::new(Self {
|
||||
package: format!("{crate_name}-{version}-{target}"),
|
||||
})
|
||||
}
|
||||
|
|
|
@ -234,7 +234,7 @@ async fn entry() -> Result<()> {
|
|||
Some(fetcher) => {
|
||||
install_from_package(
|
||||
binaries,
|
||||
fetcher,
|
||||
&*fetcher,
|
||||
install_path,
|
||||
meta,
|
||||
opts,
|
||||
|
|
Loading…
Add table
Reference in a new issue