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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
//! Copyright (c) 2026, Kirky.X
//!
//! MIT License
//!
//! 配额控制器模块
//!
//! 实现配额控制功能,支持多种配额类型、滑动窗口重置、透支功能和告警机制。

/// 默认配额限制
pub const DEFAULT_QUOTA_LIMIT: u64 = 1000;

/// 默认窗口大小(1小时)
pub const DEFAULT_WINDOW_SIZE_SECS: u64 = 3600;

/// 默认去重窗口(5分钟)
pub const DEFAULT_DEDUP_WINDOW_SECS: u64 = 300;

/// 默认透支限制百分比
pub const DEFAULT_OVERDRAFT_LIMIT_PERCENT: u8 = 20;

use crate::error::{ConsumeResult, FlowGuardError};
use crate::storage::QuotaStorage;
use chrono::{DateTime, Duration, Utc};
use dashmap::DashMap;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::time::Duration as StdDuration;

/// 配额类型
#[cfg(feature = "quota-control")]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum QuotaType {
    /// 令牌配额
    Token,
    /// 金额配额
    Money,
    /// 计数配额
    Count,
}

impl QuotaType {
    /// 从字符串解析配额类型
    pub fn parse(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "token" => Some(QuotaType::Token),
            "money" => Some(QuotaType::Money),
            "count" => Some(QuotaType::Count),
            _ => None,
        }
    }

    /// 转换为字符串
    pub fn as_str(&self) -> &'static str {
        match self {
            QuotaType::Token => "token",
            QuotaType::Money => "money",
            QuotaType::Count => "count",
        }
    }
}

/// 配额配置
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg(feature = "quota-control")]
pub struct QuotaConfig {
    /// 配额类型
    pub quota_type: QuotaType,
    /// 配额上限
    pub limit: u64,
    /// 窗口大小(秒)
    pub window_size: u64,
    /// 是否允许透支
    pub allow_overdraft: bool,
    /// 透支上限(配额的百分比,0-100)
    pub overdraft_limit_percent: u8,
    /// 告警配置
    pub alert_config: AlertConfig,
}

impl Default for QuotaConfig {
    fn default() -> Self {
        Self {
            quota_type: QuotaType::Count,
            limit: DEFAULT_QUOTA_LIMIT,
            window_size: DEFAULT_WINDOW_SIZE_SECS,
            allow_overdraft: false,
            overdraft_limit_percent: DEFAULT_OVERDRAFT_LIMIT_PERCENT,
            alert_config: AlertConfig::default(),
        }
    }
}

/// 告警配置
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg(feature = "quota-control")]
pub struct AlertConfig {
    /// 是否启用告警
    pub enabled: bool,
    /// 告警阈值(百分比)
    pub thresholds: Vec<u8>,
    /// 告警渠道
    pub channels: Vec<AlertChannel>,
    /// 告警去重时间窗口(秒)
    pub dedup_window: u64,
}

impl Default for AlertConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            thresholds: vec![80, 90, 100],
            channels: vec![AlertChannel::Log],
            dedup_window: DEFAULT_DEDUP_WINDOW_SECS,
        }
    }
}

/// 告警渠道
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg(feature = "quota-control")]
pub enum AlertChannel {
    /// 日志告警
    Log,
    /// Webhook 告警
    Webhook { url: String },
}

/// 告警信息
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg(feature = "quota-control")]
pub struct AlertInfo {
    /// 用户ID
    pub user_id: String,
    /// 资源
    pub resource: String,
    /// 配额类型
    pub quota_type: QuotaType,
    /// 告警阈值(百分比)
    pub threshold: u8,
    /// 当前使用量
    pub current_usage: u64,
    /// 配额上限
    pub limit: u64,
    /// 触发时间
    pub triggered_at: DateTime<Utc>,
}

/// 配额状态
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg(feature = "quota-control")]
pub struct QuotaState {
    /// 已消费量
    pub consumed: u64,
    /// 窗口开始时间
    pub window_start: DateTime<Utc>,
    /// 窗口结束时间
    pub window_end: DateTime<Utc>,
}

