yang-base 0.2.2

个人使用构建后端服务器
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! HTTP 客户端核心实现
//!
//! 提供 HTTP 客户端和请求构建能力;实例在启动期经
//! [`ToolsBuilder::http`](crate::tools::ToolsBuilder) 注册进应用资源,
//! 运行期通过 `Tools::http()` 或 `ActionContext::http()` 获取。

use crate::error::BaseError;
use crate::http::request::RequestBuilder;
use reqwest::{Client, Method};
use std::sync::{Arc, RwLock};
use std::time::Duration;

/// HTTP 客户端配置
///
/// 用于通过 `HttpClient::with_config` 创建自定义配置的客户端
///
/// # 示例
///
/// ```rust,ignore
/// use yang_base::http::{HttpClient, HttpClientConfig};
///
/// let config = HttpClientConfig {
///     timeout_secs: 60,
///     pool_max_idle_per_host: 10,
///     user_agent: Some("MyApp/1.0".to_string()),
///     ..Default::default()
/// };
/// let client = HttpClient::with_config(config)?;
/// ```
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct HttpClientConfig {
    /// 请求超时时间(秒),默认 30 秒
    pub timeout_secs: u64,

    /// 每个主机最大空闲连接数,默认 10
    pub pool_max_idle_per_host: usize,

    /// 连接池空闲超时时间(秒),默认 90 秒
    pub pool_idle_timeout_secs: u64,

    /// 自定义 User-Agent,默认为 None(使用 reqwest 默认值)
    pub user_agent: Option<String>,

    /// 是否接受无效的 TLS 证书,默认 false(生产环境不应设为 true)
    pub accept_invalid_certs: bool,

    /// 代理 URL,默认为 None(不使用代理)
    pub proxy_url: Option<String>,

    /// 熔断器策略,默认 None(不启用)。设为 `Some(..)` 后,对连续失败的目标
    /// host 快速失败(返回 `BaseError::HttpCircuitBreakerOpen`),按 host 分键。
    pub circuit_breaker: Option<crate::http::CircuitBreakerConfig>,
}

impl Default for HttpClientConfig {
    fn default() -> Self {
        Self {
            timeout_secs: 30,
            pool_max_idle_per_host: 10,
            pool_idle_timeout_secs: 90,
            user_agent: None,
            accept_invalid_certs: false,
            proxy_url: None,
            circuit_breaker: None,
        }
    }
}

impl HttpClientConfig {
    /// 验证 HTTP 客户端配置。
    ///
    /// 拒绝会导致请求立即失败或连接池配置退化的零值参数。
    pub fn validate(&self) -> Result<(), BaseError> {
        if self.timeout_secs == 0 {
            return Err(BaseError::ParamInvalid(
                "http.timeout_secs".to_string(),
                "HTTP 请求超时时间必须大于 0 秒".to_string(),
            ));
        }
        if self.pool_max_idle_per_host == 0 {
            return Err(BaseError::ParamInvalid(
                "http.pool_max_idle_per_host".to_string(),
                "每个主机最大空闲连接数必须大于 0".to_string(),
            ));
        }
        if self.pool_idle_timeout_secs == 0 {
            return Err(BaseError::ParamInvalid(
                "http.pool_idle_timeout_secs".to_string(),
                "连接池空闲超时时间必须大于 0 秒".to_string(),
            ));
        }
        if let Some(circuit_breaker) = &self.circuit_breaker {
            circuit_breaker.validate()?;
        }

        Ok(())
    }
}

#[cfg(test)]
mod config_tests {
    use super::*;

    #[test]
    fn test_http_client_config_validate_rejects_zero_values() {
        let invalid_configs = [
            HttpClientConfig {
                timeout_secs: 0,
                ..HttpClientConfig::default()
            },
            HttpClientConfig {
                pool_max_idle_per_host: 0,
                ..HttpClientConfig::default()
            },
            HttpClientConfig {
                pool_idle_timeout_secs: 0,
                ..HttpClientConfig::default()
            },
        ];

        for config in invalid_configs {
            let err = config.validate().expect_err("HTTP 客户端零值配置应被拒绝");

            assert!(matches!(err, BaseError::ParamInvalid(_, _)));
        }
    }

    #[test]
    fn test_with_config_rejects_invalid_config_before_building_client() {
        let config = HttpClientConfig {
            timeout_secs: 0,
            ..HttpClientConfig::default()
        };

        let err = HttpClient::with_config(config)
            .expect_err("无效 HTTP 配置应在构建 reqwest client 前被拒绝");

        assert!(matches!(err, BaseError::ParamInvalid(field, _) if field == "http.timeout_secs"));
    }

    #[test]
    fn test_http_client_config_validate_rejects_invalid_circuit_breaker_config() {
        let config = HttpClientConfig {
            circuit_breaker: Some(crate::http::CircuitBreakerConfig {
                failure_threshold: 0,
                ..crate::http::CircuitBreakerConfig::default()
            }),
            ..HttpClientConfig::default()
        };

        let err = config
            .validate()
            .expect_err("HTTP 客户端应拒绝非法熔断器配置");

        assert!(matches!(
            err,
            BaseError::ParamInvalid(field, _) if field == "http.circuit_breaker.failure_threshold"
        ));
    }
}

/// HTTP 客户端
///
/// 提供 HTTP 请求构建和发送能力。
///
/// 内部持有 `Arc<reqwest::Client>`,`clone()` 时复用同一连接池(即 `Arc::clone`),
/// 不会创建新的底层连接池。
///
/// # 示例
///
/// ```rust,ignore
/// use yang_base::http::HttpClient;
/// use yang_base::tools::ToolsBuilder;
///
/// // 启动期:把客户端注册进应用资源并冻结
/// let tools = ToolsBuilder::new().http(HttpClient::new(30)?).build()?;
///
/// // 运行期:从 Tools 获取客户端发起请求(Action 内为 ctx.http()?)
/// let response = tools.http()?
///     .get("https://api.example.com/users")
///     .send()
///     .await?;
/// ```
#[derive(Clone, Debug)]
pub struct HttpClient {
    /// reqwest 客户端(Arc 包装,clone 时复用同一连接池)
    client: Client,

    /// 默认超时时间
    default_timeout: Duration,

    /// 默认 Token(可选)
    default_token: Arc<RwLock<Option<String>>>,

    /// 熔断器(可选,默认 None)。clone 时共享同一份状态。
    circuit_breaker: Option<crate::http::CircuitBreaker>,
}

impl HttpClient {
    /// 使用结构化配置创建 HTTP 客户端
    ///
    /// # 参数
    ///
    /// - `cfg`: HTTP 客户端配置
    ///
    /// # 返回
    ///
    /// - `Ok(HttpClient)`: 客户端实例
    /// - `Err(BaseError::HttpClientCreateFailed)`: 创建失败(如代理 URL 无效)
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// use yang_base::http::{HttpClient, HttpClientConfig};
    ///
    /// let config = HttpClientConfig {
    ///     timeout_secs: 60,
    ///     pool_max_idle_per_host: 20,
    ///     ..Default::default()
    /// };
    /// let client = HttpClient::with_config(config)?;
    /// ```
    pub fn with_config(cfg: HttpClientConfig) -> Result<Self, BaseError> {
        cfg.validate()?;

        // 构建 reqwest 客户端
        let mut builder = Client::builder()
            .timeout(Duration::from_secs(cfg.timeout_secs))
            .pool_max_idle_per_host(cfg.pool_max_idle_per_host)
            .pool_idle_timeout(Duration::from_secs(cfg.pool_idle_timeout_secs))
            .danger_accept_invalid_certs(cfg.accept_invalid_certs);

        // 设置自定义 User-Agent
        if let Some(ua) = cfg.user_agent {
            builder = builder.user_agent(ua);
        }

        // 设置代理
        if let Some(proxy_url) = cfg.proxy_url {
            let proxy =
                reqwest::Proxy::all(&proxy_url).map_err(BaseError::HttpClientCreateFailed)?;
            builder = builder.proxy(proxy);
        }

        let client = builder.build().map_err(BaseError::HttpClientCreateFailed)?;

        let circuit_breaker = cfg
            .circuit_breaker
            .map(crate::http::CircuitBreaker::new)
            .transpose()?;

        Ok(Self {
            client,
            default_timeout: Duration::from_secs(cfg.timeout_secs),
            default_token: Arc::new(RwLock::new(None)),
            circuit_breaker,
        })
    }

    /// 创建新的 HTTP 客户端
    ///
    /// 委托给 `with_config`,使用默认配置并仅覆盖超时时间。
    ///
    /// # 参数
    ///
    /// - `timeout_secs`: 默认超时时间(秒)
    ///
    /// # 返回
    ///
    /// - `Ok(HttpClient)`: 客户端实例
    /// - `Err(BaseError)`: 创建失败
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// let client = HttpClient::new(30)?;
    /// ```
    pub fn new(timeout_secs: u64) -> Result<Self, BaseError> {
        // 委托给 with_config,仅覆盖超时时间
        Self::with_config(HttpClientConfig {
            timeout_secs,
            ..Default::default()
        })
    }

    /// 设置默认 Token
    ///
    /// # 参数
    ///
    /// - `token`: Token 字符串
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// let mut client = HttpClient::new(30)?;
    /// client.set_default_token("your_token".to_string());
    /// ```
    pub fn set_default_token(&self, token: String) {
        // 使用 unwrap_or_else 处理锁中毒:即使锁中毒也能恢复数据并继续写入
        let mut default_token = self
            .default_token
            .write()
            .unwrap_or_else(|p| p.into_inner());
        *default_token = Some(token);
        log::debug!("已设置默认 Token");
    }

    /// 获取默认 Token
    fn get_default_token(&self) -> Option<String> {
        // 使用 unwrap_or_else 处理锁中毒:即使锁中毒也能恢复数据并继续读取
        let default_token = self.default_token.read().unwrap_or_else(|p| p.into_inner());
        default_token.clone()
    }

    /// 创建请求构建器
    ///
    /// 注意:`self.client.clone()` 是 `Arc::clone`,复用同一底层连接池,
    /// 不会创建新的 TCP 连接池,多个请求共享连接。
    ///
    /// # 参数
    ///
    /// - `method`: HTTP 方法
    /// - `url`: 请求 URL
    ///
    /// # 返回
    ///
    /// - `RequestBuilder`: 请求构建器
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// use reqwest::Method;
    ///
    /// let response = client
    ///     .request(Method::GET, "https://api.example.com/users")
    ///     .send()
    ///     .await?;
    /// ```
    pub fn request(&self, method: Method, url: &str) -> RequestBuilder {
        // self.client.clone() 是 Arc::clone,复用同一连接池
        RequestBuilder::new(
            self.client.clone(),
            method,
            url.to_string(),
            self.default_timeout,
            self.get_default_token(),
            self.circuit_breaker.clone(),
        )
    }

    /// GET 请求
    ///
    /// # 参数
    ///
    /// - `url`: 请求 URL
    ///
    /// # 返回
    ///
    /// - `RequestBuilder`: 请求构建器
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// let response = client
    ///     .get("https://api.example.com/users")
    ///     .send()
    ///     .await?;
    /// ```
    pub fn get(&self, url: &str) -> RequestBuilder {
        self.request(Method::GET, url)
    }

    /// POST 请求
    ///
    /// # 参数
    ///
    /// - `url`: 请求 URL
    ///
    /// # 返回
    ///
    /// - `RequestBuilder`: 请求构建器
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// let response = client
    ///     .post("https://api.example.com/users")
    ///     .json(&user)?
    ///     .send()
    ///     .await?;
    /// ```
    pub fn post(&self, url: &str) -> RequestBuilder {
        self.request(Method::POST, url)
    }

    /// PUT 请求
    ///
    /// # 参数
    ///
    /// - `url`: 请求 URL
    ///
    /// # 返回
    ///
    /// - `RequestBuilder`: 请求构建器
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// let response = client
    ///     .put("https://api.example.com/users/1")
    ///     .json(&user)?
    ///     .send()
    ///     .await?;
    /// ```
    pub fn put(&self, url: &str) -> RequestBuilder {
        self.request(Method::PUT, url)
    }

    /// DELETE 请求
    ///
    /// # 参数
    ///
    /// - `url`: 请求 URL
    ///
    /// # 返回
    ///
    /// - `RequestBuilder`: 请求构建器
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// let response = client
    ///     .delete("https://api.example.com/users/1")
    ///     .send()
    ///     .await?;
    /// ```
    pub fn delete(&self, url: &str) -> RequestBuilder {
        self.request(Method::DELETE, url)
    }

    /// PATCH 请求
    ///
    /// # 参数
    ///
    /// - `url`: 请求 URL
    ///
    /// # 返回
    ///
    /// - `RequestBuilder`: 请求构建器
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// let response = client
    ///     .patch("https://api.example.com/users/1")
    ///     .json(&updates)?
    ///     .send()
    ///     .await?;
    /// ```
    pub fn patch(&self, url: &str) -> RequestBuilder {
        self.request(Method::PATCH, url)
    }
}