Skip to main content

synapse_proxy/
http_client.rs

1use std::time::Duration;
2
3/// Build the shared reqwest client used for upstream forwarding.
4///
5/// Without explicit timeouts a broken MCS/DNS path can hang until the caller
6/// gives up (e.g. sandbox `python` exit 124) while the upstream never logs a
7/// request. Configure via:
8///
9/// - `SYNAPSE_PROXY_UPSTREAM_CONNECT_TIMEOUT_SECS` (default 10)
10/// - `SYNAPSE_PROXY_UPSTREAM_TIMEOUT_SECS` (default 120)
11pub fn build_http_client() -> reqwest::Result<reqwest::Client> {
12    let connect_secs = std::env::var("SYNAPSE_PROXY_UPSTREAM_CONNECT_TIMEOUT_SECS")
13        .ok()
14        .and_then(|s| s.parse().ok())
15        .unwrap_or(10);
16    let request_secs = std::env::var("SYNAPSE_PROXY_UPSTREAM_TIMEOUT_SECS")
17        .ok()
18        .and_then(|s| s.parse().ok())
19        .unwrap_or(120);
20
21    reqwest::Client::builder()
22        .connect_timeout(Duration::from_secs(connect_secs))
23        .timeout(Duration::from_secs(request_secs))
24        .build()
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30
31    #[test]
32    fn builds_client_with_defaults() {
33        build_http_client().expect("client");
34    }
35}