Optimize MultiFetcher: Start checking ASAP

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-21 13:06:34 +10:00
parent 6180e9ec3e
commit dc8d8ccd88
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -47,30 +47,21 @@ pub struct Data {
pub meta: PkgMeta, pub meta: PkgMeta,
} }
type FetcherJoinHandle = AutoAbortJoinHandle<Result<bool, BinstallError>>;
#[derive(Default)] #[derive(Default)]
pub struct MultiFetcher { pub struct MultiFetcher(Vec<(Arc<dyn Fetcher>, FetcherJoinHandle)>);
fetchers: Vec<Arc<dyn Fetcher>>,
}
impl MultiFetcher { impl MultiFetcher {
pub fn add(&mut self, fetcher: Arc<dyn Fetcher>) { pub fn add(&mut self, fetcher: Arc<dyn Fetcher>) {
self.fetchers.push(fetcher); self.0.push((
}
pub async fn first_available(&self) -> Option<Arc<dyn Fetcher>> {
let handles: Vec<_> = self
.fetchers
.iter()
.cloned()
.map(|fetcher| {
(
fetcher.clone(), fetcher.clone(),
AutoAbortJoinHandle::new(tokio::spawn(async move { fetcher.check().await })), AutoAbortJoinHandle::new(tokio::spawn(async move { fetcher.check().await })),
) ));
}) }
.collect();
for (fetcher, handle) in handles { pub async fn first_available(self) -> Option<Arc<dyn Fetcher>> {
for (fetcher, handle) in self.0 {
match handle.await { match handle.await {
Ok(Ok(true)) => return Some(fetcher), Ok(Ok(true)) => return Some(fetcher),
Ok(Ok(false)) => (), Ok(Ok(false)) => (),