li_http_client/
config.rs

1//! Configuration for `HttpClient`s.
2
3use std::fmt::Debug;
4use std::time::Duration;
5
6/// Configuration for `HttpClient`s.
7#[non_exhaustive]
8#[derive(Clone)]
9pub struct Config {
10    /// HTTP/1.1 `keep-alive` (connection pooling).
11    ///
12    /// Default: `true`.
13    ///
14    /// Note: Does nothing on `wasm_client`.
15    pub http_keep_alive: bool,
16    /// TCP `NO_DELAY`.
17    ///
18    /// Default: `false`.
19    ///
20    /// Note: Does nothing on `wasm_client`.
21    pub tcp_no_delay: bool,
22    /// Connection timeout duration.
23    ///
24    /// Default: `Some(Duration::from_secs(60))`.
25    pub timeout: Option<Duration>,
26    /// Maximum number of simultaneous connections that this client is allowed to keep open to individual hosts at one time.
27    ///
28    /// Default: `50`.
29    /// This number is based on a few random benchmarks and see whatever gave decent perf vs resource use in Orogene.
30    ///
31    /// Note: The behavior of this is different depending on the backend in use.
32    /// - `h1_client`: `0` is disallowed and asserts as otherwise it would cause a semaphore deadlock.
33    /// - `curl_client`: `0` allows for limitless connections per host.
34    /// - `hyper_client`: No effect. Hyper does not support such an option.
35    /// - `wasm_client`: No effect. Web browsers do not support such an option.
36    pub max_connections_per_host: usize,
37    /// TLS Configuration (Rustls)
38    #[cfg_attr(feature = "docs", doc(cfg(feature = "h1_client")))]
39    #[cfg(all(feature = "h1_client", feature = "rustls"))]
40    pub tls_config: Option<std::sync::Arc<rustls_crate::ClientConfig>>,
41    /// TLS Configuration (Native TLS)
42    #[cfg_attr(feature = "docs", doc(cfg(feature = "h1_client")))]
43    #[cfg(all(feature = "h1_client", feature = "native-tls", not(feature = "rustls")))]
44    pub tls_config: Option<std::sync::Arc<async_native_tls::TlsConnector>>,
45}
46
47impl Debug for Config {
48    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49        let mut dbg_struct = f.debug_struct("Config");
50        dbg_struct
51            .field("http_keep_alive", &self.http_keep_alive)
52            .field("tcp_no_delay", &self.tcp_no_delay)
53            .field("timeout", &self.timeout)
54            .field("max_connections_per_host", &self.max_connections_per_host);
55
56        #[cfg(all(feature = "h1_client", feature = "rustls"))]
57        {
58            if self.tls_config.is_some() {
59                dbg_struct.field("tls_config", &"Some(rustls::ClientConfig)");
60            } else {
61                dbg_struct.field("tls_config", &"None");
62            }
63        }
64        #[cfg(all(feature = "h1_client", feature = "native-tls", not(feature = "rustls")))]
65        {
66            dbg_struct.field("tls_config", &self.tls_config);
67        }
68
69        dbg_struct.finish()
70    }
71}
72
73impl Config {
74    /// Construct new empty config.
75    pub fn new() -> Self {
76        Self {
77            http_keep_alive: true,
78            tcp_no_delay: false,
79            timeout: Some(Duration::from_secs(60)),
80            max_connections_per_host: 50,
81            #[cfg(all(feature = "h1_client", any(feature = "rustls", feature = "native-tls")))]
82            tls_config: None,
83        }
84    }
85}
86
87impl Default for Config {
88    fn default() -> Self {
89        Self::new()
90    }
91}
92
93impl Config {
94    /// Set HTTP/1.1 `keep-alive` (connection pooling).
95    pub fn set_http_keep_alive(mut self, keep_alive: bool) -> Self {
96        self.http_keep_alive = keep_alive;
97        self
98    }
99
100    /// Set TCP `NO_DELAY`.
101    pub fn set_tcp_no_delay(mut self, no_delay: bool) -> Self {
102        self.tcp_no_delay = no_delay;
103        self
104    }
105
106    /// Set connection timeout duration.
107    pub fn set_timeout(mut self, timeout: Option<Duration>) -> Self {
108        self.timeout = timeout;
109        self
110    }
111
112    /// Set the maximum number of simultaneous connections that this client is allowed to keep open to individual hosts at one time.
113    pub fn set_max_connections_per_host(mut self, max_connections_per_host: usize) -> Self {
114        self.max_connections_per_host = max_connections_per_host;
115        self
116    }
117
118    /// Set TLS Configuration (Rustls)
119    #[cfg_attr(feature = "docs", doc(cfg(feature = "h1_client")))]
120    #[cfg(all(feature = "h1_client", feature = "rustls"))]
121    pub fn set_tls_config(
122        mut self,
123        tls_config: Option<std::sync::Arc<rustls_crate::ClientConfig>>,
124    ) -> Self {
125        self.tls_config = tls_config;
126        self
127    }
128    /// Set TLS Configuration (Native TLS)
129    #[cfg_attr(feature = "docs", doc(cfg(feature = "h1_client")))]
130    #[cfg(all(feature = "h1_client", feature = "native-tls", not(feature = "rustls")))]
131    pub fn set_tls_config(
132        mut self,
133        tls_config: Option<std::sync::Arc<async_native_tls::TlsConnector>>,
134    ) -> Self {
135        self.tls_config = tls_config;
136        self
137    }
138}