Skip to content

Commit

Permalink
share http client throughout kitsune (where possible and useful) to m…
Browse files Browse the repository at this point in the history
…aximize pooling effectiveness
  • Loading branch information
aumetra committed Nov 13, 2024
1 parent 2a3d7eb commit 857fd3b
Show file tree
Hide file tree
Showing 27 changed files with 124 additions and 102 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions crates/kitsune-activitypub/src/deliverer/core.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use futures_util::{stream::FuturesUnordered, Stream, StreamExt};
use http::{Method, Request};
use kitsune_core::consts::USER_AGENT;
use kitsune_db::model::{account::Account, user::User};
use kitsune_error::{Error, Result};
use kitsune_federation_filter::FederationFilter;
Expand All @@ -17,8 +16,7 @@ use url::Url;
/// Does not need to be Arc wrapped for cheap cloning. It's inherently cheap to clone.
#[derive(Clone, TypedBuilder)]
pub struct Deliverer {
#[builder(default = Client::builder().user_agent(USER_AGENT).unwrap().build())]
client: Client,
http_client: Client,
federation_filter: FederationFilter,
mrf_service: MrfService,
}
Expand Down Expand Up @@ -55,7 +53,7 @@ impl Deliverer {
.body(body.into())?;

let response = self
.client
.http_client
.execute_signed(request, &account.public_key_id, &user.private_key)
.await?;

Expand Down
39 changes: 16 additions & 23 deletions crates/kitsune-activitypub/src/fetcher/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
use async_trait::async_trait;
use headers::{ContentType, HeaderMapExt};
use http::HeaderValue;
use http::{header::ACCEPT, HeaderValue, Request};
use kitsune_cache::ArcCache;
use kitsune_config::language_detection::Configuration as LanguageDetectionConfig;
use kitsune_core::{
consts::USER_AGENT,
traits::{
coerce::CoerceResolver,
fetcher::{AccountFetchOptions, PostFetchOptions},
Fetcher as FetcherTrait, Resolver,
},
use kitsune_core::traits::{
coerce::CoerceResolver,
fetcher::{AccountFetchOptions, PostFetchOptions},
Fetcher as FetcherTrait, Resolver,
};
use kitsune_db::{
model::{account::Account, custom_emoji::CustomEmoji, post::Post},
Expand All @@ -32,23 +29,14 @@ mod actor;
mod emoji;
mod object;

static ACCEPT_VALUE: HeaderValue = HeaderValue::from_static(
"application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\", application/activity+json",
);

#[derive(TypedBuilder)]
#[builder(build_method(into = Arc<Fetcher>))]
pub struct Fetcher {
#[builder(default =
Client::builder()
.default_header(
"accept",
HeaderValue::from_static(
"application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\", application/activity+json",
),
)
.unwrap()
.user_agent(USER_AGENT)
.unwrap()
.build()
)]
client: Client,
http_client: Client,
db_pool: PgPool,
embed_client: Option<EmbedClient>,
federation_filter: FederationFilter,
Expand All @@ -74,8 +62,13 @@ impl Fetcher {
bail!("instance is blocked");
}

let response = self.client.get(url.as_str()).await?;
let request = Request::builder()
.method(http::Method::GET)
.uri(url.as_str())
.header(ACCEPT, &ACCEPT_VALUE)
.body(kitsune_http_client::Body::empty())?;

let response = self.http_client.execute(request).await?;
if !response.status().is_success() {
return Ok(None);
}
Expand Down
12 changes: 6 additions & 6 deletions crates/kitsune-activitypub/tests/fetcher/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async fn fetch_actor() {
let client = Client::builder().service(service_fn(handle));

let fetcher = Fetcher::builder()
.client(client.clone())
.http_client(client.clone())
.db_pool(db_pool)
.embed_client(None)
.federation_filter(
Expand All @@ -36,7 +36,7 @@ async fn fetch_actor() {
)
.language_detection_config(language_detection_config())
.search_backend(NoopSearchService)
.resolver(Arc::new(Webfinger::with_client(client, Arc::new(NoopCache.into()))).coerce())
.resolver(Arc::new(Webfinger::new(client, Arc::new(NoopCache.into()))).coerce())
.account_cache(Arc::new(NoopCache.into()))
.post_cache(Arc::new(NoopCache.into()))
.build();
Expand Down Expand Up @@ -64,7 +64,7 @@ async fn fetch_emoji() {
let client = Client::builder().service(service_fn(handle));

let fetcher = Fetcher::builder()
.client(client.clone())
.http_client(client.clone())
.db_pool(db_pool.clone())
.embed_client(None)
.federation_filter(
Expand All @@ -75,7 +75,7 @@ async fn fetch_emoji() {
)
.language_detection_config(language_detection_config())
.search_backend(NoopSearchService)
.resolver(Arc::new(Webfinger::with_client(client, Arc::new(NoopCache.into()))).coerce())
.resolver(Arc::new(Webfinger::new(client, Arc::new(NoopCache.into()))).coerce())
.account_cache(Arc::new(NoopCache.into()))
.post_cache(Arc::new(NoopCache.into()))
.build();
Expand Down Expand Up @@ -115,7 +115,7 @@ async fn fetch_note() {
let client = Client::builder().service(service_fn(handle));

let fetcher = Fetcher::builder()
.client(client.clone())
.http_client(client.clone())
.db_pool(db_pool.clone())
.embed_client(None)
.federation_filter(
Expand All @@ -126,7 +126,7 @@ async fn fetch_note() {
)
.language_detection_config(language_detection_config())
.search_backend(NoopSearchService)
.resolver(Arc::new(Webfinger::with_client(client, Arc::new(NoopCache.into()))).coerce())
.resolver(Arc::new(Webfinger::new(client, Arc::new(NoopCache.into()))).coerce())
.account_cache(Arc::new(NoopCache.into()))
.post_cache(Arc::new(NoopCache.into()))
.build();
Expand Down
12 changes: 6 additions & 6 deletions crates/kitsune-activitypub/tests/fetcher/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ async fn federation_allow() {
let client = Client::builder().service(client);
let fetcher = builder
.clone()
.client(client.clone())
.http_client(client.clone())
.language_detection_config(language_detection_config())
.resolver(Arc::new(Webfinger::with_client(client, Arc::new(NoopCache.into()))).coerce())
.resolver(Arc::new(Webfinger::new(client, Arc::new(NoopCache.into()))).coerce())
.build();

assert_blocked!(fetcher
Expand All @@ -63,9 +63,9 @@ async fn federation_allow() {
let client = Client::builder().service(service_fn(handle));
let fetcher = builder
.clone()
.client(client.clone())
.http_client(client.clone())
.language_detection_config(language_detection_config())
.resolver(Arc::new(Webfinger::with_client(client, Arc::new(NoopCache.into()))).coerce())
.resolver(Arc::new(Webfinger::new(client, Arc::new(NoopCache.into()))).coerce())
.build();

assert!(matches!(
Expand All @@ -90,7 +90,7 @@ async fn federation_deny() {
let client = Client::builder().service(client);

let fetcher = Fetcher::builder()
.client(client.clone())
.http_client(client.clone())
.db_pool(db_pool)
.embed_client(None)
.federation_filter(
Expand All @@ -101,7 +101,7 @@ async fn federation_deny() {
)
.language_detection_config(language_detection_config())
.search_backend(NoopSearchService)
.resolver(Arc::new(Webfinger::with_client(client, Arc::new(NoopCache.into()))).coerce())
.resolver(Arc::new(Webfinger::new(client, Arc::new(NoopCache.into()))).coerce())
.account_cache(Arc::new(NoopCache.into()))
.post_cache(Arc::new(NoopCache.into()))
.build();
Expand Down
4 changes: 2 additions & 2 deletions crates/kitsune-activitypub/tests/fetcher/infinite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ async fn fetch_infinitely_long_reply_chain() {
let client = Client::builder().service(client);

let fetcher = Fetcher::builder()
.client(client.clone())
.http_client(client.clone())
.db_pool(db_pool)
.embed_client(None)
.federation_filter(
Expand All @@ -102,7 +102,7 @@ async fn fetch_infinitely_long_reply_chain() {
)
.language_detection_config(language_detection_config())
.search_backend(NoopSearchService)
.resolver(Arc::new(Webfinger::with_client(client, Arc::new(NoopCache.into()))).coerce())
.resolver(Arc::new(Webfinger::new(client, Arc::new(NoopCache.into()))).coerce())
.account_cache(Arc::new(NoopCache.into()))
.post_cache(Arc::new(NoopCache.into()))
.build();
Expand Down
12 changes: 6 additions & 6 deletions crates/kitsune-activitypub/tests/fetcher/origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ async fn check_ap_id_authority() {
let client = Client::builder().service(client);
let fetcher = builder
.clone()
.client(client.clone())
.http_client(client.clone())
.language_detection_config(language_detection_config())
.resolver(Arc::new(Webfinger::with_client(client, Arc::new(NoopCache.into()))).coerce())
.resolver(Arc::new(Webfinger::new(client, Arc::new(NoopCache.into()))).coerce())
.build();

// The mock HTTP client ensures that the fetcher doesn't access the correct server
Expand All @@ -60,9 +60,9 @@ async fn check_ap_id_authority() {
let client = Client::builder().service(client);
let fetcher = builder
.clone()
.client(client.clone())
.http_client(client.clone())
.language_detection_config(language_detection_config())
.resolver(Arc::new(Webfinger::with_client(client, Arc::new(NoopCache.into()))).coerce())
.resolver(Arc::new(Webfinger::new(client, Arc::new(NoopCache.into()))).coerce())
.build();

let _ = fetcher
Expand All @@ -84,7 +84,7 @@ async fn check_ap_content_type() {
let client = Client::builder().service(client);

let fetcher = Fetcher::builder()
.client(client.clone())
.http_client(client.clone())
.db_pool(db_pool)
.embed_client(None)
.federation_filter(
Expand All @@ -95,7 +95,7 @@ async fn check_ap_content_type() {
)
.language_detection_config(language_detection_config())
.search_backend(NoopSearchService)
.resolver(Arc::new(Webfinger::with_client(client, Arc::new(NoopCache.into()))).coerce())
.resolver(Arc::new(Webfinger::new(client, Arc::new(NoopCache.into()))).coerce())
.account_cache(Arc::new(NoopCache.into()))
.post_cache(Arc::new(NoopCache.into()))
.build();
Expand Down
8 changes: 4 additions & 4 deletions crates/kitsune-activitypub/tests/fetcher/webfinger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async fn fetch_actor_with_custom_acct() {
let client = Client::builder().service(client);

let fetcher = Fetcher::builder()
.client(client.clone())
.http_client(client.clone())
.db_pool(db_pool)
.embed_client(None)
.federation_filter(
Expand All @@ -58,7 +58,7 @@ async fn fetch_actor_with_custom_acct() {
)
.language_detection_config(language_detection_config())
.search_backend(NoopSearchService)
.resolver(Arc::new(Webfinger::with_client(client, Arc::new(NoopCache.into()))).coerce())
.resolver(Arc::new(Webfinger::new(client, Arc::new(NoopCache.into()))).coerce())
.account_cache(Arc::new(NoopCache.into()))
.post_cache(Arc::new(NoopCache.into()))
.build();
Expand Down Expand Up @@ -125,7 +125,7 @@ async fn ignore_fake_webfinger_acct() {
let client = Client::builder().service(client);

let fetcher = Fetcher::builder()
.client(client.clone())
.http_client(client.clone())
.db_pool(db_pool)
.embed_client(None)
.federation_filter(
Expand All @@ -136,7 +136,7 @@ async fn ignore_fake_webfinger_acct() {
)
.language_detection_config(language_detection_config())
.search_backend(NoopSearchService)
.resolver(Arc::new(Webfinger::with_client(client, Arc::new(NoopCache.into()))).coerce())
.resolver(Arc::new(Webfinger::new(client, Arc::new(NoopCache.into()))).coerce())
.account_cache(Arc::new(NoopCache.into()))
.post_cache(Arc::new(NoopCache.into()))
.build();
Expand Down
5 changes: 2 additions & 3 deletions crates/kitsune-captcha/src/hcaptcha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ pub struct Captcha {
pub site_key: String,
pub secret_key: String,

#[builder(default)]
client: Client,
http_client: Client,
}

#[derive(Serialize, Deserialize, TypedBuilder)]
Expand Down Expand Up @@ -46,7 +45,7 @@ impl CaptchaBackend for Captcha {
.header("Accept", "application/json")
.body(body.into())?;

let response = self.client.execute(request).await?;
let response = self.http_client.execute(request).await?;

let verification_result = response.json::<HCaptchaResponse>().await?;
if !verification_result.success {
Expand Down
5 changes: 2 additions & 3 deletions crates/kitsune-captcha/src/mcaptcha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ pub struct Captcha {
pub secret_key: String,
pub site_key: String,

#[builder(default)]
client: Client,
http_client: Client,
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -41,7 +40,7 @@ impl CaptchaBackend for Captcha {
.header("Accept", "application/json")
.body(body.into())?;

let response = self.client.execute(request).await?;
let response = self.http_client.execute(request).await?;

let verification_result = response.json::<MCaptchaResponse>().await?;
if !verification_result.valid {
Expand Down
1 change: 1 addition & 0 deletions crates/kitsune-federation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ kitsune-core = { workspace = true }
kitsune-db = { workspace = true }
kitsune-embed = { workspace = true }
kitsune-federation-filter = { workspace = true }
kitsune-http-client.workspace = true
kitsune-search = { workspace = true }
kitsune-service = { workspace = true }
kitsune-url = { workspace = true }
Expand Down
6 changes: 5 additions & 1 deletion crates/kitsune-federation/src/activitypub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct PrepareDeliverer {
attachment_service: AttachmentService,
db_pool: PgPool,
federation_filter: FederationFilter,
http_client: kitsune_http_client::Client,
mrf_service: MrfService,
url_service: UrlService,
}
Expand All @@ -38,6 +39,7 @@ pub struct PrepareFetcher {
db_pool: PgPool,
embed_client: Option<kitsune_embed::Client>,
federation_filter: FederationFilter,
http_client: kitsune_http_client::Client,
language_detection_config: LanguageDetectionConfig,
post_cache: ArcCache<str, Post>,
search_backend: AnySearchBackend,
Expand All @@ -47,6 +49,7 @@ pub struct PrepareFetcher {
pub(crate) fn prepare_deliverer(prepare: PrepareDeliverer) -> Arc<dyn Deliverer> {
let core_deliverer = kitsune_activitypub::CoreDeliverer::builder()
.federation_filter(prepare.federation_filter)
.http_client(prepare.http_client)
.mrf_service(prepare.mrf_service)
.build();

Expand All @@ -67,13 +70,14 @@ pub(crate) fn prepare_deliverer(prepare: PrepareDeliverer) -> Arc<dyn Deliverer>

#[inline]
pub(crate) fn prepare_fetcher(prepare: PrepareFetcher) -> Arc<dyn Fetcher> {
let webfinger = Webfinger::new(prepare.account_resource_cache);
let webfinger = Webfinger::new(prepare.http_client.clone(), prepare.account_resource_cache);

ActivityPubFetcher::builder()
.account_cache(prepare.account_cache)
.db_pool(prepare.db_pool.clone())
.embed_client(prepare.embed_client)
.federation_filter(prepare.federation_filter.clone())
.http_client(prepare.http_client)
.language_detection_config(prepare.language_detection_config)
.post_cache(prepare.post_cache)
.resolver(Arc::new(webfinger).coerce())
Expand Down
Loading

0 comments on commit 857fd3b

Please sign in to comment.