From 3b07da3d8f2043509effb78f171f1858007cea1b Mon Sep 17 00:00:00 2001 From: clabby Date: Thu, 15 Aug 2024 09:59:56 -0700 Subject: [PATCH] fix(host): Backoff after `MAX_RETRIES` (#429) --- bin/host/src/fetcher/mod.rs | 10 ++++++++++ bin/host/src/server.rs | 5 ++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/bin/host/src/fetcher/mod.rs b/bin/host/src/fetcher/mod.rs index ee1d75f0..d0f94700 100644 --- a/bin/host/src/fetcher/mod.rs +++ b/bin/host/src/fetcher/mod.rs @@ -67,6 +67,8 @@ where /// Get the preimage for the given key. pub async fn get_preimage(&self, key: B256) -> Result> { + const MAX_RETRIES: usize = 32; + trace!(target: "fetcher", "Pre-image requested. Key: {key}"); // Acquire a read lock on the key-value store. @@ -77,12 +79,20 @@ where drop(kv_lock); // Use a loop to keep retrying the prefetch as long as the key is not found + let mut retries = 0; while preimage.is_none() && self.last_hint.is_some() { + if retries >= MAX_RETRIES { + tracing::error!(target: "fetcher", "Max retries exceeded."); + anyhow::bail!("Max retries exceeded."); + } + let hint = self.last_hint.as_ref().expect("Cannot be None"); self.prefetch(hint).await?; let kv_lock = self.kv_store.read().await; preimage = kv_lock.get(key); + + retries += 1; } preimage.ok_or_else(|| anyhow!("Preimage not found.")) diff --git a/bin/host/src/server.rs b/bin/host/src/server.rs index 9e132d3b..be39b2c3 100644 --- a/bin/host/src/server.rs +++ b/bin/host/src/server.rs @@ -63,7 +63,10 @@ where // Spawn tasks for the futures and wait for them to complete. let server = tokio::task::spawn(server_fut); let hint_router = tokio::task::spawn(hinter_fut); - tokio::try_join!(server, hint_router).map_err(|e| anyhow!(e))?; + tokio::select! { + s = server => s.map_err(|e| anyhow!(e))?, + h = hint_router => h.map_err(|e| anyhow!(e))?, + } Ok(()) }