convert Client into Service
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
f465d77ad3
commit
8a2ae401df
13 changed files with 159 additions and 23 deletions
|
@ -35,7 +35,6 @@ pub(super) async fn fetch_support_well_known(
|
|||
_body: Vec<&str>, server_name: Box<ServerName>,
|
||||
) -> Result<RoomMessageEventContent> {
|
||||
let response = services()
|
||||
.globals
|
||||
.client
|
||||
.default
|
||||
.get(format!("https://{server_name}/.well-known/matrix/support"))
|
||||
|
|
|
@ -621,7 +621,7 @@ async fn request_url_preview(services: &Services, url: &str) -> Result<UrlPrevie
|
|||
}
|
||||
}
|
||||
|
||||
let client = &services.globals.client.url_preview;
|
||||
let client = &services.client.url_preview;
|
||||
let response = client.head(url).send().await?;
|
||||
|
||||
if let Some(remote_addr) = response.remote_addr() {
|
||||
|
|
143
src/service/client/mod.rs
Normal file
143
src/service/client/mod.rs
Normal file
|
@ -0,0 +1,143 @@
|
|||
use std::{sync::Arc, time::Duration};
|
||||
|
||||
use conduit::{Config, Result};
|
||||
use reqwest::redirect;
|
||||
|
||||
use crate::{resolver, service};
|
||||
|
||||
pub struct Service {
|
||||
pub default: reqwest::Client,
|
||||
pub url_preview: reqwest::Client,
|
||||
pub well_known: reqwest::Client,
|
||||
pub federation: reqwest::Client,
|
||||
pub sender: reqwest::Client,
|
||||
pub appservice: reqwest::Client,
|
||||
pub pusher: reqwest::Client,
|
||||
}
|
||||
|
||||
impl crate::Service for Service {
|
||||
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
|
||||
let config = &args.server.config;
|
||||
let resolver = args
|
||||
.get_service::<resolver::Service>("resolver")
|
||||
.expect("resolver must be built prior to client");
|
||||
|
||||
Ok(Arc::new(Self {
|
||||
default: base(config)
|
||||
.unwrap()
|
||||
.dns_resolver(resolver.clone())
|
||||
.build()
|
||||
.unwrap(),
|
||||
|
||||
url_preview: base(config)
|
||||
.unwrap()
|
||||
.dns_resolver(resolver.clone())
|
||||
.redirect(redirect::Policy::limited(3))
|
||||
.build()
|
||||
.unwrap(),
|
||||
|
||||
well_known: base(config)
|
||||
.unwrap()
|
||||
.dns_resolver(resolver.hooked.clone())
|
||||
.connect_timeout(Duration::from_secs(config.well_known_conn_timeout))
|
||||
.read_timeout(Duration::from_secs(config.well_known_timeout))
|
||||
.timeout(Duration::from_secs(config.well_known_timeout))
|
||||
.pool_max_idle_per_host(0)
|
||||
.redirect(redirect::Policy::limited(4))
|
||||
.build()
|
||||
.unwrap(),
|
||||
|
||||
federation: base(config)
|
||||
.unwrap()
|
||||
.dns_resolver(resolver.hooked.clone())
|
||||
.read_timeout(Duration::from_secs(config.federation_timeout))
|
||||
.timeout(Duration::from_secs(config.federation_timeout))
|
||||
.pool_max_idle_per_host(config.federation_idle_per_host.into())
|
||||
.pool_idle_timeout(Duration::from_secs(config.federation_idle_timeout))
|
||||
.redirect(redirect::Policy::limited(3))
|
||||
.build()
|
||||
.unwrap(),
|
||||
|
||||
sender: base(config)
|
||||
.unwrap()
|
||||
.dns_resolver(resolver.hooked.clone())
|
||||
.read_timeout(Duration::from_secs(config.sender_timeout))
|
||||
.timeout(Duration::from_secs(config.sender_timeout))
|
||||
.pool_max_idle_per_host(1)
|
||||
.pool_idle_timeout(Duration::from_secs(config.sender_idle_timeout))
|
||||
.redirect(redirect::Policy::limited(2))
|
||||
.build()
|
||||
.unwrap(),
|
||||
|
||||
appservice: base(config)
|
||||
.unwrap()
|
||||
.dns_resolver(resolver.clone())
|
||||
.connect_timeout(Duration::from_secs(5))
|
||||
.read_timeout(Duration::from_secs(config.appservice_timeout))
|
||||
.timeout(Duration::from_secs(config.appservice_timeout))
|
||||
.pool_max_idle_per_host(1)
|
||||
.pool_idle_timeout(Duration::from_secs(config.appservice_idle_timeout))
|
||||
.redirect(redirect::Policy::limited(2))
|
||||
.build()
|
||||
.unwrap(),
|
||||
|
||||
pusher: base(config)
|
||||
.unwrap()
|
||||
.dns_resolver(resolver.clone())
|
||||
.pool_max_idle_per_host(1)
|
||||
.pool_idle_timeout(Duration::from_secs(config.pusher_idle_timeout))
|
||||
.redirect(redirect::Policy::limited(2))
|
||||
.build()
|
||||
.unwrap(),
|
||||
}))
|
||||
}
|
||||
|
||||
fn name(&self) -> &str { service::make_name(std::module_path!()) }
|
||||
}
|
||||
|
||||
fn base(config: &Config) -> Result<reqwest::ClientBuilder> {
|
||||
let mut builder = reqwest::Client::builder()
|
||||
.hickory_dns(true)
|
||||
.connect_timeout(Duration::from_secs(config.request_conn_timeout))
|
||||
.read_timeout(Duration::from_secs(config.request_timeout))
|
||||
.timeout(Duration::from_secs(config.request_total_timeout))
|
||||
.pool_idle_timeout(Duration::from_secs(config.request_idle_timeout))
|
||||
.pool_max_idle_per_host(config.request_idle_per_host.into())
|
||||
.user_agent(conduit::version::user_agent())
|
||||
.redirect(redirect::Policy::limited(6))
|
||||
.connection_verbose(true);
|
||||
|
||||
#[cfg(feature = "gzip_compression")]
|
||||
{
|
||||
builder = if config.gzip_compression {
|
||||
builder.gzip(true)
|
||||
} else {
|
||||
builder.gzip(false).no_gzip()
|
||||
};
|
||||
};
|
||||
|
||||
#[cfg(feature = "brotli_compression")]
|
||||
{
|
||||
builder = if config.brotli_compression {
|
||||
builder.brotli(true)
|
||||
} else {
|
||||
builder.brotli(false).no_brotli()
|
||||
};
|
||||
};
|
||||
|
||||
#[cfg(not(feature = "gzip_compression"))]
|
||||
{
|
||||
builder = builder.no_gzip();
|
||||
};
|
||||
|
||||
#[cfg(not(feature = "brotli_compression"))]
|
||||
{
|
||||
builder = builder.no_brotli();
|
||||
};
|
||||
|
||||
if let Some(proxy) = config.proxy.to_proxy()? {
|
||||
Ok(builder.proxy(proxy))
|
||||
} else {
|
||||
Ok(builder)
|
||||
}
|
||||
}
|
|
@ -1,4 +1,3 @@
|
|||
mod client;
|
||||
mod data;
|
||||
mod emerg_access;
|
||||
pub(super) mod migrations;
|
||||
|
@ -24,7 +23,7 @@ use ruma::{
|
|||
use tokio::sync::Mutex;
|
||||
use url::Url;
|
||||
|
||||
use crate::{resolver, service, services};
|
||||
use crate::{service, services};
|
||||
|
||||
pub struct Service {
|
||||
pub db: Data,
|
||||
|
@ -33,7 +32,6 @@ pub struct Service {
|
|||
pub cidr_range_denylist: Vec<IPAddress>,
|
||||
keypair: Arc<ruma::signatures::Ed25519KeyPair>,
|
||||
jwt_decoding_key: Option<jsonwebtoken::DecodingKey>,
|
||||
pub client: client::Client,
|
||||
pub stable_room_versions: Vec<RoomVersionId>,
|
||||
pub unstable_room_versions: Vec<RoomVersionId>,
|
||||
pub bad_event_ratelimiter: Arc<RwLock<HashMap<OwnedEventId, RateLimitState>>>,
|
||||
|
@ -85,15 +83,11 @@ impl crate::Service for Service {
|
|||
cidr_range_denylist.push(cidr);
|
||||
}
|
||||
|
||||
let resolver = service::get::<resolver::Service>(args.service, "resolver")
|
||||
.expect("resolver must be built prior to globals");
|
||||
|
||||
let mut s = Self {
|
||||
db,
|
||||
config: config.clone(),
|
||||
cidr_range_denylist,
|
||||
keypair: Arc::new(keypair),
|
||||
client: client::Client::new(config, &resolver),
|
||||
jwt_decoding_key,
|
||||
stable_room_versions,
|
||||
unstable_room_versions,
|
||||
|
|
|
@ -7,6 +7,7 @@ pub mod services;
|
|||
pub mod account_data;
|
||||
pub mod admin;
|
||||
pub mod appservice;
|
||||
pub mod client;
|
||||
pub mod globals;
|
||||
pub mod key_backups;
|
||||
pub mod media;
|
||||
|
@ -25,7 +26,7 @@ extern crate conduit_database as database;
|
|||
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
pub(crate) use conduit::{config, debug_error, debug_warn, utils, Config, Error, Result, Server};
|
||||
pub(crate) use conduit::{config, debug_error, debug_warn, utils, Error, Result, Server};
|
||||
pub use conduit::{pdu, PduBuilder, PduCount, PduEvent};
|
||||
use database::Database;
|
||||
pub(crate) use service::{Args, Service};
|
||||
|
|
|
@ -84,12 +84,7 @@ impl Service {
|
|||
}
|
||||
}
|
||||
|
||||
let response = services()
|
||||
.globals
|
||||
.client
|
||||
.pusher
|
||||
.execute(reqwest_request)
|
||||
.await;
|
||||
let response = services().client.pusher.execute(reqwest_request).await;
|
||||
|
||||
match response {
|
||||
Ok(mut response) => {
|
||||
|
|
|
@ -49,7 +49,6 @@ where
|
|||
let reqwest_request = reqwest::Request::try_from(http_request)?;
|
||||
|
||||
let mut response = services()
|
||||
.globals
|
||||
.client
|
||||
.appservice
|
||||
.execute(reqwest_request)
|
||||
|
|
|
@ -194,7 +194,7 @@ impl Service {
|
|||
where
|
||||
T: OutgoingRequest + Debug + Send,
|
||||
{
|
||||
let client = &services().globals.client.federation;
|
||||
let client = &services().client.federation;
|
||||
send::send(client, dest, request).await
|
||||
}
|
||||
|
||||
|
|
|
@ -178,7 +178,6 @@ async fn request_well_known(dest: &str) -> Result<Option<String>> {
|
|||
}
|
||||
|
||||
let response = services()
|
||||
.globals
|
||||
.client
|
||||
.well_known
|
||||
.get(&format!("https://{dest}/.well-known/matrix/server"))
|
||||
|
|
|
@ -611,11 +611,10 @@ async fn send_events_dest_normal(
|
|||
}
|
||||
}
|
||||
|
||||
let client = &services().globals.client.sender;
|
||||
//debug_assert!(pdu_jsons.len() + edu_jsons.len() > 0, "sending empty
|
||||
// transaction");
|
||||
send::send(
|
||||
client,
|
||||
&services().client.sender,
|
||||
server,
|
||||
send_transaction_message::v1::Request {
|
||||
origin: services().globals.server_name().to_owned(),
|
||||
|
|
|
@ -43,6 +43,12 @@ pub(crate) struct Args<'a> {
|
|||
pub(crate) type Map = BTreeMap<String, MapVal>;
|
||||
pub(crate) type MapVal = (Arc<dyn Service>, Arc<dyn Any + Send + Sync>);
|
||||
|
||||
impl Args<'_> {
|
||||
pub(crate) fn get_service<T: Any + Send + Sync>(&self, name: &str) -> Option<Arc<T>> {
|
||||
get::<T>(self.service, name)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get<T: Any + Send + Sync>(map: &Map, name: &str) -> Option<Arc<T>> {
|
||||
map.get(name).map(|(_, s)| {
|
||||
s.clone()
|
||||
|
|
|
@ -5,7 +5,7 @@ use database::Database;
|
|||
use tokio::sync::Mutex;
|
||||
|
||||
use crate::{
|
||||
account_data, admin, appservice, globals, key_backups,
|
||||
account_data, admin, appservice, client, globals, key_backups,
|
||||
manager::Manager,
|
||||
media, presence, pusher, resolver, rooms, sending, service,
|
||||
service::{Args, Map, Service},
|
||||
|
@ -14,6 +14,7 @@ use crate::{
|
|||
|
||||
pub struct Services {
|
||||
pub resolver: Arc<resolver::Service>,
|
||||
pub client: Arc<client::Service>,
|
||||
pub globals: Arc<globals::Service>,
|
||||
pub rooms: rooms::Service,
|
||||
pub appservice: Arc<appservice::Service>,
|
||||
|
@ -52,6 +53,7 @@ impl Services {
|
|||
|
||||
Ok(Self {
|
||||
resolver: build!(resolver::Service),
|
||||
client: build!(client::Service),
|
||||
globals: build!(globals::Service),
|
||||
rooms: rooms::Service {
|
||||
alias: build!(rooms::alias::Service),
|
||||
|
|
|
@ -64,7 +64,6 @@ impl Service {
|
|||
#[tracing::instrument(skip_all)]
|
||||
async fn handle_updates(&self) -> Result<()> {
|
||||
let response = services()
|
||||
.globals
|
||||
.client
|
||||
.default
|
||||
.get(CHECK_FOR_UPDATES_URL)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue