Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor/MTG-1254-json-downloader-service[mtg-144][mtg-526] #415

Draft
wants to merge 22 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add retry mechanism
  • Loading branch information
kstepanovdev committed Feb 25, 2025
commit 40ae4f6131b98eb2a2d9cc67f2d87194303d9e02
79 changes: 79 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions nft_ingester/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ axum = { workspace = true }
rocksdb = { workspace = true }
num-bigint = "0.4"
tracing-test = { workspace = true }
reqwest-middleware = "0.2"
reqwest-retry = "0.4.0"

[dev-dependencies]
setup = { path = "../tests/setup" }
Expand Down
12 changes: 12 additions & 0 deletions nft_ingester/src/metadata_workers/downloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use interface::{
};
use metrics_utils::{red::RequestErrorDurationMetrics, JsonDownloaderMetricsConfig};
use reqwest::ClientBuilder;
use reqwest_middleware::ClientBuilder as MiddlewareClientBuilder;
use reqwest_retry::{policies::ExponentialBackoffBuilder, RetryTransientMiddleware};
use serde_json::Value;
use tokio::{
sync::{
Expand All @@ -23,6 +25,9 @@ use url::Url;

use crate::json_worker::JsonWorker;
pub const CLIENT_TIMEOUT: Duration = Duration::from_secs(30);
pub const MAX_RETRIES: u32 = 3;
pub const MIN_RETRY_INTERVAL: Duration = Duration::from_secs(1);
pub const MAX_RETRY_INTERVAL: Duration = Duration::from_secs(3);

pub struct MetadataDownloader {
pub worker: Arc<JsonWorker>,
Expand Down Expand Up @@ -124,9 +129,16 @@ impl NewJsonDownloader for JsonWorker {
timeout: Duration,
) -> Result<JsonDownloadResult, JsonDownloaderError> {
let start_time = chrono::Utc::now();
let retry_policy = ExponentialBackoffBuilder::default()
.retry_bounds(MIN_RETRY_INTERVAL, MAX_RETRY_INTERVAL)
.build_with_max_retries(MAX_RETRIES);

let client = ClientBuilder::new().timeout(timeout).build().map_err(|e| {
JsonDownloaderError::ErrorDownloading(format!("Failed to create client: {:?}", e))
})?;
let client = MiddlewareClientBuilder::new(client)
.with(RetryTransientMiddleware::new_with_policy(retry_policy))
.build();

// Detect if the URL is an IPFS link
let parsed_url = if download_task.metadata_url.starts_with("ipfs://") {
Expand Down