limiteron 0.1.1

Unified flow control framework for Rust
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
//! Copyright (c) 2026, Kirky.X
//!
//! MIT License
//!
//! 降级策略实现
//!
//! 提供降级策略管理,支持故障时自动降级到备用方案。
//!
//! # 特性
//!
//! - **多种策略**: FailOpen、FailClosed、Degraded
//! - **组件级配置**: 为不同组件配置不同策略
//! - **热更新**: 支持动态更新策略
//! - **故障注入**: 支持模拟故障进行测试

use crate::cache::l2::L2Cache;
use crate::error::{FlowGuardError, StorageError};
use ahash::AHashMap as HashMap;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;
use tracing::{debug, error, info, warn};

/// 降级策略
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum FallbackStrategy {
    /// 故障时允许所有请求(降级为全开放)
    FailOpen,
    /// 故障时拒绝所有请求(降级为全关闭)
    FailClosed,
    /// 故障时使用降级服务(如L2缓存、缓存配置)
    Degraded,
}

/// 组件类型
#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum ComponentType {
    /// Redis存储
    Redis,
    /// PostgreSQL存储
    Postgres,
    /// L3缓存
    L3Cache,
    /// 配置服务
    Config,
    /// 封禁服务
    Ban,
    /// 配额服务
    Quota,
    /// 其他组件
    Other(String),
}

impl From<&str> for ComponentType {
    fn from(s: &str) -> Self {
        match s.to_lowercase().as_str() {
            "redis" => ComponentType::Redis,
            "postgres" => ComponentType::Postgres,
            "l3_cache" => ComponentType::L3Cache,
            "config" => ComponentType::Config,
            "ban" => ComponentType::Ban,
            "quota" => ComponentType::Quota,
            other => ComponentType::Other(other.to_string()),
        }
    }
}

impl ComponentType {
    pub fn as_str(&self) -> &str {
        match self {
            ComponentType::Redis => "redis",
            ComponentType::Postgres => "postgres",
            ComponentType::L3Cache => "l3_cache",
            ComponentType::Config => "config",
            ComponentType::Ban => "ban",
            ComponentType::Quota => "quota",
            ComponentType::Other(s) => s,
        }
    }
}

/// 降级策略配置
#[derive(Debug, Clone)]
pub struct FallbackConfig {
    /// 组件类型
    pub component: ComponentType,
    /// 降级策略
    pub strategy: FallbackStrategy,
    /// 是否启用
    pub enabled: bool,
    /// 降级超时时间
    pub timeout: Duration,
    /// 最大重试次数
    pub max_retries: u32,
}

impl Default for FallbackConfig {
    fn default() -> Self {
        Self {
            component: ComponentType::Other("default".to_string()),
            strategy: FallbackStrategy::Degraded,
            enabled: true,
            timeout: Duration::from_secs(5),
            max_retries: 3,
        }
    }
}

impl FallbackConfig {
    pub fn new(component: ComponentType, strategy: FallbackStrategy) -> Self {
        Self {
            component,
            strategy,
            ..Default::default()
        }
    }

    pub fn enabled(mut self, enabled: bool) -> Self {
        self.enabled = enabled;
        self
    }

    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    pub fn max_retries(mut self, max_retries: u32) -> Self {
        self.max_retries = max_retries;
        self
    }
}

/// 降级策略管理器
pub struct FallbackManager {
    /// 策略配置
    strategies: Arc<RwLock<HashMap<ComponentType, FallbackConfig>>>,
    /// L2缓存(用于降级)
    l2_cache: Arc<L2Cache>,
    /// 故障状态
    failure_states: Arc<RwLock<HashMap<ComponentType, bool>>>,
}

