node: Don't needlessly fetch or announce

* We shouldn't announce if no refs were updated.
* We shouldn't blindly fetch if we don't know if we have an item in
  storage.
This commit is contained in:
Alexis Sellier 2023-04-12 14:50:03 +02:00
parent f3d513783c
commit 7cbfde1e9b
No known key found for this signature in database
1 changed files with 20 additions and 9 deletions

View File

@ -623,10 +623,15 @@ where
// We only announce refs here when the fetch wasn't user-requested. This is // We only announce refs here when the fetch wasn't user-requested. This is
// because the user might want to announce his fork, once he has created one, // because the user might want to announce his fork, once he has created one,
// or may choose to not announce anything. // or may choose to not announce anything.
match result {
FetchResult::Success { updated } if !updated.is_empty() => {
if let Err(e) = self.announce_refs(rid, &namespaces) { if let Err(e) = self.announce_refs(rid, &namespaces) {
error!(target: "service", "Failed to announce new refs: {e}"); error!(target: "service", "Failed to announce new refs: {e}");
} }
} }
_ => log::debug!(target: "service", "Nothing to announce, no refs were updated.."),
}
}
// Go back to "idle". // Go back to "idle".
if let Some(s) = self.sessions.get_mut(&remote) { if let Some(s) = self.sessions.get_mut(&remote) {
@ -830,14 +835,19 @@ where
) { ) {
// Only if we do not have the repository locally do we fetch here. // Only if we do not have the repository locally do we fetch here.
// If we do have it, only fetch after receiving a ref announcement. // If we do have it, only fetch after receiving a ref announcement.
if let Ok(true) = self.storage.contains(id) { match self.storage.contains(id) {
Ok(true) => {
// Do nothing. // Do nothing.
} else { }
// We may hit this branch due to an error returned by storage. Ok(false) => {
// We attempt to fetch in case of error because it's likely log::debug!(target: "service", "Missing tracked inventory {id}; initiating fetch..");
// the repository was corrupted, and fetching will fix it.
self.fetch(*id, announcer); self.fetch(*id, announcer);
} }
Err(e) => {
log::error!(target: "service", "Error checking local inventory: {e}");
}
}
} }
} }
} }
@ -1204,6 +1214,7 @@ where
fn sync_and_announce(&mut self) { fn sync_and_announce(&mut self) {
match self.sync_inventory() { match self.sync_inventory() {
// TODO: This will always be `true` because the local clock is ticking.
Ok(updated) => { Ok(updated) => {
if !updated.is_empty() { if !updated.is_empty() {
if let Err(e) = self if let Err(e) = self