mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-21 04:58:42 +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::path::Path;
|
||||||
|
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};
|
||||||
|
|
||||||
|
@ -10,9 +12,9 @@ mod gh_crate_meta;
|
||||||
mod quickinstall;
|
mod quickinstall;
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
pub trait Fetcher {
|
pub trait Fetcher: Send + Sync {
|
||||||
/// Create a new fetcher from some data
|
/// Create a new fetcher from some data
|
||||||
async fn new(data: &Data) -> Box<Self>
|
async fn new(data: &Data) -> Arc<Self>
|
||||||
where
|
where
|
||||||
Self: Sized;
|
Self: Sized;
|
||||||
|
|
||||||
|
@ -44,30 +46,59 @@ pub struct Data {
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct MultiFetcher {
|
pub struct MultiFetcher {
|
||||||
fetchers: Vec<Box<dyn Fetcher>>,
|
fetchers: Vec<Arc<dyn Fetcher>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MultiFetcher {
|
impl MultiFetcher {
|
||||||
pub fn add(&mut self, fetcher: Box<dyn Fetcher>) {
|
pub fn add(&mut self, fetcher: Arc<dyn Fetcher>) {
|
||||||
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 })),
|
||||||
|
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
|
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::path::Path;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use log::{debug, info, warn};
|
use log::{debug, info, warn};
|
||||||
use reqwest::Method;
|
use reqwest::Method;
|
||||||
|
@ -22,8 +23,8 @@ impl GhCrateMeta {
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
impl super::Fetcher for GhCrateMeta {
|
impl super::Fetcher for GhCrateMeta {
|
||||||
async fn new(data: &Data) -> Box<Self> {
|
async fn new(data: &Data) -> Arc<Self> {
|
||||||
Box::new(Self { data: data.clone() })
|
Arc::new(Self { data: data.clone() })
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn check(&self) -> Result<bool, BinstallError> {
|
async fn check(&self) -> Result<bool, BinstallError> {
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use log::info;
|
use log::info;
|
||||||
use reqwest::Method;
|
use reqwest::Method;
|
||||||
|
@ -17,11 +18,11 @@ pub struct QuickInstall {
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
impl super::Fetcher for QuickInstall {
|
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 crate_name = &data.name;
|
||||||
let version = &data.version;
|
let version = &data.version;
|
||||||
let target = &data.target;
|
let target = &data.target;
|
||||||
Box::new(Self {
|
Arc::new(Self {
|
||||||
package: format!("{crate_name}-{version}-{target}"),
|
package: format!("{crate_name}-{version}-{target}"),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -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