Move federation client builders to activitypub::http_client module

This commit is contained in:
silverpill 2023-03-06 16:55:21 +00:00
parent f17c9d9f76
commit 0817177282
4 changed files with 41 additions and 29 deletions

View file

@ -1,8 +1,7 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::time::Duration;
use actix_web::http::Method; use actix_web::http::Method;
use reqwest::{Client, Proxy}; use reqwest::Client;
use rsa::RsaPrivateKey; use rsa::RsaPrivateKey;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value; use serde_json::Value;
@ -24,10 +23,13 @@ use crate::json_signatures::create::{
JsonSignatureError, JsonSignatureError,
}; };
use crate::models::users::types::User; use crate::models::users::types::User;
use super::actors::types::Actor; use super::{
use super::constants::AP_MEDIA_TYPE; actors::types::Actor,
use super::identifiers::{local_actor_id, local_actor_key_id}; constants::AP_MEDIA_TYPE,
use super::queues::OutgoingActivityJobData; http_client::build_federation_client,
identifiers::{local_actor_id, local_actor_key_id},
queues::OutgoingActivityJobData,
};
const DELIVERER_TIMEOUT: u64 = 30; const DELIVERER_TIMEOUT: u64 = 30;
@ -56,15 +58,7 @@ pub enum DelivererError {
} }
fn build_client(instance: &Instance) -> reqwest::Result<Client> { fn build_client(instance: &Instance) -> reqwest::Result<Client> {
let mut client_builder = Client::builder(); build_federation_client(instance, DELIVERER_TIMEOUT)
if let Some(ref proxy_url) = instance.proxy_url {
let proxy = Proxy::all(proxy_url)?;
client_builder = client_builder.proxy(proxy);
};
let timeout = Duration::from_secs(DELIVERER_TIMEOUT);
client_builder
.timeout(timeout)
.build()
} }
async fn send_activity( async fn send_activity(

View file

@ -1,7 +1,6 @@
use std::path::Path; use std::path::Path;
use std::time::Duration;
use reqwest::{Client, Method, Proxy, RequestBuilder}; use reqwest::{Client, Method, RequestBuilder};
use serde_json::Value; use serde_json::Value;
use mitra_config::Instance; use mitra_config::Instance;
@ -13,6 +12,7 @@ use mitra_utils::{
use crate::activitypub::{ use crate::activitypub::{
actors::types::Actor, actors::types::Actor,
constants::{AP_CONTEXT, AP_MEDIA_TYPE}, constants::{AP_CONTEXT, AP_MEDIA_TYPE},
http_client::build_federation_client,
identifiers::{local_actor_key_id, local_instance_actor_id}, identifiers::{local_actor_key_id, local_instance_actor_id},
types::Object, types::Object,
vocabulary::GROUP, vocabulary::GROUP,
@ -24,7 +24,6 @@ use crate::http_signatures::create::{
use crate::media::{save_file, SUPPORTED_MEDIA_TYPES}; use crate::media::{save_file, SUPPORTED_MEDIA_TYPES};
use crate::webfinger::types::{ActorAddress, JsonResourceDescriptor}; use crate::webfinger::types::{ActorAddress, JsonResourceDescriptor};
const FETCHER_CONNECTION_TIMEOUT: u64 = 30;
const FETCHER_TIMEOUT: u64 = 180; const FETCHER_TIMEOUT: u64 = 180;
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]
@ -52,17 +51,7 @@ pub enum FetchError {
} }
fn build_client(instance: &Instance) -> reqwest::Result<Client> { fn build_client(instance: &Instance) -> reqwest::Result<Client> {
let mut client_builder = Client::builder(); build_federation_client(instance, FETCHER_TIMEOUT)
if let Some(ref proxy_url) = instance.proxy_url {
let proxy = Proxy::all(proxy_url)?;
client_builder = client_builder.proxy(proxy);
};
let timeout = Duration::from_secs(FETCHER_TIMEOUT);
let connect_timeout = Duration::from_secs(FETCHER_CONNECTION_TIMEOUT);
client_builder
.timeout(timeout)
.connect_timeout(connect_timeout)
.build()
} }
fn build_request( fn build_request(

View file

@ -0,0 +1,28 @@
use std::cmp::max;
use std::time::Duration;
use reqwest::{Client, Proxy};
use mitra_config::Instance;
const CONNECTION_TIMEOUT: u64 = 30;
pub fn build_federation_client(
instance: &Instance,
timeout: u64,
) -> reqwest::Result<Client> {
let mut client_builder = Client::builder();
if let Some(ref proxy_url) = instance.proxy_url {
let proxy = Proxy::all(proxy_url)?;
client_builder = client_builder.proxy(proxy);
};
let request_timeout = Duration::from_secs(timeout);
let connect_timeout = Duration::from_secs(max(
timeout,
CONNECTION_TIMEOUT,
));
client_builder
.timeout(request_timeout)
.connect_timeout(connect_timeout)
.build()
}

View file

@ -6,6 +6,7 @@ pub mod constants;
mod deliverer; mod deliverer;
pub mod fetcher; pub mod fetcher;
mod handlers; mod handlers;
mod http_client;
pub mod identifiers; pub mod identifiers;
pub mod queues; pub mod queues;
mod receiver; mod receiver;