mirror of
https://gitlab.com/famedly/conduit.git
synced 2024-12-27 12:54:33 +01:00
Re-use a basic request in all possible cases
This commit is contained in:
parent
20006c91af
commit
f8d1c1a8af
4 changed files with 44 additions and 31 deletions
|
@ -46,11 +46,7 @@ where
|
||||||
*reqwest_request.timeout_mut() = Some(Duration::from_secs(30));
|
*reqwest_request.timeout_mut() = Some(Duration::from_secs(30));
|
||||||
|
|
||||||
let url = reqwest_request.url().clone();
|
let url = reqwest_request.url().clone();
|
||||||
let mut response = globals
|
let mut response = globals.reqwest_client().execute(reqwest_request).await?;
|
||||||
.reqwest_client()?
|
|
||||||
.build()?
|
|
||||||
.execute(reqwest_request)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
// reqwest::Response -> http::Response conversion
|
// reqwest::Response -> http::Response conversion
|
||||||
let status = response.status();
|
let status = response.status();
|
||||||
|
|
|
@ -39,6 +39,7 @@ pub struct Globals {
|
||||||
keypair: Arc<ruma::signatures::Ed25519KeyPair>,
|
keypair: Arc<ruma::signatures::Ed25519KeyPair>,
|
||||||
dns_resolver: TokioAsyncResolver,
|
dns_resolver: TokioAsyncResolver,
|
||||||
jwt_decoding_key: Option<jsonwebtoken::DecodingKey<'static>>,
|
jwt_decoding_key: Option<jsonwebtoken::DecodingKey<'static>>,
|
||||||
|
basic_client: reqwest::Client,
|
||||||
pub(super) server_signingkeys: Arc<dyn Tree>,
|
pub(super) server_signingkeys: Arc<dyn Tree>,
|
||||||
pub bad_event_ratelimiter: Arc<RwLock<HashMap<Box<EventId>, RateLimitState>>>,
|
pub bad_event_ratelimiter: Arc<RwLock<HashMap<Box<EventId>, RateLimitState>>>,
|
||||||
pub bad_signature_ratelimiter: Arc<RwLock<HashMap<Vec<String>, RateLimitState>>>,
|
pub bad_signature_ratelimiter: Arc<RwLock<HashMap<Vec<String>, RateLimitState>>>,
|
||||||
|
@ -132,6 +133,8 @@ impl Globals {
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|secret| jsonwebtoken::DecodingKey::from_secret(secret.as_bytes()).into_static());
|
.map(|secret| jsonwebtoken::DecodingKey::from_secret(secret.as_bytes()).into_static());
|
||||||
|
|
||||||
|
let basic_client = reqwest_client_builder(&config, None)?.build()?;
|
||||||
|
|
||||||
let s = Self {
|
let s = Self {
|
||||||
globals,
|
globals,
|
||||||
config,
|
config,
|
||||||
|
@ -141,6 +144,7 @@ impl Globals {
|
||||||
})?,
|
})?,
|
||||||
actual_destination_cache: Arc::new(RwLock::new(WellKnownMap::new())),
|
actual_destination_cache: Arc::new(RwLock::new(WellKnownMap::new())),
|
||||||
tls_name_override,
|
tls_name_override,
|
||||||
|
basic_client,
|
||||||
server_signingkeys,
|
server_signingkeys,
|
||||||
jwt_decoding_key,
|
jwt_decoding_key,
|
||||||
bad_event_ratelimiter: Arc::new(RwLock::new(HashMap::new())),
|
bad_event_ratelimiter: Arc::new(RwLock::new(HashMap::new())),
|
||||||
|
@ -163,17 +167,15 @@ impl Globals {
|
||||||
&self.keypair
|
&self.keypair
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a reqwest client which can be used to send requests.
|
/// Returns a reqwest client which can be used to send requests
|
||||||
pub fn reqwest_client(&self) -> Result<reqwest::ClientBuilder> {
|
pub fn reqwest_client(&self) -> reqwest::Client {
|
||||||
let mut reqwest_client_builder = reqwest::Client::builder()
|
// can't return &Client or else we'll hold a lock around the DB across an await
|
||||||
.connect_timeout(Duration::from_secs(30))
|
self.basic_client.clone()
|
||||||
.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);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(reqwest_client_builder)
|
/// Returns a reqwest client builder which can be customized and used to send requests.
|
||||||
|
pub fn reqwest_client_builder(&self) -> Result<reqwest::ClientBuilder> {
|
||||||
|
reqwest_client_builder(&self.config, Some(1))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tracing::instrument(skip(self))]
|
#[tracing::instrument(skip(self))]
|
||||||
|
@ -340,3 +342,21 @@ impl Globals {
|
||||||
r
|
r
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn reqwest_client_builder(
|
||||||
|
config: &Config,
|
||||||
|
max_idle: Option<usize>,
|
||||||
|
) -> 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(max_idle) = max_idle {
|
||||||
|
reqwest_client_builder = reqwest_client_builder.pool_max_idle_per_host(max_idle);
|
||||||
|
}
|
||||||
|
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));
|
//*reqwest_request.timeout_mut() = Some(Duration::from_secs(5));
|
||||||
|
|
||||||
let url = reqwest_request.url().clone();
|
let url = reqwest_request.url().clone();
|
||||||
let response = globals
|
let response = globals.reqwest_client().execute(reqwest_request).await;
|
||||||
.reqwest_client()?
|
|
||||||
.build()?
|
|
||||||
.execute(reqwest_request)
|
|
||||||
.await;
|
|
||||||
|
|
||||||
match response {
|
match response {
|
||||||
Ok(mut response) => {
|
Ok(mut response) => {
|
||||||
|
|
|
@ -237,21 +237,25 @@ where
|
||||||
|
|
||||||
let url = reqwest_request.url().clone();
|
let url = reqwest_request.url().clone();
|
||||||
|
|
||||||
let mut client = globals.reqwest_client()?;
|
let client = if let Some((override_name, port)) = globals
|
||||||
if let Some((override_name, port)) = globals
|
|
||||||
.tls_name_override
|
.tls_name_override
|
||||||
.read()
|
.read()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.get(&actual_destination.hostname())
|
.get(&actual_destination.hostname())
|
||||||
{
|
{
|
||||||
client = client.resolve(
|
globals
|
||||||
&actual_destination.hostname(),
|
.reqwest_client_builder()?
|
||||||
SocketAddr::new(override_name[0], *port),
|
.resolve(
|
||||||
);
|
&actual_destination.hostname(),
|
||||||
|
SocketAddr::new(override_name[0], *port),
|
||||||
|
)
|
||||||
|
.build()?
|
||||||
// port will be ignored
|
// port will be ignored
|
||||||
}
|
} else {
|
||||||
|
globals.reqwest_client()
|
||||||
|
};
|
||||||
|
|
||||||
let response = client.build()?.execute(reqwest_request).await;
|
let response = client.execute(reqwest_request).await;
|
||||||
|
|
||||||
match response {
|
match response {
|
||||||
Ok(mut response) => {
|
Ok(mut response) => {
|
||||||
|
@ -492,9 +496,6 @@ async fn request_well_known(
|
||||||
let body: serde_json::Value = serde_json::from_str(
|
let body: serde_json::Value = serde_json::from_str(
|
||||||
&globals
|
&globals
|
||||||
.reqwest_client()
|
.reqwest_client()
|
||||||
.ok()?
|
|
||||||
.build()
|
|
||||||
.ok()?
|
|
||||||
.get(&format!(
|
.get(&format!(
|
||||||
"https://{}/.well-known/matrix/server",
|
"https://{}/.well-known/matrix/server",
|
||||||
destination
|
destination
|
||||||
|
|
Loading…
Reference in a new issue