Merge branch 'asonix/encourage-reqwest-reuse' into 'next'
Re-use a basic reqwest client in all possible cases See merge request famedly/conduit!265
This commit is contained in:
commit
8db7d2c025
|
@ -2018,8 +2018,7 @@ dependencies = [
|
|||
[[package]]
|
||||
name = "reqwest"
|
||||
version = "0.11.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "87f242f1488a539a79bac6dbe7c8609ae43b7914b7736210f239a37cccb32525"
|
||||
source = "git+https://github.com/niuhuan/reqwest?branch=dns-resolver-fn#57b7cf4feb921573dfafad7d34b9ac6e44ead0bd"
|
||||
dependencies = [
|
||||
"base64 0.13.0",
|
||||
"bytes",
|
||||
|
|
|
@ -48,7 +48,7 @@ rand = "0.8.4"
|
|||
# Used to hash passwords
|
||||
rust-argon2 = "0.8.3"
|
||||
# Used to send requests
|
||||
reqwest = { version = "0.11.4", default-features = false, features = ["rustls-tls", "socks"] }
|
||||
reqwest = { version = "0.11.4", default-features = false, features = ["rustls-tls", "socks"], git = "https://github.com/niuhuan/reqwest", branch = "dns-resolver-fn" }
|
||||
# Used for conduit::Error type
|
||||
thiserror = "1.0.28"
|
||||
# Used to generate thumbnails for images
|
||||
|
|
|
@ -41,11 +41,7 @@ where
|
|||
*reqwest_request.timeout_mut() = Some(Duration::from_secs(30));
|
||||
|
||||
let url = reqwest_request.url().clone();
|
||||
let mut response = globals
|
||||
.reqwest_client()?
|
||||
.build()?
|
||||
.execute(reqwest_request)
|
||||
.await?;
|
||||
let mut response = globals.default_client().execute(reqwest_request).await?;
|
||||
|
||||
// reqwest::Response -> http::Response conversion
|
||||
let status = response.status();
|
||||
|
|
|
@ -10,7 +10,7 @@ use std::{
|
|||
collections::{BTreeMap, HashMap},
|
||||
fs,
|
||||
future::Future,
|
||||
net::IpAddr,
|
||||
net::{IpAddr, SocketAddr},
|
||||
path::PathBuf,
|
||||
sync::{Arc, Mutex, RwLock},
|
||||
time::{Duration, Instant},
|
||||
|
@ -39,6 +39,8 @@ pub struct Globals {
|
|||
keypair: Arc<ruma::signatures::Ed25519KeyPair>,
|
||||
dns_resolver: TokioAsyncResolver,
|
||||
jwt_decoding_key: Option<jsonwebtoken::DecodingKey<'static>>,
|
||||
federation_client: reqwest::Client,
|
||||
default_client: reqwest::Client,
|
||||
pub(super) server_signingkeys: Arc<dyn Tree>,
|
||||
pub bad_event_ratelimiter: Arc<RwLock<HashMap<Box<EventId>, RateLimitState>>>,
|
||||
pub bad_signature_ratelimiter: Arc<RwLock<HashMap<Vec<String>, RateLimitState>>>,
|
||||
|
@ -132,6 +134,17 @@ impl Globals {
|
|||
.as_ref()
|
||||
.map(|secret| jsonwebtoken::DecodingKey::from_secret(secret.as_bytes()).into_static());
|
||||
|
||||
let default_client = reqwest_client_builder(&config)?.build()?;
|
||||
let name_override = Arc::clone(&tls_name_override);
|
||||
let federation_client = reqwest_client_builder(&config)?
|
||||
.resolve_fn(move |domain| {
|
||||
let read_guard = name_override.read().unwrap();
|
||||
let (override_name, port) = read_guard.get(&domain)?;
|
||||
let first_name = override_name.get(0)?;
|
||||
Some(SocketAddr::new(*first_name, *port))
|
||||
})
|
||||
.build()?;
|
||||
|
||||
let s = Self {
|
||||
globals,
|
||||
config,
|
||||
|
@ -141,6 +154,8 @@ impl Globals {
|
|||
})?,
|
||||
actual_destination_cache: Arc::new(RwLock::new(WellKnownMap::new())),
|
||||
tls_name_override,
|
||||
federation_client,
|
||||
default_client,
|
||||
server_signingkeys,
|
||||
jwt_decoding_key,
|
||||
bad_event_ratelimiter: Arc::new(RwLock::new(HashMap::new())),
|
||||
|
@ -163,17 +178,16 @@ impl Globals {
|
|||
&self.keypair
|
||||
}
|
||||
|
||||
/// Returns a reqwest client which can be used to send requests.
|
||||
pub fn reqwest_client(&self) -> Result<reqwest::ClientBuilder> {
|
||||
let mut reqwest_client_builder = reqwest::Client::builder()
|
||||
.connect_timeout(Duration::from_secs(30))
|
||||
.timeout(Duration::from_secs(60 * 3))
|
||||
.pool_max_idle_per_host(1);
|
||||
if let Some(proxy) = self.config.proxy.to_proxy()? {
|
||||
reqwest_client_builder = reqwest_client_builder.proxy(proxy);
|
||||
}
|
||||
/// Returns a reqwest client which can be used to send requests
|
||||
pub fn default_client(&self) -> reqwest::Client {
|
||||
// Client is cheap to clone (Arc wrapper) and avoids lifetime issues
|
||||
self.default_client.clone()
|
||||
}
|
||||
|
||||
Ok(reqwest_client_builder)
|
||||
/// Returns a client used for resolving .well-knowns
|
||||
pub fn federation_client(&self) -> reqwest::Client {
|
||||
// Client is cheap to clone (Arc wrapper) and avoids lifetime issues
|
||||
self.federation_client.clone()
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
|
@ -340,3 +354,15 @@ impl Globals {
|
|||
r
|
||||
}
|
||||
}
|
||||
|
||||
fn reqwest_client_builder(config: &Config) -> Result<reqwest::ClientBuilder> {
|
||||
let mut reqwest_client_builder = reqwest::Client::builder()
|
||||
.connect_timeout(Duration::from_secs(30))
|
||||
.timeout(Duration::from_secs(60 * 3));
|
||||
|
||||
if let Some(proxy) = config.proxy.to_proxy()? {
|
||||
reqwest_client_builder = reqwest_client_builder.proxy(proxy);
|
||||
}
|
||||
|
||||
Ok(reqwest_client_builder)
|
||||
}
|
||||
|
|
|
@ -115,11 +115,7 @@ where
|
|||
//*reqwest_request.timeout_mut() = Some(Duration::from_secs(5));
|
||||
|
||||
let url = reqwest_request.url().clone();
|
||||
let response = globals
|
||||
.reqwest_client()?
|
||||
.build()?
|
||||
.execute(reqwest_request)
|
||||
.await;
|
||||
let response = globals.default_client().execute(reqwest_request).await;
|
||||
|
||||
match response {
|
||||
Ok(mut response) => {
|
||||
|
|
|
@ -236,21 +236,7 @@ where
|
|||
|
||||
let url = reqwest_request.url().clone();
|
||||
|
||||
let mut client = globals.reqwest_client()?;
|
||||
if let Some((override_name, port)) = globals
|
||||
.tls_name_override
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(&actual_destination.hostname())
|
||||
{
|
||||
client = client.resolve(
|
||||
&actual_destination.hostname(),
|
||||
SocketAddr::new(override_name[0], *port),
|
||||
);
|
||||
// port will be ignored
|
||||
}
|
||||
|
||||
let response = client.build()?.execute(reqwest_request).await;
|
||||
let response = globals.federation_client().execute(reqwest_request).await;
|
||||
|
||||
match response {
|
||||
Ok(mut response) => {
|
||||
|
@ -490,10 +476,7 @@ async fn request_well_known(
|
|||
) -> Option<String> {
|
||||
let body: serde_json::Value = serde_json::from_str(
|
||||
&globals
|
||||
.reqwest_client()
|
||||
.ok()?
|
||||
.build()
|
||||
.ok()?
|
||||
.default_client()
|
||||
.get(&format!(
|
||||
"https://{}/.well-known/matrix/server",
|
||||
destination
|
||||
|
|
Loading…
Reference in New Issue