Don't store Config in State

This commit is contained in:
Aode (Lion) 2021-09-21 13:26:31 -05:00
parent cf60445972
commit 4b4aaaa0b4
4 changed files with 13 additions and 12 deletions

View file

@ -18,7 +18,6 @@ use tracing::info;
pub struct State { pub struct State {
pub(crate) public_key: RsaPublicKey, pub(crate) public_key: RsaPublicKey,
private_key: RsaPrivateKey, private_key: RsaPrivateKey,
config: Config,
object_cache: Arc<RwLock<LruCache<Url, Url>>>, object_cache: Arc<RwLock<LruCache<Url, Url>>>,
node_cache: NodeCache, node_cache: NodeCache,
breakers: Breakers, breakers: Breakers,
@ -30,7 +29,6 @@ impl std::fmt::Debug for State {
f.debug_struct("State") f.debug_struct("State")
.field("public_key", &"PublicKey") .field("public_key", &"PublicKey")
.field("private_key", &"[redacted]") .field("private_key", &"[redacted]")
.field("config", &self.config)
.field("object_cache", &"Object Cache") .field("object_cache", &"Object Cache")
.field("node_cache", &self.node_cache) .field("node_cache", &self.node_cache)
.field("breakers", &self.breakers) .field("breakers", &self.breakers)
@ -44,11 +42,11 @@ impl State {
self.node_cache.clone() self.node_cache.clone()
} }
pub(crate) fn requests(&self) -> Requests { pub(crate) fn requests(&self, config: &Config) -> Requests {
Requests::new( Requests::new(
self.config.generate_url(UrlKind::MainKey).to_string(), config.generate_url(UrlKind::MainKey).to_string(),
self.private_key.clone(), self.private_key.clone(),
self.config.user_agent(), config.user_agent(),
self.breakers.clone(), self.breakers.clone(),
) )
} }
@ -85,7 +83,7 @@ impl State {
} }
#[tracing::instrument(name = "Building state")] #[tracing::instrument(name = "Building state")]
pub(crate) async fn build(config: Config, db: Db) -> Result<Self, Error> { pub(crate) async fn build(db: Db) -> Result<Self, Error> {
let private_key = if let Ok(Some(key)) = db.private_key().await { let private_key = if let Ok(Some(key)) = db.private_key().await {
info!("Using existing key"); info!("Using existing key");
key key
@ -107,7 +105,6 @@ impl State {
let state = State { let state = State {
public_key, public_key,
private_key, private_key,
config,
object_cache: Arc::new(RwLock::new(LruCache::new(1024 * 8))), object_cache: Arc::new(RwLock::new(LruCache::new(1024 * 8))),
node_cache: NodeCache::new(db.clone()), node_cache: NodeCache::new(db.clone()),
breakers: Breakers::default(), breakers: Breakers::default(),

View file

@ -102,7 +102,7 @@ impl JobState {
config: Config, config: Config,
) -> Self { ) -> Self {
JobState { JobState {
requests: state.requests(), requests: state.requests(&config),
node_cache: state.node_cache(), node_cache: state.node_cache(),
db, db,
actors, actors,

View file

@ -94,7 +94,7 @@ async fn main() -> Result<(), anyhow::Error> {
} }
let media = MediaCache::new(db.clone()); 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 actors = ActorCache::new(db.clone());
let job_server = create_server(); let job_server = create_server();
@ -113,7 +113,7 @@ async fn main() -> Result<(), anyhow::Error> {
.wrap(TracingLogger::default()) .wrap(TracingLogger::default())
.app_data(web::Data::new(db.clone())) .app_data(web::Data::new(db.clone()))
.app_data(web::Data::new(state.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(actors.clone()))
.app_data(web::Data::new(config.clone())) .app_data(web::Data::new(config.clone()))
.app_data(web::Data::new(job_server.clone())) .app_data(web::Data::new(job_server.clone()))
@ -124,7 +124,7 @@ async fn main() -> Result<(), anyhow::Error> {
web::resource("/inbox") web::resource("/inbox")
.wrap(config.digest_middleware()) .wrap(config.digest_middleware())
.wrap(config.signature_middleware( .wrap(config.signature_middleware(
state.requests(), state.requests(&config),
actors.clone(), actors.clone(),
state.clone(), state.clone(),
)) ))

View file

@ -214,6 +214,7 @@ impl Requests {
self.consecutive_errors.swap(0, Ordering::Relaxed); self.consecutive_errors.swap(0, Ordering::Relaxed);
} }
#[tracing::instrument(name = "Fetch Json")]
pub(crate) async fn fetch_json<T>(&self, url: &str) -> Result<T, Error> pub(crate) async fn fetch_json<T>(&self, url: &str) -> Result<T, Error>
where where
T: serde::de::DeserializeOwned, T: serde::de::DeserializeOwned,
@ -221,6 +222,7 @@ impl Requests {
self.do_fetch(url, "application/json").await self.do_fetch(url, "application/json").await
} }
#[tracing::instrument(name = "Fetch Activity+Json")]
pub(crate) async fn fetch<T>(&self, url: &str) -> Result<T, Error> pub(crate) async fn fetch<T>(&self, url: &str) -> Result<T, Error>
where where
T: serde::de::DeserializeOwned, T: serde::de::DeserializeOwned,
@ -288,6 +290,7 @@ impl Requests {
Ok(serde_json::from_slice(body.as_ref())?) 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> { pub(crate) async fn fetch_bytes(&self, url: &str) -> Result<(String, Bytes), Error> {
let parsed_url = url.parse::<Url>()?; let parsed_url = url.parse::<Url>()?;
@ -358,9 +361,10 @@ impl Requests {
Ok((content_type, bytes)) Ok((content_type, bytes))
} }
#[tracing::instrument("Deliver to Inbox")]
pub(crate) async fn deliver<T>(&self, inbox: Url, item: &T) -> Result<(), Error> pub(crate) async fn deliver<T>(&self, inbox: Url, item: &T) -> Result<(), Error>
where where
T: serde::ser::Serialize, T: serde::ser::Serialize + std::fmt::Debug,
{ {
if !self.breakers.should_try(&inbox).await { if !self.breakers.should_try(&inbox).await {
return Err(ErrorKind::Breaker.into()); return Err(ErrorKind::Breaker.into());