node: Improve fetch command

Signed-off-by: Alexis Sellier <self@cloudhead.io>
This commit is contained in:
Alexis Sellier 2022-09-04 22:40:42 +02:00
parent ef2d09114a
commit ec865ee9f4
No known key found for this signature in database
1 changed files with 49 additions and 44 deletions

View File

@ -56,6 +56,8 @@ pub enum Event {}
pub enum FetchError { pub enum FetchError {
#[error(transparent)] #[error(transparent)]
Git(#[from] git2::Error), Git(#[from] git2::Error),
#[error(transparent)]
Storage(#[from] storage::Error),
} }
/// Result of looking up providers in our routing table. /// Result of looking up providers in our routing table.
@ -66,6 +68,7 @@ pub enum FetchLookup {
results: chan::Receiver<FetchResult>, results: chan::Receiver<FetchResult>,
}, },
NotFound, NotFound,
Error(FetchError),
} }
/// Result of a fetch request from a specific provider. /// Result of a fetch request from a specific provider.
@ -358,27 +361,33 @@ where
Command::Connect(addr) => self.context.connect(addr), Command::Connect(addr) => self.context.connect(addr),
Command::Fetch(proj, resp) => { Command::Fetch(proj, resp) => {
let providers = self.providers(&proj).collect::<Vec<_>>(); let providers = self.providers(&proj).collect::<Vec<_>>();
let providers = if let Some(providers) = NonEmpty::from_vec(providers) {
providers
} else {
log::error!("No providers found for {}", proj);
resp.send(FetchLookup::NotFound).ok();
if let Some(providers) = NonEmpty::from_vec(providers) { return;
};
log::debug!("Found {} providers for {}", providers.len(), proj); log::debug!("Found {} providers for {}", providers.len(), proj);
let (results_, results) = chan::bounded(providers.len()); let mut repo = match self.storage.repository(&proj) {
{ Ok(repo) => repo,
let (_, head) = &providers.head; Err(err) => {
let tail = providers log::error!("Error opening repo for {}: {}", proj, err);
.tail() resp.send(FetchLookup::Error(err.into())).ok();
.iter()
.map(|(_, peer)| peer.addr)
.collect::<Vec<_>>();
return;
}
};
let (results_, results) = chan::bounded(providers.len());
resp.send(FetchLookup::Found { resp.send(FetchLookup::Found {
providers: NonEmpty::from((head.addr, tail)), providers: providers.clone().map(|(_, peer)| peer.addr),
results, results,
}) })
.ok(); .ok();
}
let mut repo = self.storage.repository(&proj).unwrap();
// TODO: Limit the number of providers we fetch from? Randomize? // TODO: Limit the number of providers we fetch from? Randomize?
for (_, peer) in providers { for (_, peer) in providers {
match repo.fetch(&Url { match repo.fetch(&Url {
@ -402,10 +411,6 @@ where
} }
} }
} }
} else {
log::error!("No providers found for {}", proj);
resp.send(FetchLookup::NotFound).ok();
}
} }
Command::AnnounceRefsUpdate(proj) => { Command::AnnounceRefsUpdate(proj) => {
let user = *self.storage.user_id(); let user = *self.storage.user_id();