From 13c734019df85bbcb8659f2309b7c2c6be39b3fa Mon Sep 17 00:00:00 2001 From: philipp Date: Sun, 18 Feb 2024 17:00:15 +0100 Subject: [PATCH] use retry mechanism for fetching data --- src/misc.rs | 33 +++++++++++++++++++++++++++++++++ src/paragraph/mod.rs | 4 ++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/misc.rs b/src/misc.rs index cdd8810..40b78ab 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -110,3 +110,36 @@ fn delete_all_in_dir>(dir_path: P) -> std::io::Result<()> { Ok(()) } + +pub(crate) fn fetch_with_retries(url: &str) -> Result { + let mut attempts = 0; + let max_attempts = 100; + + loop { + match ureq::get(url).call() { + Ok(response) => { + // If the request is successful, return the response body + match response.into_string() { + Ok(d) => return Ok(d), + Err(e) => return Err(e.into()), + } + } + Err(ureq::Error::Transport(_)) => { + // Increment the attempt counter + attempts += 1; + + // Check if the maximum number of attempts has been reached + if attempts >= max_attempts { + return Err(Error::new("Maximum attempts reached.")); + } + + // TODO: Implement a backoff strategy here. As a simple example, we sleep for 1 second. + std::thread::sleep(std::time::Duration::from_secs(1)); + } + Err(e) => { + // For non-transport errors, return the error immediately. + return Err(e.into()); + } + } + } +} diff --git a/src/paragraph/mod.rs b/src/paragraph/mod.rs index f1423f8..bd17e7e 100644 --- a/src/paragraph/mod.rs +++ b/src/paragraph/mod.rs @@ -29,7 +29,7 @@ use log::info; use crate::{ law::LawBuilder, - misc::{get_cache_dir, Error}, + misc::{fetch_with_retries, get_cache_dir, Error}, }; use self::parser::Risdok; @@ -138,7 +138,7 @@ fn fetch(url: &str) -> Result { Ok(data) => Ok(data), Err(_) => { info!("Not finding url {url} in the cache, downloading..."); - let data = ureq::get(url).call()?.into_string()?; + let data = fetch_with_retries(url)?; let path = Path::new(&expected_filename); if let Some(parent) = path.parent() { // Try to create the directory (and any necessary parent directories)