From dc8d8ccd8850566276e00d9f7fddf31d54c9c378 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Thu, 21 Jul 2022 13:06:34 +1000 Subject: [PATCH] Optimize `MultiFetcher`: Start `check`ing ASAP Signed-off-by: Jiahao XU --- src/fetchers.rs | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/src/fetchers.rs b/src/fetchers.rs index 237a2b85..dc60dfc6 100644 --- a/src/fetchers.rs +++ b/src/fetchers.rs @@ -47,30 +47,21 @@ pub struct Data { pub meta: PkgMeta, } +type FetcherJoinHandle = AutoAbortJoinHandle>; + #[derive(Default)] -pub struct MultiFetcher { - fetchers: Vec>, -} +pub struct MultiFetcher(Vec<(Arc, FetcherJoinHandle)>); impl MultiFetcher { pub fn add(&mut self, fetcher: Arc) { - self.fetchers.push(fetcher); + self.0.push(( + fetcher.clone(), + AutoAbortJoinHandle::new(tokio::spawn(async move { fetcher.check().await })), + )); } - pub async fn first_available(&self) -> Option> { - let handles: Vec<_> = self - .fetchers - .iter() - .cloned() - .map(|fetcher| { - ( - fetcher.clone(), - AutoAbortJoinHandle::new(tokio::spawn(async move { fetcher.check().await })), - ) - }) - .collect(); - - for (fetcher, handle) in handles { + pub async fn first_available(self) -> Option> { + for (fetcher, handle) in self.0 { match handle.await { Ok(Ok(true)) => return Some(fetcher), Ok(Ok(false)) => (),