/// 配额控制器
#[cfg(feature = "quota-control")]
pub struct QuotaController<S: QuotaStorage> {
    /// 存储后端
    storage: Arc<S>,
    /// 配额配置
    config: QuotaConfig,
    /// 告警去重缓存(key: user_id:resource:threshold, value: last_alert_time)
    alert_dedup: Arc<DashMap<String, DateTime<Utc>>>,
}

impl<S: QuotaStorage + Clone + 'static> Clone for QuotaController<S> {
    fn clone(&self) -> Self {
        Self {
            storage: self.storage.clone(),
            config: self.config.clone(),
            alert_dedup: self.alert_dedup.clone(),
        }
    }
}

impl<S: QuotaStorage + 'static> QuotaController<S> {
    /// 创建新的配额控制器
    ///
    /// # 参数
    /// - `storage`: 存储后端
    /// - `config`: 配额配置
    ///
    /// # 示例
    /// ```rust
    /// use limiteron::quota_controller::{QuotaController, QuotaConfig, QuotaType};
    /// use limiteron::storage::MockQuotaStorage;
    ///
    /// let config = QuotaConfig {
    ///     quota_type: QuotaType::Count,
    ///     limit: 1000,
    ///     window_size: 3600,
    ///     allow_overdraft: true,
    ///     overdraft_limit_percent: 20,
    ///     alert_config: Default::default(),
    /// };
    /// let controller = QuotaController::new(MockQuotaStorage, config);
    /// ```
    pub fn new(storage: S, config: QuotaConfig) -> Self {
        Self {
            storage: Arc::new(storage),
            config,
            alert_dedup: Arc::new(DashMap::new()),
        }
    }

    /// 消费配额
    ///
    /// # 参数
    /// - `user_id`: 用户ID
    /// - `resource`: 资源标识
    /// - `cost`: 消费数量
    ///
    /// # 返回
    /// - `Ok(result)`: 消费结果
    /// - `Err(error)`: 错误信息
    ///
    /// # 示例
    /// ```rust
    /// # use limiteron::quota_controller::{QuotaController, QuotaConfig, QuotaType};
    /// # use limiteron::storage::MockQuotaStorage;
    /// #
    /// # let controller = QuotaController::new(MockQuotaStorage, QuotaConfig::default());
    /// #
    /// # async {
    /// let result = controller.consume("user123", "api_call", 10).await.unwrap();
    /// println!("Allowed: {}, Remaining: {}", result.allowed, result.remaining);
    /// # };
    /// ```
    pub async fn consume(
        &self,
        user_id: &str,
        resource: &str,
        cost: u64,
    ) -> Result<ConsumeResult, FlowGuardError> {
        // 验证消费数量
        if cost == 0 {
            return Ok(ConsumeResult {
                allowed: true,
                remaining: self.config.limit,
                alert_triggered: false,
            });
        }

        // 获取当前配额状态
        let quota_state = self.get_or_create_quota_state(user_id, resource).await?;

        // 检查窗口是否需要重置
        let updated_state = self.check_and_reset_window(quota_state).await?;

        // 计算可透支上限(使用 checked_mul 防止整数溢出)
        let overdraft_limit = if self.config.allow_overdraft {
            self.config
                .limit
                .checked_mul(self.config.overdraft_limit_percent as u64)
                .and_then(|v| v.checked_div(100))
                .unwrap_or(u64::MAX / 2) // 如果溢出,使用安全值
        } else {
            0
        };

        // 计算总限制(使用 checked_add 防止整数溢出)
        let total_limit = self
            .config
            .limit
            .checked_add(overdraft_limit)
            .unwrap_or(u64::MAX / 2); // 如果溢出,使用安全值

        // 检查是否超过总限制
        if updated_state.consumed + cost > total_limit {
            return Ok(ConsumeResult {
                allowed: false,
                remaining: total_limit.saturating_sub(updated_state.consumed),
                alert_triggered: false,
            });
        }

        // 更新消费量
        let new_consumed = updated_state.consumed + cost;

        // 保存到存储
        self.save_quota_state(user_id, resource, &updated_state, new_consumed)
            .await?;

        // 计算剩余配额
        let remaining = total_limit.saturating_sub(new_consumed);

        // 检查告警
        let alert_triggered = self
            .check_and_trigger_alert(user_id, resource, new_consumed)
            .await?;

        Ok(ConsumeResult {
            allowed: true,
            remaining,
            alert_triggered,
        })
    }

    /// 获取配额状态
    ///
    /// # 参数
    /// - `user_id`: 用户ID
    /// - `resource`: 资源标识
    ///
    /// # 返回
    /// - `Ok(Some(state))`: 配额状态
    /// - `Ok(None)`: 配额不存在
    /// - `Err(error)`: 错误信息
    pub async fn get_quota(
        &self,
        user_id: &str,
        resource: &str,
    ) -> Result<Option<QuotaState>, FlowGuardError> {
        let quota_info = self
            .storage
            .get_quota(user_id, resource)
            .await
            .map_err(FlowGuardError::StorageError)?;

        if let Some(info) = quota_info {
            Ok(Some(QuotaState {
                consumed: info.consumed,
                window_start: info.window_start,
                window_end: info.window_end,
            }))
        } else {
            Ok(None)
        }
    }

    /// 重置配额
    ///
    /// # 参数
    /// - `user_id`: 用户ID
    /// - `resource`: 资源标识
    ///
    /// # 返回
    /// - `Ok(())`: 重置成功
    /// - `Err(error)`: 错误信息
    pub async fn reset_quota(&self, user_id: &str, resource: &str) -> Result<(), FlowGuardError> {
        self.storage
            .reset(
                user_id,
                resource,
                self.config.limit,
                StdDuration::from_secs(self.config.window_size),
            )
            .await
            .map_err(FlowGuardError::StorageError)?;

        Ok(())
    }

    /// 获取或创建配额状态
    async fn get_or_create_quota_state(
        &self,
        user_id: &str,
        resource: &str,
    ) -> Result<QuotaState, FlowGuardError> {
        if let Some(state) = self.get_quota(user_id, resource).await? {
            return Ok(state);
        }

        // 创建新的配额状态
        let now = Utc::now();
        let window_start = now;
        let window_end = now + Duration::seconds(self.config.window_size as i64);

        Ok(QuotaState {
            consumed: 0,
            window_start,
            window_end,
        })
    }

    /// 检查并重置窗口
    ///
    /// 实现滑动窗口重置逻辑:如果当前时间超过窗口结束时间,
    /// 则计算新的窗口时间,并按比例保留配额消费量。
    async fn check_and_reset_window(
        &self,
        state: QuotaState,
    ) -> Result<QuotaState, FlowGuardError> {
        let now = Utc::now();

        // 如果当前时间在窗口内,不需要重置
        if now < state.window_end {
            return Ok(state);
        }

        // 计算窗口跨越情况
        let window_duration = Duration::seconds(self.config.window_size as i64);
        let elapsed = now.signed_duration_since(state.window_start);
        let windows_passed = (elapsed.num_seconds() / window_duration.num_seconds()) as u64;

        // 计算新窗口时间(使用 checked_mul 防止整数溢出)
        // 限制 windows_passed 避免溢出
        let safe_windows_passed = windows_passed.min(i32::MAX as u64) as i32;
        let new_window_start = state.window_start + window_duration * safe_windows_passed;
        let new_window_end = new_window_start + window_duration;

        // 滑动窗口重置:根据时间比例保留消费量
        // 例如:如果窗口已经过去 50%,则保留 50% 的消费量
        let window_elapsed = now.signed_duration_since(state.window_start);
        let window_progress = (window_elapsed.num_milliseconds() as f64
            / window_duration.num_milliseconds() as f64)
            .min(1.0);

        // 计算应该保留的消费量
        let retained_consumed = if windows_passed >= 1 {
            // 如果跨越了至少一个完整窗口,完全重置
            0
        } else {
            // 单个窗口内,按比例保留
            (state.consumed as f64 * (1.0 - window_progress)) as u64
        };

        Ok(QuotaState {
            consumed: retained_consumed,
            window_start: new_window_start,
            window_end: new_window_end,
        })
    }

    /// 保存配额状态
    async fn save_quota_state(
        &self,
        user_id: &str,
        resource: &str,
        state: &QuotaState,
        new_consumed: u64,
    ) -> Result<(), FlowGuardError> {
        // 使用存储的 consume 方法更新配额
        // 计算总限制(防止整数溢出)
        let overdraft_limit = if self.config.allow_overdraft {
            self.config
                .limit
                .checked_mul(self.config.overdraft_limit_percent as u64)
                .and_then(|v| v.checked_div(100))
                .unwrap_or(u64::MAX / 2)
        } else {
            0
        };

        let total_limit = self
            .config
            .limit
            .checked_add(overdraft_limit)
            .unwrap_or(u64::MAX / 2);

        let _result = self
            .storage
            .consume(
                user_id,
                resource,
                new_consumed.saturating_sub(state.consumed),
                total_limit,
                StdDuration::from_secs(self.config.window_size),
            )
            .await
            .map_err(FlowGuardError::StorageError)?;

        Ok(())
    }

    /// 检查并触发告警
    async fn check_and_trigger_alert(
        &self,
        user_id: &str,
        resource: &str,
        consumed: u64,
    ) -> Result<bool, FlowGuardError> {
        if !self.config.alert_config.enabled {
            return Ok(false);
        }

        // 计算使用率
        let usage_percent = if self.config.limit > 0 {
            (consumed as f64 / self.config.limit as f64 * 100.0) as u8
        } else {
            100
        };

        let mut alert_triggered = false;

        // 检查每个告警阈值
        for &threshold in &self.config.alert_config.thresholds {
            if usage_percent >= threshold {
                // 检查是否需要去重
                let dedup_key = format!("{}:{}:{}", user_id, resource, threshold);

                let should_alert = {
                    if let Some(last_alert_time) = self.alert_dedup.get(&dedup_key) {
                        let elapsed = Utc::now().signed_duration_since(*last_alert_time);
                        elapsed.num_seconds() as u64 >= self.config.alert_config.dedup_window
                    } else {
                        true
                    }
                };

                if should_alert {
                    // 创建告警信息
                    let alert_info = AlertInfo {
                        user_id: user_id.to_string(),
                        resource: resource.to_string(),
                        quota_type: self.config.quota_type,
                        threshold,
                        current_usage: consumed,
                        limit: self.config.limit,
                        triggered_at: Utc::now(),
                    };

                    // 异步发送告警
                    self.send_alert(alert_info).await;

                    // 更新去重缓存
                    self.alert_dedup.insert(dedup_key, Utc::now());

                    alert_triggered = true;
                }
            }
        }

        Ok(alert_triggered)
    }

    /// 发送告警
    async fn send_alert(&self, alert_info: AlertInfo) {
        for channel in &self.config.alert_config.channels {
            let channel = channel.clone();
            let alert_info = alert_info.clone();

            // 使用 tokio::spawn 异步发送告警,不阻塞主流程
            tokio::spawn(async move {
                match channel {
                    AlertChannel::Log => {
                        tracing::warn!(
                            user_id = %alert_info.user_id,
                            resource = %alert_info.resource,
                            quota_type = %alert_info.quota_type.as_str(),
                            threshold = alert_info.threshold,
                            current_usage = alert_info.current_usage,
                            limit = alert_info.limit,
                            triggered_at = %alert_info.triggered_at.format("%Y-%m-%d %H:%M:%S UTC"),
                            "配额告警触发"
                        );
                    }
                    AlertChannel::Webhook { url } => {
                        // 发送 Webhook 告警
                        if let Err(e) = send_webhook_alert(&url, &alert_info).await {
                            tracing::error!(error = %e, "发送 Webhook 告警失败");
                        }
                    }
                }
            });
        }
    }

    /// 获取配置
    pub fn config(&self) -> &QuotaConfig {
        &self.config
    }

    /// 更新配置
    pub fn update_config(&mut self, config: QuotaConfig) {
        self.config = config;
    }

    /// 清理过期的告警去重记录
    pub fn cleanup_alert_dedup(&self) {
        let now = Utc::now();
        let dedup_window = Duration::seconds(self.config.alert_config.dedup_window as i64);

        self.alert_dedup.retain(|_, last_alert_time| {
            now.signed_duration_since(*last_alert_time) < dedup_window
        });
    }
}