impl FallbackManager {
    /// 创建新的降级策略管理器
    ///
    /// # 参数
    /// - `l2_cache`: L2缓存实例
    ///
    /// # 示例
    /// ```rust
    /// use limiteron::fallback::{FallbackManager, ComponentType};
    /// use limiteron::L2Cache;
    /// use std::sync::Arc;
    /// use std::time::Duration;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let l2_cache = Arc::new(L2Cache::new(10000, Duration::from_secs(60)));
    ///     let manager = FallbackManager::new(l2_cache);
    /// }
    /// ```
    pub fn new(l2_cache: Arc<L2Cache>) -> Self {
        info!("创建降级策略管理器");

        // 默认策略
        let mut strategies = HashMap::new();
        strategies.insert(
            ComponentType::Redis,
            FallbackConfig::new(ComponentType::Redis, FallbackStrategy::Degraded),
        );
        strategies.insert(
            ComponentType::Postgres,
            FallbackConfig::new(ComponentType::Postgres, FallbackStrategy::Degraded),
        );
        strategies.insert(
            ComponentType::L3Cache,
            FallbackConfig::new(ComponentType::L3Cache, FallbackStrategy::Degraded),
        );
        strategies.insert(
            ComponentType::Config,
            FallbackConfig::new(ComponentType::Config, FallbackStrategy::FailClosed),
        );
        strategies.insert(
            ComponentType::Ban,
            FallbackConfig::new(ComponentType::Ban, FallbackStrategy::Degraded),
        );
        strategies.insert(
            ComponentType::Quota,
            FallbackConfig::new(ComponentType::Quota, FallbackStrategy::Degraded),
        );

        Self {
            strategies: Arc::new(RwLock::new(strategies)),
            l2_cache,
            failure_states: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// 设置降级策略
    ///
    /// # 参数
    /// - `component`: 组件类型
    /// - `config`: 策略配置
    pub async fn set_strategy(&self, component: ComponentType, config: FallbackConfig) {
        info!(
            "设置降级策略: component={:?}, strategy={:?}",
            component, config.strategy
        );

        let mut strategies = self.strategies.write().await;
        strategies.insert(component, config);
    }

    /// 获取降级策略
    ///
    /// # 参数
    /// - `component`: 组件类型
    ///
    /// # 返回
    /// - 策略配置
    pub async fn get_strategy(&self, component: ComponentType) -> Option<FallbackConfig> {
        let strategies = self.strategies.read().await;
        strategies.get(&component).cloned()
    }

    /// 执行带降级策略的操作
    ///
    /// # 参数
    /// - `component`: 组件类型
    /// - `operation`: 要执行的操作
    /// - `fallback_operation`: 降级操作
    ///
    /// # 返回
    /// - `Ok(T)`: 操作成功
    /// - `Err(FlowGuardError)`: 操作失败且降级也失败
    ///
    /// # 示例
    /// ```rust
    /// use limiteron::fallback::{FallbackManager, FallbackStrategy, ComponentType};
    /// use limiteron::error::FlowGuardError;
    /// use limiteron::L2Cache;
    /// use std::time::Duration;
    /// use std::sync::Arc;
    ///
    /// # #[tokio::main]
    /// # async fn main() {
    /// let l2_cache = Arc::new(L2Cache::new(10000, Duration::from_secs(60)));
    /// let manager = FallbackManager::new(l2_cache);
    ///
    /// let result = manager.execute_with_fallback(
    ///     ComponentType::Redis,
    ///     || async { Ok::<String, FlowGuardError>("primary".to_string()) },
    ///     || async { Ok::<String, FlowGuardError>("fallback".to_string()) }
    /// ).await;
    /// # }
    /// ```
    pub async fn execute_with_fallback<F, Fut, FB, FBFut, T>(
        &self,
        component: ComponentType,
        operation: F,
        fallback_operation: FB,
    ) -> Result<T, FlowGuardError>
    where
        F: FnOnce() -> Fut,
        Fut: std::future::Future<Output = Result<T, FlowGuardError>>,
        FB: FnOnce() -> FBFut,
        FBFut: std::future::Future<Output = Result<T, FlowGuardError>>,
    {
        let config = self
            .get_strategy(component.clone())
            .await
            .unwrap_or_default();

        if !config.enabled {
            // 策略未启用,直接执行操作
            return operation().await;
        }

        // 检查是否处于故障状态
        let _is_failed = {
            let states = self.failure_states.read().await;
            *states.get(&component).unwrap_or(&false)
        };

        // 尝试执行主操作(即使在故障状态下也要尝试,以检测是否恢复)
        let result = operation().await;

        match result {
            Ok(value) => {
                // 操作成功,清除故障状态
                self.clear_failure(component).await;
                Ok(value)
            }
            Err(e) => {
                // 操作失败,根据策略处理
                warn!("组件操作失败: component={:?}, error={}", component, e);

                // 标记为故障状态
                self.set_failure(component.clone()).await;

                // 执行降级策略
                self.execute_fallback(component, config, fallback_operation)
                    .await
            }
        }
    }

    /// 执行降级策略
    async fn execute_fallback<F, Fut, T>(
        &self,
        component: ComponentType,
        config: FallbackConfig,
        fallback_operation: F,
    ) -> Result<T, FlowGuardError>
    where
        F: FnOnce() -> Fut,
        Fut: std::future::Future<Output = Result<T, FlowGuardError>>,
    {
        info!(
            "执行降级策略: component={:?}, strategy={:?}",
            component, config.strategy
        );

        match config.strategy {
            FallbackStrategy::FailOpen => {
                // 故障开放:返回默认值或允许请求
                warn!("降级策略: FailOpen - 允许请求通过");
                Err(FlowGuardError::LimitError(
                    "服务降级,但允许请求通过".to_string(),
                ))
            }
            FallbackStrategy::FailClosed => {
                // 故障关闭:拒绝请求
                error!("降级策略: FailClosed - 拒绝请求");
                Err(FlowGuardError::StorageError(StorageError::ConnectionError(
                    "服务降级,拒绝请求".to_string(),
                )))
            }
            FallbackStrategy::Degraded => {
                // 降级服务:使用备用方案
                debug!("降级策略: Degraded - 使用备用方案");
                fallback_operation().await
            }
        }
    }

    /// 标记组件为故障状态
    async fn set_failure(&self, component: ComponentType) {
        warn!("组件故障: {:?}", component);
        let mut states = self.failure_states.write().await;
        states.insert(component, true);
    }

    /// 清除组件故障状态
    async fn clear_failure(&self, component: ComponentType) {
        let mut states = self.failure_states.write().await;
        states.remove(&component);
        info!("组件恢复: {:?}", component);
    }

    /// 记录组件故障
    pub async fn record_failure(&self, component: ComponentType, _error: &str) {
        warn!("组件故障记录: {:?}", component);
        self.set_failure(component).await;
    }

    /// 获取组件故障计数
    pub async fn get_failure_count(&self, component: ComponentType) -> u32 {
        let states = self.failure_states.read().await;
        if *states.get(&component).unwrap_or(&false) {
            1
        } else {
            0
        }
    }

    /// 检查组件是否故障
    pub async fn is_failed(&self, component: ComponentType) -> bool {
        let states = self.failure_states.read().await;
        *states.get(&component).unwrap_or(&false)
    }

    /// 手动触发故障(用于测试)
    pub async fn inject_failure(&self, component: ComponentType) {
        warn!("注入故障: {:?}", component);
        self.set_failure(component).await;
    }

    /// 手动恢复故障(用于测试)
    pub async fn recover_failure(&self, component: ComponentType) {
        info!("恢复故障: {:?}", component);
        self.clear_failure(component).await;
    }

    /// 获取所有故障状态
    pub async fn get_all_failures(&self) -> Vec<ComponentType> {
        let states = self.failure_states.read().await;
        states
            .iter()
            .filter(|(_, &failed)| failed)
            .map(|(component, _)| component.clone())
            .collect()
    }

    /// 获取L2缓存
    pub fn l2_cache(&self) -> &Arc<L2Cache> {
        &self.l2_cache
    }
}

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

    #[test]
    fn test_fallback_strategy_display() {
        let strategy = FallbackStrategy::FailOpen;
        assert_eq!(format!("{:?}", strategy), "FailOpen");

        let strategy = FallbackStrategy::FailClosed;
        assert_eq!(format!("{:?}", strategy), "FailClosed");

        let strategy = FallbackStrategy::Degraded;
        assert_eq!(format!("{:?}", strategy), "Degraded");
    }

    #[test]
    fn test_component_type_from_str() {
        assert_eq!(ComponentType::from("redis"), ComponentType::Redis);
        assert_eq!(ComponentType::from("postgres"), ComponentType::Postgres);
        assert_eq!(ComponentType::from("l3_cache"), ComponentType::L3Cache);
        assert_eq!(
            ComponentType::from("other"),
            ComponentType::Other("other".to_string())
        );
    }

    #[test]
    fn test_component_type_as_str() {
        assert_eq!(ComponentType::Redis.as_str(), "redis");
        assert_eq!(ComponentType::Postgres.as_str(), "postgres");
        assert_eq!(ComponentType::L3Cache.as_str(), "l3_cache");
    }

    #[test]
    fn test_fallback_config_default() {
        let config = FallbackConfig::default();
        assert_eq!(config.strategy, FallbackStrategy::Degraded);
        assert!(config.enabled);
        assert_eq!(config.timeout, Duration::from_secs(5));
        assert_eq!(config.max_retries, 3);
    }

    #[test]
    fn test_fallback_config_builder() {
        let config = FallbackConfig::new(ComponentType::Redis, FallbackStrategy::FailOpen)
            .enabled(false)
            .timeout(Duration::from_secs(10))
            .max_retries(5);

        assert_eq!(config.component, ComponentType::Redis);
        assert_eq!(config.strategy, FallbackStrategy::FailOpen);
        assert!(!config.enabled);
        assert_eq!(config.timeout, Duration::from_secs(10));
        assert_eq!(config.max_retries, 5);
    }

    #[tokio::test]
    async fn test_fallback_manager_new() {
        let l2_cache = Arc::new(L2Cache::new(10000, Duration::from_secs(60)));
        let manager = FallbackManager::new(l2_cache);

        // 验证默认策略
        let redis_strategy = manager.get_strategy(ComponentType::Redis).await;
        assert!(redis_strategy.is_some());
        assert_eq!(redis_strategy.unwrap().strategy, FallbackStrategy::Degraded);

        let postgres_strategy = manager.get_strategy(ComponentType::Postgres).await;
        assert!(postgres_strategy.is_some());
        assert_eq!(
            postgres_strategy.unwrap().strategy,
            FallbackStrategy::Degraded
        );
    }

    #[tokio::test]
    async fn test_fallback_manager_set_strategy() {
        let l2_cache = Arc::new(L2Cache::new(10000, Duration::from_secs(60)));
        let manager = FallbackManager::new(l2_cache);

        let config = FallbackConfig::new(ComponentType::Redis, FallbackStrategy::FailOpen);
        manager.set_strategy(ComponentType::Redis, config).await;

        let strategy = manager.get_strategy(ComponentType::Redis).await;
        assert!(strategy.is_some());
        assert_eq!(strategy.unwrap().strategy, FallbackStrategy::FailOpen);
    }

    #[tokio::test]
    async fn test_fallback_manager_execute_success() {
        let l2_cache = Arc::new(L2Cache::new(10000, Duration::from_secs(60)));
        let manager = FallbackManager::new(l2_cache);

        let result = manager
            .execute_with_fallback(
                ComponentType::Redis,
                || async { Ok::<String, FlowGuardError>("primary".to_string()) },
                || async { Ok::<String, FlowGuardError>("fallback".to_string()) },
            )
            .await;

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "primary");
        assert!(!manager.is_failed(ComponentType::Redis).await);
    }

