From 1f21bdb2f920fc638b8581bb1a89af0c066e0874 Mon Sep 17 00:00:00 2001 From: cetra3 Date: Wed, 19 Jul 2023 19:39:04 +0930 Subject: [PATCH] Add http cache for webfingers (#3317) * Add http cache for webfingers * Remove the outgoing cache middleware & adjust the cache headers directive * Use 1h & 3day cache header * Update routes and adjust the cache headers location * revert apub caching --------- Co-authored-by: Dessalines Co-authored-by: Felix Ableitner --- crates/routes/src/feeds.rs | 20 +++++++++++++++----- crates/routes/src/nodeinfo.rs | 16 +++++++++++++--- crates/routes/src/webfinger.rs | 4 ++-- crates/utils/src/cache_header.rs | 22 ++++++++++++++++++++++ crates/utils/src/lib.rs | 1 + 5 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 crates/utils/src/cache_header.rs diff --git a/crates/routes/src/feeds.rs b/crates/routes/src/feeds.rs index a71871abc..49cdb27b5 100644 --- a/crates/routes/src/feeds.rs +++ b/crates/routes/src/feeds.rs @@ -20,7 +20,12 @@ use lemmy_db_views_actor::{ person_mention_view::PersonMentionQuery, structs::{CommentReplyView, PersonMentionView}, }; -use lemmy_utils::{claims::Claims, error::LemmyError, utils::markdown::markdown_to_html}; +use lemmy_utils::{ + cache_header::cache_1hour, + claims::Claims, + error::LemmyError, + utils::markdown::markdown_to_html, +}; use once_cell::sync::Lazy; use rss::{ extension::dublincore::DublinCoreExtensionBuilder, @@ -65,10 +70,15 @@ enum RequestType { } pub fn config(cfg: &mut web::ServiceConfig) { - cfg - .route("/feeds/{type}/{name}.xml", web::get().to(get_feed)) - .route("/feeds/all.xml", web::get().to(get_all_feed)) - .route("/feeds/local.xml", web::get().to(get_local_feed)); + cfg.service( + web::scope("/feeds") + .route("/{type}/{name}.xml", web::get().to(get_feed)) + .route("/all.xml", web::get().to(get_all_feed).wrap(cache_1hour())) + .route( + "/local.xml", + web::get().to(get_local_feed).wrap(cache_1hour()), + ), + ); } static RSS_NAMESPACE: Lazy> = Lazy::new(|| { diff --git a/crates/routes/src/nodeinfo.rs b/crates/routes/src/nodeinfo.rs index ef6544622..f9df94122 100644 --- a/crates/routes/src/nodeinfo.rs +++ b/crates/routes/src/nodeinfo.rs @@ -3,14 +3,24 @@ use anyhow::anyhow; use lemmy_api_common::context::LemmyContext; use lemmy_db_schema::RegistrationMode; use lemmy_db_views::structs::SiteView; -use lemmy_utils::{error::LemmyError, version}; +use lemmy_utils::{ + cache_header::{cache_1hour, cache_3days}, + error::LemmyError, + version, +}; use serde::{Deserialize, Serialize}; use url::Url; pub fn config(cfg: &mut web::ServiceConfig) { cfg - .route("/nodeinfo/2.0.json", web::get().to(node_info)) - .route("/.well-known/nodeinfo", web::get().to(node_info_well_known)); + .route( + "/nodeinfo/2.0.json", + web::get().to(node_info).wrap(cache_1hour()), + ) + .route( + "/.well-known/nodeinfo", + web::get().to(node_info_well_known).wrap(cache_3days()), + ); } async fn node_info_well_known( diff --git a/crates/routes/src/webfinger.rs b/crates/routes/src/webfinger.rs index 72adc9502..e3a0a5615 100644 --- a/crates/routes/src/webfinger.rs +++ b/crates/routes/src/webfinger.rs @@ -8,7 +8,7 @@ use lemmy_db_schema::{ source::{community::Community, person::Person}, traits::ApubActor, }; -use lemmy_utils::error::LemmyError; +use lemmy_utils::{cache_header::cache_3days, error::LemmyError}; use serde::Deserialize; use std::collections::HashMap; use url::Url; @@ -21,7 +21,7 @@ struct Params { pub fn config(cfg: &mut web::ServiceConfig) { cfg.route( ".well-known/webfinger", - web::get().to(get_webfinger_response), + web::get().to(get_webfinger_response).wrap(cache_3days()), ); } diff --git a/crates/utils/src/cache_header.rs b/crates/utils/src/cache_header.rs new file mode 100644 index 000000000..042c943a7 --- /dev/null +++ b/crates/utils/src/cache_header.rs @@ -0,0 +1,22 @@ +use actix_web::middleware::DefaultHeaders; + +/// Adds a cache header to requests +/// +/// Common cache amounts are: +/// * 1 hour = 60s * 60m = `3600` seconds +/// * 3 days = 60s * 60m * 24h * 3d = `259200` seconds +/// +/// Mastodon & other activitypub server defaults to 3d +pub fn cache_header(seconds: usize) -> DefaultHeaders { + DefaultHeaders::new().add(("Cache-Control", format!("public, max-age={seconds}"))) +} + +/// Set a 1 hour cache time +pub fn cache_1hour() -> DefaultHeaders { + cache_header(3600) +} + +/// Set a 3 day cache time +pub fn cache_3days() -> DefaultHeaders { + cache_header(259200) +} diff --git a/crates/utils/src/lib.rs b/crates/utils/src/lib.rs index 9ca427cf9..1ef8a842c 100644 --- a/crates/utils/src/lib.rs +++ b/crates/utils/src/lib.rs @@ -4,6 +4,7 @@ extern crate strum_macros; extern crate smart_default; pub mod apub; +pub mod cache_header; pub mod email; pub mod rate_limit; pub mod settings;