|
1 | 1 | #![allow(clippy::blocks_in_conditions)] // TODO: `rustc` 1.80.1 clippy issue |
2 | 2 |
|
3 | | -use std::time::Duration; |
| 3 | +use std::time::{Duration, Instant}; |
4 | 4 |
|
5 | 5 | use async_trait::async_trait; |
6 | 6 | use cache_types::SerializedOffchainLookup; |
@@ -31,6 +31,8 @@ use super::{ |
31 | 31 | mod cache_types; |
32 | 32 |
|
33 | 33 | pub const DEFAULT_TIMEOUT: u64 = 30; |
| 34 | +const FETCH_RETRY_INTERVAL: Duration = Duration::from_secs(3); |
| 35 | +const FETCH_RETRY_BUDGET: Duration = Duration::from_secs(60); |
34 | 36 |
|
35 | 37 | #[derive(Clone, Debug, Serialize)] |
36 | 38 | struct OffchainLookupRequestBody { |
@@ -241,12 +243,28 @@ async fn metadata_build( |
241 | 243 | continue; |
242 | 244 | } |
243 | 245 |
|
244 | | - // if we fail, we want to try the other urls |
245 | | - match fetch_offchain_data(ism_builder, &info, url, origin_tx_hash.clone()).await { |
246 | | - Ok(data) => return Ok(data), |
247 | | - Err(err) => { |
248 | | - tracing::warn!(?ism_address, url, ?err, "Failed to fetch offchain data"); |
249 | | - continue; |
| 246 | + let deadline = Instant::now() + FETCH_RETRY_BUDGET; |
| 247 | + loop { |
| 248 | + match fetch_offchain_data(ism_builder, &info, url, origin_tx_hash.clone()).await { |
| 249 | + Ok(data) => return Ok(data), |
| 250 | + Err(err) => { |
| 251 | + if Instant::now() >= deadline { |
| 252 | + tracing::warn!( |
| 253 | + ?ism_address, |
| 254 | + url, |
| 255 | + ?err, |
| 256 | + "Failed to fetch offchain data, budget exhausted" |
| 257 | + ); |
| 258 | + break; |
| 259 | + } |
| 260 | + tracing::debug!( |
| 261 | + ?ism_address, |
| 262 | + url, |
| 263 | + ?err, |
| 264 | + "Failed to fetch offchain data, retrying" |
| 265 | + ); |
| 266 | + tokio::time::sleep(FETCH_RETRY_INTERVAL).await; |
| 267 | + } |
250 | 268 | } |
251 | 269 | } |
252 | 270 | } |
|
0 commit comments