From 4b4aaaa0b42806b9992e32472db3b4e0e003b125 Mon Sep 17 00:00:00 2001 From: "Aode (Lion)" Date: Tue, 21 Sep 2021 13:26:31 -0500 Subject: [PATCH] Don't store Config in State --- src/data/state.rs | 11 ++++------- src/jobs/mod.rs | 2 +- src/main.rs | 6 +++--- src/requests.rs | 6 +++++- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/data/state.rs b/src/data/state.rs index 373afad..36d4007 100644 --- a/src/data/state.rs +++ b/src/data/state.rs @@ -18,7 +18,6 @@ use tracing::info; pub struct State { pub(crate) public_key: RsaPublicKey, private_key: RsaPrivateKey, - config: Config, object_cache: Arc>>, node_cache: NodeCache, breakers: Breakers, @@ -30,7 +29,6 @@ impl std::fmt::Debug for State { f.debug_struct("State") .field("public_key", &"PublicKey") .field("private_key", &"[redacted]") - .field("config", &self.config) .field("object_cache", &"Object Cache") .field("node_cache", &self.node_cache) .field("breakers", &self.breakers) @@ -44,11 +42,11 @@ impl State { self.node_cache.clone() } - pub(crate) fn requests(&self) -> Requests { + pub(crate) fn requests(&self, config: &Config) -> Requests { Requests::new( - self.config.generate_url(UrlKind::MainKey).to_string(), + config.generate_url(UrlKind::MainKey).to_string(), self.private_key.clone(), - self.config.user_agent(), + config.user_agent(), self.breakers.clone(), ) } @@ -85,7 +83,7 @@ impl State { } #[tracing::instrument(name = "Building state")] - pub(crate) async fn build(config: Config, db: Db) -> Result { + pub(crate) async fn build(db: Db) -> Result { let private_key = if let Ok(Some(key)) = db.private_key().await { info!("Using existing key"); key @@ -107,7 +105,6 @@ impl State { let state = State { public_key, private_key, - config, object_cache: Arc::new(RwLock::new(LruCache::new(1024 * 8))), node_cache: NodeCache::new(db.clone()), breakers: Breakers::default(), diff --git a/src/jobs/mod.rs b/src/jobs/mod.rs index 7c3e775..51e36e5 100644 --- a/src/jobs/mod.rs +++ b/src/jobs/mod.rs @@ -102,7 +102,7 @@ impl JobState { config: Config, ) -> Self { JobState { - requests: state.requests(), + requests: state.requests(&config), node_cache: state.node_cache(), db, actors, diff --git a/src/main.rs b/src/main.rs index c565ade..bdc17ca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -94,7 +94,7 @@ async fn main() -> Result<(), anyhow::Error> { } let media = MediaCache::new(db.clone()); - let state = State::build(config.clone(), db.clone()).await?; + let state = State::build(db.clone()).await?; let actors = ActorCache::new(db.clone()); let job_server = create_server(); @@ -113,7 +113,7 @@ async fn main() -> Result<(), anyhow::Error> { .wrap(TracingLogger::default()) .app_data(web::Data::new(db.clone())) .app_data(web::Data::new(state.clone())) - .app_data(web::Data::new(state.requests())) + .app_data(web::Data::new(state.requests(&config))) .app_data(web::Data::new(actors.clone())) .app_data(web::Data::new(config.clone())) .app_data(web::Data::new(job_server.clone())) @@ -124,7 +124,7 @@ async fn main() -> Result<(), anyhow::Error> { web::resource("/inbox") .wrap(config.digest_middleware()) .wrap(config.signature_middleware( - state.requests(), + state.requests(&config), actors.clone(), state.clone(), )) diff --git a/src/requests.rs b/src/requests.rs index 216195c..191aad7 100644 --- a/src/requests.rs +++ b/src/requests.rs @@ -214,6 +214,7 @@ impl Requests { self.consecutive_errors.swap(0, Ordering::Relaxed); } + #[tracing::instrument(name = "Fetch Json")] pub(crate) async fn fetch_json(&self, url: &str) -> Result where T: serde::de::DeserializeOwned, @@ -221,6 +222,7 @@ impl Requests { self.do_fetch(url, "application/json").await } + #[tracing::instrument(name = "Fetch Activity+Json")] pub(crate) async fn fetch(&self, url: &str) -> Result where T: serde::de::DeserializeOwned, @@ -288,6 +290,7 @@ impl Requests { Ok(serde_json::from_slice(body.as_ref())?) } + #[tracing::instrument(name = "Fetch Bytes")] pub(crate) async fn fetch_bytes(&self, url: &str) -> Result<(String, Bytes), Error> { let parsed_url = url.parse::()?; @@ -358,9 +361,10 @@ impl Requests { Ok((content_type, bytes)) } + #[tracing::instrument("Deliver to Inbox")] pub(crate) async fn deliver(&self, inbox: Url, item: &T) -> Result<(), Error> where - T: serde::ser::Serialize, + T: serde::ser::Serialize + std::fmt::Debug, { if !self.breakers.should_try(&inbox).await { return Err(ErrorKind::Breaker.into());