Skip to main content

rss_proxy/
fetcher.rs

1use crate::model::Feed;
2use anyhow::{anyhow, Context, Result};
3use reqwest::header::{ETAG, IF_MODIFIED_SINCE, IF_NONE_MATCH, LAST_MODIFIED, USER_AGENT};
4use std::collections::HashMap;
5use std::sync::Arc;
6use std::time::{Duration, Instant};
7use tokio::sync::RwLock;
8use tracing::{debug, info};
9
10#[derive(Clone)]
11struct CacheEntry {
12    feed: Feed,
13    etag: Option<String>,
14    last_modified: Option<String>,
15    fetched_at: Instant,
16}
17
18#[derive(Clone)]
19pub struct FeedFetcher {
20    client: reqwest::Client,
21    cache: Arc<RwLock<HashMap<String, CacheEntry>>>,
22    ttl: Duration,
23}
24
25impl FeedFetcher {
26    pub fn new(ttl_seconds: u64) -> Self {
27        let client = reqwest::Client::builder()
28            .timeout(Duration::from_secs(15))
29            .build()
30            .expect("Failed to initialize reqwest client");
31
32        Self {
33            client,
34            cache: Arc::new(RwLock::new(HashMap::new())),
35            ttl: Duration::from_secs(ttl_seconds),
36        }
37    }
38
39    pub async fn fetch(&self, url: &str) -> Result<Feed> {
40        // 1. Check in-memory cache
41        {
42            let cache = self.cache.read().await;
43            if let Some(entry) = cache.get(url) {
44                if entry.fetched_at.elapsed() < self.ttl {
45                    debug!("Returning cached feed for {}", url);
46                    return Ok(entry.feed.clone());
47                }
48            }
49        }
50
51        // 2. Prepare conditional GET request if we have ETag / Last-Modified
52        let (cached_etag, cached_last_modified) = {
53            let cache = self.cache.read().await;
54            cache
55                .get(url)
56                .map(|e| (e.etag.clone(), e.last_modified.clone()))
57                .unwrap_or((None, None))
58        };
59
60        let mut request = self.client.get(url).header(
61            USER_AGENT,
62            "rss-proxy/0.1.0 (+https://github.com/rust-rss-proxy)",
63        );
64
65        if let Some(ref etag) = cached_etag {
66            request = request.header(IF_NONE_MATCH, etag);
67        }
68        if let Some(ref lm) = cached_last_modified {
69            request = request.header(IF_MODIFIED_SINCE, lm);
70        }
71
72        info!("Fetching upstream feed from: {}", url);
73        let response = request
74            .send()
75            .await
76            .with_context(|| format!("Failed to send request to {}", url))?;
77
78        // 3. Handle 304 Not Modified
79        if response.status() == reqwest::StatusCode::NOT_MODIFIED {
80            debug!("Upstream returned 304 Not Modified for {}", url);
81            let mut cache = self.cache.write().await;
82            if let Some(entry) = cache.get_mut(url) {
83                entry.fetched_at = Instant::now();
84                return Ok(entry.feed.clone());
85            }
86        }
87
88        if !response.status().is_success() {
89            return Err(anyhow!(
90                "Upstream returned HTTP error {} for {}",
91                response.status(),
92                url
93            ));
94        }
95
96        // 4. Capture caching headers
97        let etag = response
98            .headers()
99            .get(ETAG)
100            .and_then(|v| v.to_str().ok())
101            .map(|s| s.to_string());
102
103        let last_modified = response
104            .headers()
105            .get(LAST_MODIFIED)
106            .and_then(|v| v.to_str().ok())
107            .map(|s| s.to_string());
108
109        let bytes = response
110            .bytes()
111            .await
112            .with_context(|| format!("Failed to read response body from {}", url))?;
113
114        // 5. Parse feed with feed-rs
115        let parsed = feed_rs::parser::parse(&bytes[..])
116            .with_context(|| format!("Failed to parse feed XML from {}", url))?;
117
118        let feed = Feed::from_feed_rs(parsed);
119
120        // 6. Update cache
121        {
122            let mut cache = self.cache.write().await;
123            cache.insert(
124                url.to_string(),
125                CacheEntry {
126                    feed: feed.clone(),
127                    etag,
128                    last_modified,
129                    fetched_at: Instant::now(),
130                },
131            );
132        }
133
134        Ok(feed)
135    }
136}