forked from mirrors/relay
Don't store Config in State
This commit is contained in:
parent
cf60445972
commit
4b4aaaa0b4
4 changed files with 13 additions and 12 deletions
|
@ -18,7 +18,6 @@ use tracing::info;
|
|||
pub struct State {
|
||||
pub(crate) public_key: RsaPublicKey,
|
||||
private_key: RsaPrivateKey,
|
||||
config: Config,
|
||||
object_cache: Arc<RwLock<LruCache<Url, Url>>>,
|
||||
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<Self, Error> {
|
||||
pub(crate) async fn build(db: Db) -> Result<Self, Error> {
|
||||
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(),
|
||||
|
|
|
@ -102,7 +102,7 @@ impl JobState {
|
|||
config: Config,
|
||||
) -> Self {
|
||||
JobState {
|
||||
requests: state.requests(),
|
||||
requests: state.requests(&config),
|
||||
node_cache: state.node_cache(),
|
||||
db,
|
||||
actors,
|
||||
|
|
|
@ -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(),
|
||||
))
|
||||
|
|
|
@ -214,6 +214,7 @@ impl Requests {
|
|||
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>
|
||||
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<T>(&self, url: &str) -> Result<T, Error>
|
||||
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::<Url>()?;
|
||||
|
||||
|
@ -358,9 +361,10 @@ impl Requests {
|
|||
Ok((content_type, bytes))
|
||||
}
|
||||
|
||||
#[tracing::instrument("Deliver to Inbox")]
|
||||
pub(crate) async fn deliver<T>(&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());
|
||||
|
|
Loading…
Reference in a new issue