Skip to main content

x_one/xhttp/
config.rs

1//! xhttp 配置结构体
2//!
3//! 对应 `application.yml` 中的 `XHttp` 节点。
4//!
5//! ```yaml
6//! XHttp:
7//!   Timeout: "30s"
8//!   DialTimeout: "10s"
9//!   PoolMaxIdlePerHost: 10
10//! ```
11
12use serde::Deserialize;
13
14/// XHttp 配置 key
15pub const XHTTP_CONFIG_KEY: &str = "XHttp";
16
17/// XHttp 配置
18///
19/// # 配置示例
20/// ```yaml
21/// XHttp:
22///   Timeout: "30s"
23///   DialTimeout: "10s"
24///   DialKeepAlive: "30s"
25///   PoolMaxIdlePerHost: 10
26/// ```
27#[derive(Debug, Deserialize, Clone)]
28#[serde(default)]
29pub struct XHttpConfig {
30    /// 请求超时(duration 字符串,默认 "30s")
31    #[serde(rename = "Timeout")]
32    pub timeout: String,
33
34    /// 连接超时(duration 字符串,默认 "10s")
35    #[serde(rename = "DialTimeout")]
36    pub dial_timeout: String,
37
38    /// Keep-alive 时间(duration 字符串,默认 "30s")
39    #[serde(rename = "DialKeepAlive")]
40    pub dial_keep_alive: String,
41
42    /// 连接池每个主机最大空闲数(默认 10)
43    #[serde(rename = "PoolMaxIdlePerHost")]
44    pub pool_max_idle_per_host: usize,
45}
46
47impl Default for XHttpConfig {
48    fn default() -> Self {
49        Self {
50            timeout: "30s".into(),
51            dial_timeout: "10s".into(),
52            dial_keep_alive: "30s".into(),
53            pool_max_idle_per_host: 10,
54        }
55    }
56}
57
58/// 加载 XHttp 配置
59pub(crate) fn load_config() -> XHttpConfig {
60    crate::xconfig::parse_config::<XHttpConfig>(XHTTP_CONFIG_KEY).unwrap_or_default()
61}