From 31a0bdad439a4575c1686f690e9e72bd44dde472 Mon Sep 17 00:00:00 2001 From: Vika Date: Mon, 26 Aug 2024 15:22:29 +0300 Subject: Add HTTP fetcher cache It just does its thing in the background, potentially speeding up things. Maybe I could also use the underlying in-memory cache implementation (Moka) to speed up my database. I heard crates.io got some good results from that. --- src/micropub/mod.rs | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) (limited to 'src/micropub') diff --git a/src/micropub/mod.rs b/src/micropub/mod.rs index 08150d2..8f95085 100644 --- a/src/micropub/mod.rs +++ b/src/micropub/mod.rs @@ -82,7 +82,7 @@ fn populate_reply_context( async fn background_processing( db: D, mf2: serde_json::Value, - http: reqwest::Client, + http: reqwest_middleware::ClientWithMiddleware, ) -> () { // TODO: Post-processing the post (aka second write pass) // - [x] Download rich reply contexts @@ -232,7 +232,7 @@ pub(crate) async fn _post( uid: String, mf2: serde_json::Value, db: D, - http: reqwest::Client, + http: reqwest_middleware::ClientWithMiddleware, jobset: Arc>>, ) -> Result { // Here, we have the following guarantees: @@ -501,7 +501,7 @@ async fn dispatch_body( #[tracing::instrument(skip(db, http))] pub(crate) async fn post( State(db): State, - State(http): State, + State(http): State, State(jobset): State>>>, TypedHeader(content_type): TypedHeader, user: User, @@ -655,7 +655,7 @@ pub fn router() -> axum::routing::Metho where S: Storage + FromRef + 'static, A: AuthBackend + FromRef, - reqwest::Client: FromRef, + reqwest_middleware::ClientWithMiddleware: FromRef, Arc>>: FromRef { axum::routing::get(query::) @@ -758,7 +758,14 @@ mod tests { }; let (uid, mf2) = super::normalize_mf2(post, &user); - let err = super::_post(&user, uid, mf2, db.clone(), reqwest::Client::new(), Arc::new(Mutex::new(tokio::task::JoinSet::new()))) + let err = super::_post( + &user, uid, mf2, db.clone(), + reqwest_middleware::ClientWithMiddleware::new( + reqwest::Client::new(), + Box::default() + ), + Arc::new(Mutex::new(tokio::task::JoinSet::new())) + ) .await .unwrap_err(); @@ -788,7 +795,14 @@ mod tests { }; let (uid, mf2) = super::normalize_mf2(post, &user); - let err = super::_post(&user, uid, mf2, db.clone(), reqwest::Client::new(), Arc::new(Mutex::new(tokio::task::JoinSet::new()))) + let err = super::_post( + &user, uid, mf2, db.clone(), + reqwest_middleware::ClientWithMiddleware::new( + reqwest::Client::new(), + Box::default() + ), + Arc::new(Mutex::new(tokio::task::JoinSet::new())) + ) .await .unwrap_err(); @@ -816,7 +830,14 @@ mod tests { }; let (uid, mf2) = super::normalize_mf2(post, &user); - let res = super::_post(&user, uid, mf2, db.clone(), reqwest::Client::new(), Arc::new(Mutex::new(tokio::task::JoinSet::new()))) + let res = super::_post( + &user, uid, mf2, db.clone(), + reqwest_middleware::ClientWithMiddleware::new( + reqwest::Client::new(), + Box::default() + ), + Arc::new(Mutex::new(tokio::task::JoinSet::new())) + ) .await .unwrap(); -- cgit 1.4.1