Skip to content

Commit

Permalink
feat: cache HTTP GET requests
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Dec 10, 2024
1 parent 2b97226 commit deb2de0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ pub struct InnerContext {

/// Iroh for realtime peer channels.
pub(crate) iroh: Arc<RwLock<Option<Iroh>>>,

/// Cache for HTTP GET requests.
pub(crate) http_cache: parking_lot::RwLock<HashMap<String, crate::net::http::Response>>,
}

/// The state of ongoing process.
Expand Down Expand Up @@ -451,6 +454,7 @@ impl Context {
push_subscriber,
push_subscribed: AtomicBool::new(false),
iroh: Arc::new(RwLock::new(None)),
http_cache: Default::default(),
};

let ctx = Context {
Expand Down
22 changes: 17 additions & 5 deletions src/net/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::net::session::SessionStream;
use crate::net::tls::wrap_rustls;

/// HTTP(S) GET response.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Response {
/// Response body.
pub blob: Vec<u8>,
Expand Down Expand Up @@ -91,8 +91,14 @@ where
}

/// Retrieves the binary contents of URL using HTTP GET request.
pub async fn read_url_blob(context: &Context, url: &str) -> Result<Response> {
let mut url = url.to_string();
pub async fn read_url_blob(context: &Context, original_url: &str) -> Result<Response> {
if let Some(response) = context.http_cache.read().get(original_url) {
info!(context, "Returning {original_url:?} from cache.");
return Ok(response.clone());
}

info!(context, "Not found {original_url:?} in cache.");
let mut url = original_url.to_string();

// Follow up to 10 http-redirects
for _i in 0..10 {
Expand Down Expand Up @@ -139,11 +145,17 @@ pub async fn read_url_blob(context: &Context, url: &str) -> Result<Response> {
});
let body = response.collect().await?.to_bytes();
let blob: Vec<u8> = body.to_vec();
return Ok(Response {
let response = Response {
blob,
mimetype,
encoding,
});
};
info!(context, "Inserting {original_url:?} into cache.");
context
.http_cache
.write()
.insert(original_url.to_string(), response.clone());
return Ok(response);
}

Err(anyhow!("Followed 10 redirections"))
Expand Down

0 comments on commit deb2de0

Please sign in to comment.