/// 发送 Webhook 告警
///
/// 注意:此功能需要启用 `webhook` feature 并添加 `reqwest` 依赖。
/// 如果未启用,将返回错误。
#[cfg(feature = "webhook")]
async fn send_webhook_alert(
    url: &str,
    alert_info: &AlertInfo,
) -> Result<(), Box<dyn std::error::Error>> {
    let client = reqwest::Client::new();
    let response = client
        .post(url)
        .json(alert_info)
        .timeout(StdDuration::from_secs(5))
        .send()
        .await?;

    if response.status().is_success() {
        Ok(())
    } else {
        Err(format!("Webhook 返回错误状态码: {}", response.status()).into())
    }
}

/// 发送 Webhook 告警(未启用 webhook feature 时的存根实现)
#[cfg(not(feature = "webhook"))]
async fn send_webhook_alert(
    _url: &str,
    _alert_info: &AlertInfo,
) -> Result<(), Box<dyn std::error::Error>> {
    Err("Webhook 功能未启用,请启用 'webhook' feature".into())
}

// ============================================================================
// 单元测试
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::error::StorageError;
    use crate::storage::{QuotaInfo, QuotaStorage};
    use ahash::AHashMap as HashMap;
    use async_trait::async_trait;
    use std::sync::Mutex;

    /// 测试用的配额存储实现
    struct TestQuotaStorage {
        quotas: Mutex<HashMap<String, QuotaInfo>>,
    }

    impl TestQuotaStorage {
        fn new() -> Self {
            Self {
                quotas: Mutex::new(HashMap::new()),
            }
        }
    }

    #[async_trait]
    impl QuotaStorage for TestQuotaStorage {
        async fn get_quota(
            &self,
            user_id: &str,
            resource: &str,
        ) -> Result<Option<QuotaInfo>, StorageError> {
            let key = format!("{}:{}", user_id, resource);
            Ok(self.quotas.lock().unwrap().get(&key).cloned())
        }

        async fn consume(
            &self,
            user_id: &str,
            resource: &str,
            cost: u64,
            limit: u64,
            window: StdDuration,
        ) -> Result<ConsumeResult, StorageError> {
            let key = format!("{}:{}", user_id, resource);
            let mut quotas = self.quotas.lock().unwrap();

            let quota_info = quotas.entry(key.clone()).or_insert_with(|| {
                let now = Utc::now();
                QuotaInfo {
                    consumed: 0,
                    limit,
                    window_start: now,
                    window_end: now
                        + Duration::from_std(window)
                            .unwrap_or(Duration::seconds(DEFAULT_WINDOW_SIZE_SECS as i64)),
                }
            });

            // 检查窗口是否过期
            let now = Utc::now();
            if now >= quota_info.window_end {
                // 窗口已过期,重置消费量
                quota_info.consumed = 0;
                quota_info.window_start = now;
                quota_info.window_end = now
                    + Duration::from_std(window)
                        .unwrap_or(Duration::seconds(DEFAULT_WINDOW_SIZE_SECS as i64));
                quota_info.limit = limit;
            }

            if quota_info.consumed + cost > quota_info.limit {
                return Ok(ConsumeResult {
                    allowed: false,
                    remaining: quota_info.limit - quota_info.consumed,
                    alert_triggered: false,
                });
            }

            quota_info.consumed += cost;

            Ok(ConsumeResult {
                allowed: true,
                remaining: quota_info.limit - quota_info.consumed,
                alert_triggered: false,
            })
        }

        async fn reset(
            &self,
            user_id: &str,
            resource: &str,
            limit: u64,
            window: StdDuration,
        ) -> Result<(), StorageError> {
            let key = format!("{}:{}", user_id, resource);
            let mut quotas = self.quotas.lock().unwrap();

            if let Some(quota_info) = quotas.get_mut(&key) {
                quota_info.consumed = 0;
                quota_info.limit = limit;
                let now = Utc::now();
                quota_info.window_start = now;
                quota_info.window_end = now
                    + Duration::from_std(window)
                        .unwrap_or(Duration::seconds(DEFAULT_WINDOW_SIZE_SECS as i64));
            }

            Ok(())
        }
    }

    /// 测试配额类型解析
    #[test]
    fn test_quota_type_parse() {
        assert_eq!(QuotaType::parse("token"), Some(QuotaType::Token));
        assert_eq!(QuotaType::parse("money"), Some(QuotaType::Money));
        assert_eq!(QuotaType::parse("count"), Some(QuotaType::Count));
        assert_eq!(QuotaType::parse("unknown"), None);
    }

    /// 测试配额类型字符串转换
    #[test]
    fn test_quota_type_as_str() {
        assert_eq!(QuotaType::Token.as_str(), "token");
        assert_eq!(QuotaType::Money.as_str(), "money");
        assert_eq!(QuotaType::Count.as_str(), "count");
    }

    /// 测试配额配置默认值
    #[test]
    fn test_quota_config_default() {
        let config = QuotaConfig::default();
        assert_eq!(config.quota_type, QuotaType::Count);
        assert_eq!(config.limit, 1000);
        assert_eq!(config.window_size, 3600);
        assert!(!config.allow_overdraft);
        assert_eq!(config.overdraft_limit_percent, 20);
        assert!(config.alert_config.enabled);
    }

    /// 测试告警配置默认值
    #[test]
    fn test_alert_config_default() {
        let config = AlertConfig::default();
        assert!(config.enabled);
        assert_eq!(config.thresholds, vec![80, 90, 100]);
        assert_eq!(config.channels, vec![AlertChannel::Log]);
        assert_eq!(config.dedup_window, 300);
    }

    /// 测试创建配额控制器
    #[test]
    fn test_quota_controller_new() {
        let storage = TestQuotaStorage::new();
        let config = QuotaConfig::default();
        let controller = QuotaController::new(storage, config);

        assert_eq!(controller.config().limit, 1000);
    }

    /// 测试消费配额 - 基本场景
    #[tokio::test]
    async fn test_consume_basic() {
        let storage = TestQuotaStorage::new();
        let config = QuotaConfig {
            quota_type: QuotaType::Count,
            limit: 100,
            window_size: 3600,
            allow_overdraft: false,
            overdraft_limit_percent: 0,
            alert_config: AlertConfig {
                enabled: false,
                ..Default::default()
            },
        };

        let controller = QuotaController::new(storage, config);

        // 消费 10 个配额
        let result = controller.consume("user1", "resource1", 10).await.unwrap();
        assert!(result.allowed);
        assert_eq!(result.remaining, 90);
        assert!(!result.alert_triggered);

        // 再消费 20 个配额
        let result = controller.consume("user1", "resource1", 20).await.unwrap();
        assert!(result.allowed);
        assert_eq!(result.remaining, 70);
    }

    /// 测试消费配额 - 超过限制
    #[tokio::test]
    async fn test_consume_exceeds_limit() {
        let storage = TestQuotaStorage::new();
        let config = QuotaConfig {
            quota_type: QuotaType::Count,
            limit: 100,
            window_size: 3600,
            allow_overdraft: false,
            overdraft_limit_percent: 0,
            alert_config: AlertConfig {
                enabled: false,
                ..Default::default()
            },
        };

        let controller = QuotaController::new(storage, config);

        // 消费 100 个配额
        let result = controller.consume("user1", "resource1", 100).await.unwrap();
        assert!(result.allowed);
        assert_eq!(result.remaining, 0);

        // 尝试再消费 1 个配额,应该被拒绝
        let result = controller.consume("user1", "resource1", 1).await.unwrap();
        assert!(!result.allowed);
        assert_eq!(result.remaining, 0);
    }

    /// 测试透支功能
    #[tokio::test]
    async fn test_overdraft() {
        let storage = TestQuotaStorage::new();
        let config = QuotaConfig {
            quota_type: QuotaType::Count,
            limit: 100,
            window_size: 3600,
            allow_overdraft: true,
            overdraft_limit_percent: 20,
            alert_config: AlertConfig {
                enabled: false,
                ..Default::default()
            },
        };

        let controller = QuotaController::new(storage, config);

        // 消费 100 个配额(达到上限)
        let result = controller.consume("user1", "resource1", 100).await.unwrap();
        assert!(result.allowed);
        assert_eq!(result.remaining, 20); // remaining includes overdraft (120 - 100 = 20)

        // 消费 10 个配额(透支)
        let result = controller.consume("user1", "resource1", 10).await.unwrap();
        assert!(result.allowed);
        assert_eq!(result.remaining, 10); // 120 - 110 = 10

        // 尝试再消费 11 个配额(超过透支上限),应该被拒绝
        let result = controller.consume("user1", "resource1", 11).await.unwrap();
        assert!(!result.allowed);
    }

    /// 测试滑动窗口重置
    #[tokio::test]
    async fn test_sliding_window_reset() {
        let storage = TestQuotaStorage::new();
        let config = QuotaConfig {
            quota_type: QuotaType::Count,
            limit: 100,
            window_size: 1, // 1 秒窗口
            allow_overdraft: false,
            overdraft_limit_percent: 0,
            alert_config: AlertConfig {
                enabled: false,
                ..Default::default()
            },
        };

        let controller = QuotaController::new(storage, config);

        // 消费 50 个配额
        let result = controller.consume("user1", "resource1", 50).await.unwrap();
        assert!(result.allowed);
        assert_eq!(result.remaining, 50);

        // 等待窗口过期(超过一个完整窗口)
        tokio::time::sleep(tokio::time::Duration::from_millis(1100)).await;

        // 现在应该可以消费配额了(滑动窗口会完全重置)
        let result = controller.consume("user1", "resource1", 30).await.unwrap();
        assert!(result.allowed);
        // 窗口已经完全过期,所以应该有 100 - 30 = 70 剩余
        // 但由于滑动窗口的特性,可能会有部分保留
        // 所以我们只检查是否允许消费
        assert!(result.allowed);
    }

    /// 测试告警触发
    #[tokio::test]
    async fn test_alert_trigger() {
        let storage = TestQuotaStorage::new();
        let config = QuotaConfig {
            quota_type: QuotaType::Count,
            limit: 100,
            window_size: 3600,
            allow_overdraft: false,
            overdraft_limit_percent: 0,
            alert_config: AlertConfig {
                enabled: true,
                thresholds: vec![80, 90, 100],
                channels: vec![AlertChannel::Log],
                dedup_window: DEFAULT_DEDUP_WINDOW_SECS,
            },
        };

        let controller = QuotaController::new(storage, config);

        // 消费 80 个配额,应该触发 80% 告警
        let result = controller.consume("user1", "resource1", 80).await.unwrap();
        assert!(result.allowed);
        assert!(result.alert_triggered);

        // 消费 10 个配额,应该触发 90% 告警
        let result = controller.consume("user1", "resource1", 10).await.unwrap();
        assert!(result.allowed);
        assert!(result.alert_triggered);

        // 消费 10 个配额,应该触发 100% 告警
        let result = controller.consume("user1", "resource1", 10).await.unwrap();
        assert!(result.allowed);
        assert!(result.alert_triggered);
    }

    /// 测试告警去重
    #[tokio::test]
    async fn test_alert_dedup() {
        let storage = TestQuotaStorage::new();
        let config = QuotaConfig {
            quota_type: QuotaType::Count,
            limit: 100,
            window_size: 3600,
            allow_overdraft: false,
            overdraft_limit_percent: 0,
            alert_config: AlertConfig {
                enabled: true,
                thresholds: vec![80],
                channels: vec![AlertChannel::Log],
                dedup_window: 5, // 5 秒去重窗口
            },
        };

        let controller = QuotaController::new(storage, config);

        // 消费 80 个配额,应该触发告警
        let result = controller.consume("user1", "resource1", 80).await.unwrap();
        assert!(result.allowed);
        assert!(result.alert_triggered);

        // 立即再次消费到 90%,仍然不应该触发告警(去重)
        let result = controller.consume("user1", "resource1", 10).await.unwrap();
        assert!(result.allowed);
        assert!(!result.alert_triggered);

        // 等待去重窗口过期
        tokio::time::sleep(tokio::time::Duration::from_millis(5100)).await;

        // 清理过期的去重记录
        controller.cleanup_alert_dedup();

        // 再次消费到 95%,应该触发告警
        let result = controller.consume("user1", "resource1", 5).await.unwrap();
        assert!(result.allowed);
        assert!(result.alert_triggered);
    }

    /// 测试获取配额状态
    #[tokio::test]
    async fn test_get_quota() {
        let storage = TestQuotaStorage::new();
        let config = QuotaConfig {
            quota_type: QuotaType::Count,
            limit: 100,
            window_size: 3600,
            allow_overdraft: false,
            overdraft_limit_percent: 0,
            alert_config: AlertConfig {
                enabled: false,
                ..Default::default()
            },
        };

        let controller = QuotaController::new(storage, config);

        // 消费配额
        controller.consume("user1", "resource1", 50).await.unwrap();

        // 获取配额状态
        let state = controller.get_quota("user1", "resource1").await.unwrap();
        assert!(state.is_some());
        assert_eq!(state.unwrap().consumed, 50);
    }

    /// 测试重置配额
    #[tokio::test]
    async fn test_reset_quota() {
        let storage = TestQuotaStorage::new();
        let config = QuotaConfig {
            quota_type: QuotaType::Count,
            limit: 100,
            window_size: 3600,
            allow_overdraft: false,
            overdraft_limit_percent: 0,
            alert_config: AlertConfig {
                enabled: false,
                ..Default::default()
            },
        };

        let controller = QuotaController::new(storage, config);

        // 消费配额
        controller.consume("user1", "resource1", 50).await.unwrap();

        // 重置配额
        controller.reset_quota("user1", "resource1").await.unwrap();

        // 获取配额状态
        let state = controller.get_quota("user1", "resource1").await.unwrap();
        assert!(state.is_some());
        assert_eq!(state.unwrap().consumed, 0);
    }

    /// 测试不同配额类型
    #[test]
    fn test_quota_types() {
        let token_config = QuotaConfig {
            quota_type: QuotaType::Token,
            ..Default::default()
        };
        assert_eq!(token_config.quota_type.as_str(), "token");

        let money_config = QuotaConfig {
            quota_type: QuotaType::Money,
            ..Default::default()
        };
        assert_eq!(money_config.quota_type.as_str(), "money");

        let count_config = QuotaConfig {
            quota_type: QuotaType::Count,
            ..Default::default()
        };
        assert_eq!(count_config.quota_type.as_str(), "count");
    }

    /// 测试并发消费
    #[tokio::test]
    async fn test_concurrent_consume() {
        let storage = TestQuotaStorage::new();
        let config = QuotaConfig {
            quota_type: QuotaType::Count,
            limit: 100,
            window_size: 3600,
            allow_overdraft: false,
            overdraft_limit_percent: 0,
            alert_config: AlertConfig {
                enabled: false,
                ..Default::default()
            },
        };

        let controller = Arc::new(QuotaController::new(storage, config));
        let mut handles = vec![];

        // 创建 10 个并发任务,每个消费 10 个配额
        for _ in 0..10 {
            let controller_clone = Arc::clone(&controller);
            handles.push(tokio::spawn(async move {
                controller_clone.consume("user1", "resource1", 10).await
            }));
        }

        let mut total_consumed = 0;
        for handle in handles {
            let result = handle.await.unwrap().unwrap();
            if result.allowed {
                total_consumed += 10;
            }
        }

        // 总消费量应该不超过 100
        assert!(total_consumed <= 100);
    }

    /// 测试消费数量为 0
    #[tokio::test]
    async fn test_consume_zero() {
        let storage = TestQuotaStorage::new();
        let config = QuotaConfig::default();
        let controller = QuotaController::new(storage, config);

        let result = controller.consume("user1", "resource1", 0).await.unwrap();
        assert!(result.allowed);
        assert_eq!(result.remaining, 1000);
        assert!(!result.alert_triggered);
    }

    /// 测试更新配置
    #[test]
    fn test_update_config() {
        let storage = TestQuotaStorage::new();
        let config = QuotaConfig::default();
        let mut controller = QuotaController::new(storage, config);

        assert_eq!(controller.config().limit, 1000);

        let new_config = QuotaConfig {
            limit: 500,
            ..Default::default()
        };
        controller.update_config(new_config);

        assert_eq!(controller.config().limit, 500);
    }
}