commit 552a60776429c2f61e0cf188d48334e445176d27
parent f943d06302bad9359d690b6bc984ac3f777d6345
Author: egor-achkasov <eaachkasov@gmail.com>
Date: Mon, 23 Feb 2026 17:52:14 +0000
Add retries on thread HTTP GET
Diffstat:
1 file changed, 17 insertions(+), 4 deletions(-)
diff --git a/src/main.rs b/src/main.rs
@@ -9,6 +9,22 @@ use post::Post;
use anyhow::{Context, Ok, Result};
use std::result::Result::Ok as StdOk;
+fn fetch_with_retry(url: &str, attempts: u32) -> Result<String> {
+ for attempt in 1..=attempts {
+ match reqwest::blocking::get(url).and_then(|r| r.text()) {
+ StdOk(text) => return Ok(text),
+ Err(e) => {
+ eprintln!("\n\tHTTP request failed for {url}: {e}");
+ if attempt < attempts {
+ eprintln!("\tWaiting 3 seconds...");
+ std::thread::sleep(std::time::Duration::from_secs(3));
+ }
+ }
+ }
+ }
+ anyhow::bail!("failed to get thread after {attempts} attempts")
+}
+
fn scrape_thread(url: &str, config: &Config) -> Result<Post> {
use std::io::Write;
let t_total = std::time::Instant::now();
@@ -16,10 +32,7 @@ fn scrape_thread(url: &str, config: &Config) -> Result<Post> {
print!("\tGetting thread...");
std::io::stdout().flush().ok();
let t = std::time::Instant::now();
- let html = reqwest::blocking::get(url)
- .with_context(|| format!("HTTP GET failed for {url}"))?
- .text()
- .context("failed to read response body")?;
+ let html = fetch_with_retry(url, 3)?;
println!(" Done ({} ms)", t.elapsed().as_millis());
print!("\tParsing posts...");