fix(server): 限流 fail-close 默认开启 + 配置测试
生产安全:Redis 不可达时默认拒绝请求(503)而非放行。 - config/default.toml: fail_close 默认值 false → true - config.rs: Default + serde default 均改为 true - 新增 2 个单元测试验证默认值和 serde 行为
This commit is contained in:
@@ -127,14 +127,18 @@ impl StorageConfig {
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct RateLimitConfig {
|
||||
/// Redis 不可达时是否拒绝请求(生产环境必须为 true)。
|
||||
#[serde(default)]
|
||||
/// Redis 不可达时是否拒绝请求。默认 true(安全优先)。
|
||||
#[serde(default = "default_fail_close")]
|
||||
pub fail_close: bool,
|
||||
}
|
||||
|
||||
fn default_fail_close() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
impl Default for RateLimitConfig {
|
||||
fn default() -> Self {
|
||||
Self { fail_close: false }
|
||||
Self { fail_close: true }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,3 +158,20 @@ impl AppConfig {
|
||||
Ok(app_config)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn rate_limit_default_is_fail_close() {
|
||||
let config = RateLimitConfig::default();
|
||||
assert!(config.fail_close, "RateLimitConfig 默认应为 fail_close = true");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serde_default_uses_custom_fn() {
|
||||
let config: RateLimitConfig = serde_json::from_str("{}").unwrap();
|
||||
assert!(config.fail_close, "serde 反序列化缺失字段时应使用 default_fail_close() = true");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user