    #[tokio::test]
    async fn test_fallback_manager_execute_fail_degraded() {
        let l2_cache = Arc::new(L2Cache::new(10000, Duration::from_secs(60)));
        let manager = FallbackManager::new(l2_cache);

        let config = FallbackConfig::new(ComponentType::Redis, FallbackStrategy::Degraded);
        manager.set_strategy(ComponentType::Redis, config).await;

        let result = manager
            .execute_with_fallback(
                ComponentType::Redis,
                || async {
                    Err::<String, FlowGuardError>(FlowGuardError::StorageError(
                        StorageError::ConnectionError("connection failed".to_string()),
                    ))
                },
                || async { Ok::<String, FlowGuardError>("fallback".to_string()) },
            )
            .await;

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "fallback");
        assert!(manager.is_failed(ComponentType::Redis).await);
    }

    #[tokio::test]
    async fn test_fallback_manager_execute_fail_fail_open() {
        let l2_cache = Arc::new(L2Cache::new(10000, Duration::from_secs(60)));
        let manager = FallbackManager::new(l2_cache);

        let config = FallbackConfig::new(ComponentType::Redis, FallbackStrategy::FailOpen);
        manager.set_strategy(ComponentType::Redis, config).await;

        let result = manager
            .execute_with_fallback(
                ComponentType::Redis,
                || async {
                    Err::<String, FlowGuardError>(FlowGuardError::StorageError(
                        StorageError::ConnectionError("connection failed".to_string()),
                    ))
                },
                || async { Ok::<String, FlowGuardError>("fallback".to_string()) },
            )
            .await;

        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("服务降级,但允许请求通过"));
    }

    #[tokio::test]
    async fn test_fallback_manager_execute_fail_fail_closed() {
        let l2_cache = Arc::new(L2Cache::new(10000, Duration::from_secs(60)));
        let manager = FallbackManager::new(l2_cache);

        let config = FallbackConfig::new(ComponentType::Redis, FallbackStrategy::FailClosed);
        manager.set_strategy(ComponentType::Redis, config).await;

        let result = manager
            .execute_with_fallback(
                ComponentType::Redis,
                || async {
                    Err::<String, FlowGuardError>(FlowGuardError::StorageError(
                        StorageError::ConnectionError("connection failed".to_string()),
                    ))
                },
                || async { Ok::<String, FlowGuardError>("fallback".to_string()) },
            )
            .await;

        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("服务降级,拒绝请求"));
    }

    #[tokio::test]
    async fn test_fallback_manager_inject_failure() {
        let l2_cache = Arc::new(L2Cache::new(10000, Duration::from_secs(60)));
        let manager = FallbackManager::new(l2_cache);

        assert!(!manager.is_failed(ComponentType::Redis).await);

        manager.inject_failure(ComponentType::Redis).await;
        assert!(manager.is_failed(ComponentType::Redis).await);

        manager.recover_failure(ComponentType::Redis).await;
        assert!(!manager.is_failed(ComponentType::Redis).await);
    }

    #[tokio::test]
    async fn test_fallback_manager_get_all_failures() {
        let l2_cache = Arc::new(L2Cache::new(10000, Duration::from_secs(60)));
        let manager = FallbackManager::new(l2_cache);

        manager.inject_failure(ComponentType::Redis).await;
        manager.inject_failure(ComponentType::Postgres).await;

        let failures = manager.get_all_failures().await;
        assert_eq!(failures.len(), 2);
        assert!(failures.contains(&ComponentType::Redis));
        assert!(failures.contains(&ComponentType::Postgres));
    }

    #[tokio::test]
    async fn test_fallback_manager_recovery() {
        let l2_cache = Arc::new(L2Cache::new(10000, Duration::from_secs(60)));
        let manager = FallbackManager::new(l2_cache);

        // 第一次失败
        let result = manager
            .execute_with_fallback(
                ComponentType::Redis,
                || async {
                    Err::<String, FlowGuardError>(FlowGuardError::StorageError(
                        StorageError::ConnectionError("connection failed".to_string()),
                    ))
                },
                || async { Ok::<String, FlowGuardError>("fallback".to_string()) },
            )
            .await;

        assert!(result.is_ok());
        assert!(manager.is_failed(ComponentType::Redis).await);

        // 第二次成功,应该清除故障状态
        let result = manager
            .execute_with_fallback(
                ComponentType::Redis,
                || async { Ok::<String, FlowGuardError>("recovered".to_string()) },
                || async { Ok::<String, FlowGuardError>("fallback".to_string()) },
            )
            .await;

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "recovered");
        assert!(!manager.is_failed(ComponentType::Redis).await);
    }

    #[tokio::test]
    async fn test_fallback_manager_l2_cache() {
        let l2_cache = Arc::new(L2Cache::new(10000, Duration::from_secs(60)));
        let manager = FallbackManager::new(l2_cache);

        let cache = manager.l2_cache();
        assert_eq!(cache.len().await, 0);
    }
}