Skip to content

Commit

Permalink
fix(host): Backoff after MAX_RETRIES (#429)
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby authored Aug 15, 2024
1 parent ca172fe commit 3b07da3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
10 changes: 10 additions & 0 deletions bin/host/src/fetcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ where

/// Get the preimage for the given key.
pub async fn get_preimage(&self, key: B256) -> Result<Vec<u8>> {
const MAX_RETRIES: usize = 32;

trace!(target: "fetcher", "Pre-image requested. Key: {key}");

// Acquire a read lock on the key-value store.
Expand All @@ -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."))
Expand Down
5 changes: 4 additions & 1 deletion bin/host/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down

0 comments on commit 3b07da3

Please sign in to comment.