limiteron 0.2.9

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
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
// Copyright (c) 2026 Kirky.X
// SPDX-License-Identifier: MIT
//! 决策链类型定义

use crate::error::{Decision, LimiteronError, RejectionMetadata};
use crate::limiters::Limiter;
use ahash::AHashMap;
use log::{info, warn};
use parking_lot::RwLock;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};

// ============================================================================
// 决策链节点
// ============================================================================

/// 决策链节点
///
/// 责任链中的单个节点,包含一个限流器和相关配置。
#[derive(Clone)]
pub struct DecisionNode {
    /// 节点ID
    pub id: String,
    /// 节点名称
    pub name: String,
    /// 限流器
    pub limiter: Arc<dyn Limiter>,
    /// 优先级(数值越大优先级越高)
    pub priority: u16,
    /// 是否启用
    pub enabled: bool,
    /// 是否短路(拒绝时立即返回)
    pub short_circuit: bool,
    /// 成本(每次请求消耗的令牌数)
    pub cost: u64,
}

impl DecisionNode {
    /// 使用依赖注入创建决策节点
    ///
    /// # 参数
    /// - `id`: 节点ID
    /// - `name`: 节点名称
    /// - `limiter`: 限流器(依赖注入)
    /// - `priority`: 优先级
    ///
    /// # 返回
    /// - 决策节点实例
    pub fn with_dependencies(
        id: String,
        name: String,
        limiter: Arc<dyn Limiter>,
        priority: u16,
    ) -> Self {
        Self {
            id,
            name,
            limiter,
            priority,
            enabled: true,
            short_circuit: true,
            cost: 1,
        }
    }

    /// 创建决策节点(向后兼容)
    pub fn new(id: String, name: String, limiter: Arc<dyn Limiter>, priority: u16) -> Self {
        Self::with_dependencies(id, name, limiter, priority)
    }

    /// 设置是否启用
    ///
    /// # 参数
    /// - `enabled`: 是否启用
    pub fn with_enabled(mut self, enabled: bool) -> Self {
        self.enabled = enabled;
        self
    }

    /// 设置是否短路
    ///
    /// # 参数
    /// - `short_circuit`: 是否短路
    pub fn with_short_circuit(mut self, short_circuit: bool) -> Self {
        self.short_circuit = short_circuit;
        self
    }

    /// 设置成本
    ///
    /// # 参数
    /// - `cost`: 成本
    pub fn with_cost(mut self, cost: u64) -> Self {
        self.cost = cost;
        self
    }
}

// ============================================================================
// 决策链
// ============================================================================

/// 原子统计结构体
///
/// 使用原子操作实现无锁统计更新,提供高性能的并发统计。
///
/// # 性能优势
///
/// - 无锁设计:使用原子操作避免锁竞争
/// - 顺序一致:所有原子操作使用 `Ordering::SeqCst`,保证跨计数器的全局
///   可见性顺序。`DecisionChain::check` 的调用模式是先 `increment_total`
///   后 `increment_allowed/rejected/error`,SeqCst 确保 snapshot 不会
///   观测到 "total 已 increment 而分类计数尚未 increment" 的中间态,
///   从而维护 `allowed + rejected + error >= total` 的业务不变式。
///   相比 `Relaxed` 的小幅性能损失换来更强的跨计数器一致性保证。
/// - 高并发:支持多线程同时更新统计信息
///
/// # 示例
///
/// ```rust
/// use limiteron::decision_chain::types::AtomicChainStats;
///
/// let stats = AtomicChainStats::new();
/// stats.increment_total();
/// stats.increment_allowed();
///
/// let snapshot = stats.snapshot();
/// assert_eq!(snapshot.total_checks, 1);
/// assert_eq!(snapshot.allowed_count, 1);
/// ```
pub struct AtomicChainStats {
    /// 总检查次数
    total_checks: AtomicU64,
    /// 允许次数
    allowed_count: AtomicU64,
    /// 拒绝次数
    rejected_count: AtomicU64,
    /// 错误次数
    error_count: AtomicU64,
    /// 各节点的拒绝次数(需要锁保护的动态数据)
    node_rejections: RwLock<AHashMap<String, u64>>,
}

impl AtomicChainStats {
    /// 创建新的原子统计实例
    ///
    /// # 返回
    /// - 初始化为零的原子统计实例
    pub fn new() -> Self {
        Self {
            total_checks: AtomicU64::new(0),
            allowed_count: AtomicU64::new(0),
            rejected_count: AtomicU64::new(0),
            error_count: AtomicU64::new(0),
            node_rejections: RwLock::new(AHashMap::new()),
        }
    }

    /// 增加总检查次数
    #[inline]
    pub fn increment_total(&self) {
        self.total_checks.fetch_add(1, Ordering::SeqCst);
    }

    /// 增加允许次数
    #[inline]
    pub fn increment_allowed(&self) {
        self.allowed_count.fetch_add(1, Ordering::SeqCst);
    }

    /// 增加拒绝次数
    #[inline]
    pub fn increment_rejected(&self) {
        self.rejected_count.fetch_add(1, Ordering::SeqCst);
    }

    /// 增加错误次数
    #[inline]
    pub fn increment_error(&self) {
        self.error_count.fetch_add(1, Ordering::SeqCst);
    }

    /// 增加指定节点的拒绝次数
    ///
    /// # 参数
    /// - `node_id`: 节点ID
    pub fn increment_node_rejection(&self, node_id: &str) {
        let mut rejections = self.node_rejections.write();
        *rejections.entry(node_id.to_string()).or_insert(0) += 1;
    }

    /// 获取统计快照
    ///
    /// 返回当前统计信息的快照,用于读取和展示。
    ///
    /// # 返回
    /// - 统计快照(值类型)
    pub fn snapshot(&self) -> ChainStats {
        let node_rejections: Vec<(String, u64)> = self
            .node_rejections
            .read()
            .iter()
            .map(|(k, v)| (k.clone(), *v))
            .collect();

        ChainStats {
            total_checks: self.total_checks.load(Ordering::SeqCst),
            allowed_count: self.allowed_count.load(Ordering::SeqCst),
            rejected_count: self.rejected_count.load(Ordering::SeqCst),
            error_count: self.error_count.load(Ordering::SeqCst),
            node_rejections,
        }
    }

    /// 重置所有统计信息
    pub fn reset(&self) {
        self.total_checks.store(0, Ordering::SeqCst);
        self.allowed_count.store(0, Ordering::SeqCst);
        self.rejected_count.store(0, Ordering::SeqCst);
        self.error_count.store(0, Ordering::SeqCst);
        self.node_rejections.write().clear();
    }
}

impl Default for AtomicChainStats {
    fn default() -> Self {
        Self::new()
    }
}

/// 决策链
///
/// 使用责任链模式实现多限流器组合决策。
#[derive(Clone)]
pub struct DecisionChain {
    /// 链中的节点(按优先级排序)
    nodes: Vec<DecisionNode>,
    /// 统计信息(原子类型)
    stats: Arc<AtomicChainStats>,
}

/// 决策链统计信息
///
/// 统计快照,用于读取和展示统计信息。
#[derive(Debug, Clone, Default)]
pub struct ChainStats {
    /// 总检查次数
    pub total_checks: u64,
    /// 允许次数
    pub allowed_count: u64,
    /// 拒绝次数
    pub rejected_count: u64,
    /// 错误次数
    pub error_count: u64,
    /// 各节点的拒绝次数
    pub node_rejections: Vec<(String, u64)>,
}

impl DecisionChain {
    /// 使用构建器创建决策链
    ///
    /// # 返回
    /// - 决策链构建器
    pub fn builder() -> DecisionChainBuilder {
        DecisionChainBuilder::new()
    }

    /// 使用依赖注入创建决策链
    pub fn with_dependencies(nodes: Vec<DecisionNode>) -> Self {
        let mut chain = Self {
            nodes: Vec::new(),
            stats: Arc::new(AtomicChainStats::new()),
        };

        for node in nodes {
            chain.add_node(node);
        }

        chain
    }

    /// 检查决策链(向后兼容)
    pub async fn check(&self) -> Result<Decision, LimiteronError> {
        let enabled_nodes: Vec<&DecisionNode> = self.nodes.iter().filter(|n| n.enabled).collect();

        let mut last_rejection: Option<RejectionMetadata> = None;

        for node in enabled_nodes {
            match node.limiter.allow(node.cost).await {
                Ok(false) => {
                    // 节点拒绝 - 使用原子操作更新统计
                    self.stats.increment_total();
                    self.stats.increment_rejected();
                    self.stats.increment_node_rejection(&node.id);

                    // 检查短路标志
                    if node.short_circuit {
                        // 短路:立即返回拒绝,不执行后续节点
                        return Ok(Decision::Rejected(RejectionMetadata {
                            reason: format!("Rejected by {}: rate limit exceeded", node.name),
                            retry_after: 60,
                            limit: 0,
                            reset_at: std::time::SystemTime::now()
                                .duration_since(std::time::UNIX_EPOCH)
                                .map(|d| d.as_secs())
                                .unwrap_or(0)
                                + 60,
                        }));
                    }
                    // 非短路:记录拒绝但继续执行后续节点
                    last_rejection = Some(RejectionMetadata {
                        reason: format!("Rejected by {}: rate limit exceeded", node.name),
                        retry_after: 60,
                        limit: 0,
                        reset_at: std::time::SystemTime::now()
                            .duration_since(std::time::UNIX_EPOCH)
                            .map(|d| d.as_secs())
                            .unwrap_or(0)
                            + 60,
                    });
                }
                Err(e) => {
                    // 节点错误 - 使用原子操作更新统计
                    self.stats.increment_total();
                    self.stats.increment_error();
                    return Err(e);
                }
                _ => {} // 允许,继续下一个节点
            }
        }

        // 所有节点都允许 - 使用原子操作更新统计
        self.stats.increment_total();

        // 如果有任何非短路拒绝,返回最后的拒绝结果
        if let Some(rejection) = last_rejection {
            self.stats.increment_allowed();
            return Ok(Decision::Rejected(rejection));
        }

        self.stats.increment_allowed();
        Ok(Decision::allowed_default())
    }

    /// 获取统计信息
    ///
    /// 返回当前统计信息的快照。
    ///
    /// # 返回
    /// - 统计快照(值类型)
    pub async fn stats(&self) -> ChainStats {
        self.stats.snapshot()
    }

    /// 获取统计信息(同步版本)
    ///
    /// 由于使用原子操作,可以同步获取统计信息。
    ///
    /// # 返回
    /// - 统计快照(值类型)
    pub fn stats_sync(&self) -> ChainStats {
        self.stats.snapshot()
    }

    /// 重置统计信息
    pub async fn reset_stats(&self) {
        self.stats.reset();
    }

    /// 重置统计信息(同步版本)
    ///
    /// 由于使用原子操作,可以同步重置统计信息。
    pub fn reset_stats_sync(&self) {
        self.stats.reset();
    }

    /// 获取节点数量
    pub fn node_count(&self) -> usize {
        self.nodes.len()
    }

    /// 获取启用的节点数量
    pub fn enabled_node_count(&self) -> usize {
        self.nodes.iter().filter(|n| n.enabled).count()
    }

    /// 添加节点
    pub fn add_node(&mut self, node: DecisionNode) {
        self.nodes.push(node);
    }

    /// 启用节点
    ///
    /// # 参数
    /// - `node_id`: 节点ID
    pub fn enable_node(&mut self, node_id: &str) -> bool {
        if let Some(node) = self.nodes.iter_mut().find(|n| n.id == node_id) {
            node.enabled = true;
            info!("Enabled node: {}", node_id);
            true
        } else {
            warn!("Failed to enable node: {} (not found)", node_id);
            false
        }
    }

    /// 禁用节点
    ///
    /// # 参数
    /// - `node_id`: 节点ID
    pub fn disable_node(&mut self, node_id: &str) -> bool {
        if let Some(node) = self.nodes.iter_mut().find(|n| n.id == node_id) {
            node.enabled = false;
            info!("Disabled node: {}", node_id);
            true
        } else {
            warn!("Failed to disable node: {} (not found)", node_id);
            false
        }
    }

    /// 设置节点短路
    ///
    /// # 参数
    /// - `node_id`: 节点ID
    /// - `short_circuit`: 是否短路
    pub fn set_short_circuit(&mut self, node_id: &str, short_circuit: bool) -> bool {
        if let Some(node) = self.nodes.iter_mut().find(|n| n.id == node_id) {
            node.short_circuit = short_circuit;
            info!("Set short_circuit={} for node: {}", short_circuit, node_id);
            true
        } else {
            warn!(
                "Failed to set short_circuit for node: {} (not found)",
                node_id
            );
            false
        }
    }
}

// ============================================================================
// 构建器
// ============================================================================

/// 决策链构建器
///
/// 提供流式API构建决策链。
pub struct DecisionChainBuilder {
    nodes: Vec<DecisionNode>,
}

impl DecisionChainBuilder {
    /// 创建新的构建器
    ///
    /// # 示例
    /// ```rust
    /// use limiteron::decision_chain::DecisionChainBuilder;
    ///
    /// let builder = DecisionChainBuilder::new();
    /// ```
    pub fn new() -> Self {
        Self { nodes: Vec::new() }
    }

    /// 添加节点
    ///
    /// # 参数
    /// - `node`: 决策节点
    pub fn add_node(mut self, node: DecisionNode) -> Self {
        self.nodes.push(node);
        self
    }

    /// 构建决策链
    ///
    /// # 返回
    /// - 决策链实例
    pub fn build(self) -> DecisionChain {
        DecisionChain::with_dependencies(self.nodes)
    }
}

impl Default for DecisionChainBuilder {
    fn default() -> Self {
        Self::new()
    }
}

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

#[cfg(test)]
#[allow(deprecated)]
mod tests {
    use super::*;
    use crate::limiters::{
        ConcurrencyLimiter, FixedWindowLimiter, ShardedSlidingWindowLimiter, TokenBucketLimiter,
    };
    use async_trait::async_trait;
    use std::time::Duration;

    // Helper structs for testing
    struct MockLimiter {
        allowed: Arc<std::sync::atomic::AtomicBool>,
    }
    impl MockLimiter {
        fn new(allowed: bool) -> Self {
            Self {
                allowed: Arc::new(std::sync::atomic::AtomicBool::new(allowed)),
            }
        }
        fn set_allowed(&self, v: bool) {
            self.allowed.store(v, std::sync::atomic::Ordering::SeqCst);
        }
    }
    #[async_trait]
    impl Limiter for MockLimiter {
        async fn allow(&self, _cost: u64) -> Result<bool, LimiteronError> {
            let a = self.allowed.load(std::sync::atomic::Ordering::SeqCst);
            Ok(a)
        }
    }

    struct SpyLimiter {
        calls: Arc<std::sync::atomic::AtomicUsize>,
    }
    impl SpyLimiter {
        fn new() -> Self {
            Self {
                calls: Arc::new(std::sync::atomic::AtomicUsize::new(0)),
            }
        }
    }
    #[async_trait]
    impl Limiter for SpyLimiter {
        async fn allow(&self, _cost: u64) -> Result<bool, LimiteronError> {
            self.calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            Ok(true)
        }
    }

    // ==================== DecisionNode 测试 ====================

    #[test]
    fn test_decision_node_creation() {
        let limiter = Arc::new(TokenBucketLimiter::new(100, 10));
        let node = DecisionNode::with_dependencies(
            "node1".to_string(),
            "Token Bucket".to_string(),
            limiter,
            100,
        );

        assert_eq!(node.id, "node1");
        assert_eq!(node.name, "Token Bucket");
        assert_eq!(node.priority, 100);
        assert!(node.enabled);
        assert!(node.short_circuit);
        assert_eq!(node.cost, 1);
    }

    #[test]
    fn test_decision_node_with_options() {
        let limiter = Arc::new(TokenBucketLimiter::new(100, 10));
        let node = DecisionNode::with_dependencies(
            "node1".to_string(),
            "Token Bucket".to_string(),
            limiter,
            100,
        )
        .with_enabled(false)
        .with_short_circuit(false)
        .with_cost(5);

        assert!(!node.enabled);
        assert!(!node.short_circuit);
        assert_eq!(node.cost, 5);
    }

    // ==================== DecisionChain 测试 ====================

    #[tokio::test]
    async fn test_decision_chain_empty() {
        let chain = DecisionChain::with_dependencies(vec![]);
        let decision = chain.check().await.unwrap();

        assert_eq!(decision, Decision::allowed_default());
    }

    #[tokio::test]
    async fn test_decision_chain_single_node() {
        let limiter = Arc::new(TokenBucketLimiter::new(10, 1));
        let node = DecisionNode::with_dependencies(
            "node1".to_string(),
            "Token Bucket".to_string(),
            limiter,
            100,
        );

        let chain = DecisionChain::with_dependencies(vec![node]);

        // 前10个请求应该被允许
        for _ in 0..10 {
            let decision = chain.check().await.unwrap();
            assert_eq!(decision, Decision::allowed_default());
        }

        // 第11个请求应该被拒绝
        let decision = chain.check().await.unwrap();
        assert!(matches!(decision, Decision::Rejected(_)));
    }

    #[tokio::test]
    async fn test_decision_chain_multiple_nodes() {
        let limiter1 = Arc::new(TokenBucketLimiter::new(5, 1));
        let limiter2 = Arc::new(ShardedSlidingWindowLimiter::new(Duration::from_secs(1), 10));

        let node1 = DecisionNode::with_dependencies(
            "node1".to_string(),
            "Token Bucket".to_string(),
            limiter1,
            100,
        );

        let node2 = DecisionNode::with_dependencies(
            "node2".to_string(),
            "Sliding Window".to_string(),
            limiter2,
            50,
        );

        let chain = DecisionChain::with_dependencies(vec![node1, node2]);

        // 前5个请求应该被允许
        for _ in 0..5 {
            let decision = chain.check().await.unwrap();
            assert_eq!(decision, Decision::allowed_default());
        }

        // 第6个请求应该被更高优先级的node1拒绝
        let decision = chain.check().await.unwrap();
        assert!(matches!(decision, Decision::Rejected(_)));
    }

    #[tokio::test]
    async fn test_decision_chain_priority() {
        let limiter1 = Arc::new(TokenBucketLimiter::new(10, 1));
        let limiter2 = Arc::new(TokenBucketLimiter::new(5, 1));

        let node1 = DecisionNode::with_dependencies(
            "node1".to_string(),
            "Low Priority".to_string(),
            limiter1,
            50,
        );

        let node2 = DecisionNode::with_dependencies(
            "node2".to_string(),
            "High Priority".to_string(),
            limiter2,
            100,
        );

        let chain = DecisionChain::with_dependencies(vec![node1, node2]);

        // 高优先级的node2应该先被检查
        for _ in 0..5 {
            let decision = chain.check().await.unwrap();
            assert_eq!(decision, Decision::allowed_default());
        }

        // node2应该先拒绝
        let decision = chain.check().await.unwrap();
        assert!(matches!(decision, Decision::Rejected(_)));

        // 验证拒绝原因来自node2
        if let Decision::Rejected(metadata) = decision {
            assert!(metadata.reason.contains("High Priority"));
        }
    }

    #[tokio::test]
    async fn test_decision_chain_disabled_node() {
        let limiter1 = Arc::new(TokenBucketLimiter::new(0, 1));
        let limiter2 = Arc::new(TokenBucketLimiter::new(10, 1));

        let node1 = DecisionNode::with_dependencies(
            "node1".to_string(),
            "Disabled Node".to_string(),
            limiter1,
            100,
        )
        .with_enabled(false);

        let node2 = DecisionNode::with_dependencies(
            "node2".to_string(),
            "Active Node".to_string(),
            limiter2,
            50,
        );

        let chain = DecisionChain::with_dependencies(vec![node1, node2]);

        // node1被禁用,应该检查node2
        let decision = chain.check().await.unwrap();
        assert_eq!(decision, Decision::allowed_default());
    }

    #[tokio::test]
    async fn test_decision_chain_short_circuit() {
        let limiter1 = Arc::new(TokenBucketLimiter::new(5, 1));
        let limiter2 = Arc::new(TokenBucketLimiter::new(10, 1));

        let node1 = DecisionNode::with_dependencies(
            "node1".to_string(),
            "First Node".to_string(),
            limiter1,
            100,
        );

        let node2 = DecisionNode::with_dependencies(
            "node2".to_string(),
            "Second Node".to_string(),
            limiter2,
            50,
        );

        let chain = DecisionChain::with_dependencies(vec![node1, node2]);

        // 前5个请求应该被允许
        for _ in 0..5 {
            let decision = chain.check().await.unwrap();
            assert_eq!(decision, Decision::allowed_default());
        }

        // 第6个请求应该被node1拒绝,并短路
        let decision = chain.check().await.unwrap();
        assert!(matches!(decision, Decision::Rejected(_)));
    }

    #[tokio::test]
    async fn test_decision_chain_no_short_circuit() {
        let limiter1 = Arc::new(TokenBucketLimiter::new(5, 1));
        let limiter2 = Arc::new(TokenBucketLimiter::new(3, 1));

        let node1 = DecisionNode::with_dependencies(
            "node1".to_string(),
            "First Node".to_string(),
            limiter1,
            100,
        )
        .with_short_circuit(false);

        let node2 = DecisionNode::with_dependencies(
            "node2".to_string(),
            "Second Node".to_string(),
            limiter2,
            50,
        );

        let chain = DecisionChain::with_dependencies(vec![node1, node2]);

        // 前3个请求应该被允许
        for _ in 0..3 {
            let decision = chain.check().await.unwrap();
            assert_eq!(decision, Decision::allowed_default());
        }

        // 第4个请求应该被node2拒绝
        let decision = chain.check().await.unwrap();
        assert!(matches!(decision, Decision::Rejected(_)));
    }

    #[tokio::test]
    async fn test_decision_chain_check() {
        let limiter1 = Arc::new(TokenBucketLimiter::new(3, 1));
        let limiter2 = Arc::new(TokenBucketLimiter::new(5, 1));

        let node1 = DecisionNode::with_dependencies(
            "node1".to_string(),
            "First Node".to_string(),
            limiter1,
            100,
        );

        let node2 = DecisionNode::with_dependencies(
            "node2".to_string(),
            "Second Node".to_string(),
            limiter2,
            50,
        );

        let chain = DecisionChain::with_dependencies(vec![node1, node2]);

        // 前3个请求应该被允许
        for _ in 0..3 {
            let decision = chain.check().await.unwrap();
            assert_eq!(decision, Decision::allowed_default());
        }

        // 第4个请求应该检查所有节点
        let decision = chain.check().await.unwrap();
        if let Decision::Rejected(metadata) = decision {
            // 应该包含两个节点的拒绝原因
            assert!(metadata.reason.contains("First Node"));
        }
    }

    #[tokio::test]
    async fn test_decision_chain_stats() {
        let limiter = Arc::new(TokenBucketLimiter::new(5, 1));
        let node = DecisionNode::with_dependencies(
            "node1".to_string(),
            "Token Bucket".to_string(),
            limiter,
            100,
        );

        let chain = DecisionChain::with_dependencies(vec![node]);

        // 发送10个请求
        for _ in 0..10 {
            chain.check().await.unwrap();
        }

        let stats = chain.stats().await;
        assert_eq!(stats.total_checks, 10);
        assert_eq!(stats.allowed_count, 5);
        assert_eq!(stats.rejected_count, 5);
    }

    #[tokio::test]
    async fn test_decision_chain_node_rejections() {
        let limiter1 = Arc::new(TokenBucketLimiter::new(5, 1));
        let limiter2 = Arc::new(TokenBucketLimiter::new(3, 1));

        let node1 = DecisionNode::with_dependencies(
            "node1".to_string(),
            "First Node".to_string(),
            limiter1,
            100,
        )
        .with_short_circuit(false);

        let node2 = DecisionNode::with_dependencies(
            "node2".to_string(),
            "Second Node".to_string(),
            limiter2,
            50,
        );

        let chain = DecisionChain::with_dependencies(vec![node1, node2]);

        // 发送10个请求
        for _ in 0..10 {
            chain.check().await.unwrap();
        }

        let stats = chain.stats().await;

        // 应该有两个节点的拒绝记录
        assert_eq!(stats.node_rejections.len(), 2);

        // 验证拒绝次数
        let node1_rejections = stats
            .node_rejections
            .iter()
            .find(|(id, _)| id == "node1")
            .map(|(_, count)| *count)
            .unwrap_or(0);
        let node2_rejections = stats
            .node_rejections
            .iter()
            .find(|(id, _)| id == "node2")
            .map(|(_, count)| *count)
            .unwrap_or(0);

        assert!(node1_rejections > 0);
        assert!(node2_rejections > 0);
    }

    #[tokio::test]
    async fn test_decision_chain_add_node() {
        let limiter = Arc::new(TokenBucketLimiter::new(10, 1));
        let node = DecisionNode::with_dependencies(
            "node1".to_string(),
            "Token Bucket".to_string(),
            limiter,
            100,
        );

        let mut chain = DecisionChain::with_dependencies(vec![]);
        assert_eq!(chain.node_count(), 0);

        chain.add_node(node);
        assert_eq!(chain.node_count(), 1);
    }

    #[tokio::test]
    async fn test_decision_chain_enable_disable_node() {
        let limiter = Arc::new(TokenBucketLimiter::new(0, 1));
        let node = DecisionNode::with_dependencies(
            "node1".to_string(),
            "Token Bucket".to_string(),
            limiter,
            100,
        );

        let mut chain = DecisionChain::with_dependencies(vec![node]);

        // 禁用节点
        chain.disable_node("node1");
        assert_eq!(chain.enabled_node_count(), 0);

        let decision = chain.check().await.unwrap();
        assert_eq!(decision, Decision::allowed_default());

        // 启用节点
        chain.enable_node("node1");
        assert_eq!(chain.enabled_node_count(), 1);

        let decision = chain.check().await.unwrap();
        assert!(matches!(decision, Decision::Rejected(_)));
    }

    #[tokio::test]
    async fn test_decision_chain_set_short_circuit() {
        let limiter1 = Arc::new(MockLimiter::new(true));
        let limiter2_spy = Arc::new(SpyLimiter::new());
        let limiter2 = limiter2_spy.clone();

        // Node1: Mock limiter, initially allowed. Short circuit OFF.
        let node1 = DecisionNode::with_dependencies(
            "node1".to_string(),
            "Mock Node".to_string(),
            limiter1.clone(),
            100,
        )
        .with_short_circuit(false);

        // Node2: Spy limiter, always allowed.
        let node2 = DecisionNode::with_dependencies(
            "node2".to_string(),
            "Spy Node".to_string(),
            limiter2,
            50,
        );

        let mut chain = DecisionChain::with_dependencies(vec![node1, node2]);

        // 1. Initial check: Node1 allows. Node2 should be called.
        let decision = chain.check().await.unwrap();
        assert_eq!(decision, Decision::allowed_default());
        assert_eq!(
            limiter2_spy.calls.load(std::sync::atomic::Ordering::SeqCst),
            1
        );

        // 2. Node1 rejects. Short circuit OFF. Node2 should be called.
        limiter1.set_allowed(false);
        let decision = chain.check().await.unwrap();
        assert!(matches!(decision, Decision::Rejected(_)));
        assert_eq!(
            limiter2_spy.calls.load(std::sync::atomic::Ordering::SeqCst),
            2
        ); // Increased

        // 3. Enable short circuit on Node1.
        chain.set_short_circuit("node1", true);

        // 4. Node1 rejects. Short circuit ON. Node2 should NOT be called.
        let decision = chain.check().await.unwrap();
        assert!(matches!(decision, Decision::Rejected(_)));
        assert_eq!(
            limiter2_spy.calls.load(std::sync::atomic::Ordering::SeqCst),
            2
        ); // Unchanged!

        // 5. Node1 allows again. Node2 should be called.
        limiter1.set_allowed(true);
        let decision = chain.check().await.unwrap();
        assert_eq!(decision, Decision::allowed_default());
        assert_eq!(
            limiter2_spy.calls.load(std::sync::atomic::Ordering::SeqCst),
            3
        ); // Increased
    }

    // ==================== DecisionChainBuilder 测试 ====================

    #[test]
    fn test_decision_chain_builder() {
        let limiter1 = Arc::new(TokenBucketLimiter::new(100, 10));
        let limiter2 = Arc::new(ShardedSlidingWindowLimiter::new(
            Duration::from_secs(1),
            100,
        ));

        let node1 = DecisionNode::with_dependencies(
            "node1".to_string(),
            "Token Bucket".to_string(),
            limiter1,
            100,
        );

        let node2 = DecisionNode::with_dependencies(
            "node2".to_string(),
            "Sliding Window".to_string(),
            limiter2,
            50,
        );

        let chain = DecisionChainBuilder::new()
            .add_node(node1)
            .add_node(node2)
            .build();

        assert_eq!(chain.node_count(), 2);
    }

    #[tokio::test]
    async fn test_decision_chain_mixed_limiters() {
        let limiter1 = Arc::new(TokenBucketLimiter::new(10, 1));
        let limiter2 = Arc::new(ShardedSlidingWindowLimiter::new(Duration::from_secs(1), 5));
        let limiter3 = Arc::new(FixedWindowLimiter::new(Duration::from_secs(1), 3));
        let limiter4 = Arc::new(ConcurrencyLimiter::new(2));

        let node1 = DecisionNode::with_dependencies(
            "token_bucket".to_string(),
            "Token Bucket".to_string(),
            limiter1,
            100,
        );

        let node2 = DecisionNode::with_dependencies(
            "sliding_window".to_string(),
            "Sliding Window".to_string(),
            limiter2,
            75,
        );

        let node3 = DecisionNode::with_dependencies(
            "fixed_window".to_string(),
            "Fixed Window".to_string(),
            limiter3,
            50,
        );

        let node4 = DecisionNode::with_dependencies(
            "concurrency".to_string(),
            "Concurrency".to_string(),
            limiter4,
            25,
        );

        let chain = DecisionChain::with_dependencies(vec![node1, node2, node3, node4]);

        // 第一个请求应该被允许
        let decision = chain.check().await.unwrap();
        assert_eq!(decision, Decision::allowed_default());

        // 检查统计
        let stats = chain.stats().await;
        assert_eq!(stats.total_checks, 1);
        assert_eq!(stats.allowed_count, 1);
    }

    #[tokio::test]
    async fn test_decision_chain_cost() {
        let limiter = Arc::new(TokenBucketLimiter::new(10, 1));
        let node = DecisionNode::with_dependencies(
            "node1".to_string(),
            "Token Bucket".to_string(),
            limiter,
            100,
        )
        .with_cost(2);

        let chain = DecisionChain::with_dependencies(vec![node]);

        // 5个请求,每个消耗2个令牌
        for _ in 0..5 {
            let decision = chain.check().await.unwrap();
            assert_eq!(decision, Decision::allowed_default());
        }

        // 第6个请求应该被拒绝(总共消耗了10个令牌)
        let decision = chain.check().await.unwrap();
        assert!(matches!(decision, Decision::Rejected(_)));
    }

    #[tokio::test]
    async fn test_decision_chain_reset_stats() {
        let limiter = Arc::new(TokenBucketLimiter::new(10, 1));
        let node = DecisionNode::with_dependencies(
            "node1".to_string(),
            "Token Bucket".to_string(),
            limiter,
            100,
        );

        let chain = DecisionChain::with_dependencies(vec![node]);

        // 发送一些请求
        for _ in 0..5 {
            chain.check().await.unwrap();
        }

        // 检查统计
        let stats = chain.stats().await;
        assert_eq!(stats.total_checks, 5);

        // 重置统计
        chain.reset_stats().await;

        // 检查重置后的统计
        let stats = chain.stats().await;
        assert_eq!(stats.total_checks, 0);
    }

    #[tokio::test]
    async fn test_decision_chain_concurrent_checks() {
        let limiter = Arc::new(TokenBucketLimiter::new(100, 10));
        let node = DecisionNode::with_dependencies(
            "node1".to_string(),
            "Token Bucket".to_string(),
            limiter,
            100,
        );

        let chain = Arc::new(DecisionChain::with_dependencies(vec![node]));
        let mut handles = vec![];

        // 并发检查
        for _ in 0..10 {
            let chain_clone = Arc::clone(&chain);
            handles.push(tokio::spawn(
                async move { chain_clone.check().await.unwrap() },
            ));
        }

        // 等待所有检查完成
        let mut results = Vec::new();
        for handle in handles {
            results.push(handle.await.unwrap());
        }

        // 所有检查都应该成功
        for result in results {
            assert_eq!(result, Decision::allowed_default());
        }

        // 检查统计
        let stats = chain.stats().await;
        assert_eq!(stats.total_checks, 10);
    }

    // ==================== AtomicChainStats 测试 ====================

    #[test]
    fn test_atomic_chain_stats_new() {
        let stats = AtomicChainStats::new();
        let snapshot = stats.snapshot();

        assert_eq!(snapshot.total_checks, 0);
        assert_eq!(snapshot.allowed_count, 0);
        assert_eq!(snapshot.rejected_count, 0);
        assert_eq!(snapshot.error_count, 0);
        assert!(snapshot.node_rejections.is_empty());
    }

    #[test]
    fn test_atomic_chain_stats_default() {
        let stats = AtomicChainStats::default();
        let snapshot = stats.snapshot();

        assert_eq!(snapshot.total_checks, 0);
    }

    #[test]
    fn test_atomic_chain_stats_increment_operations() {
        let stats = AtomicChainStats::new();

        // 测试各种增量操作
        stats.increment_total();
        stats.increment_total();
        stats.increment_total();

        stats.increment_allowed();
        stats.increment_allowed();

        stats.increment_rejected();

        stats.increment_error();
        stats.increment_error();
        stats.increment_error();

        let snapshot = stats.snapshot();
        assert_eq!(snapshot.total_checks, 3);
        assert_eq!(snapshot.allowed_count, 2);
        assert_eq!(snapshot.rejected_count, 1);
        assert_eq!(snapshot.error_count, 3);
    }

    #[test]
    fn test_atomic_chain_stats_node_rejections() {
        let stats = AtomicChainStats::new();

        // 测试节点拒绝计数
        stats.increment_node_rejection("node1");
        stats.increment_node_rejection("node1");
        stats.increment_node_rejection("node1");
        stats.increment_node_rejection("node2");

        let snapshot = stats.snapshot();
        assert_eq!(snapshot.node_rejections.len(), 2);

        let node1_count = snapshot
            .node_rejections
            .iter()
            .find(|(id, _)| id == "node1")
            .map(|(_, count)| *count)
            .unwrap_or(0);
        let node2_count = snapshot
            .node_rejections
            .iter()
            .find(|(id, _)| id == "node2")
            .map(|(_, count)| *count)
            .unwrap_or(0);

        assert_eq!(node1_count, 3);
        assert_eq!(node2_count, 1);
    }

    #[test]
    fn test_atomic_chain_stats_reset() {
        let stats = AtomicChainStats::new();

        // 添加一些数据
        stats.increment_total();
        stats.increment_allowed();
        stats.increment_node_rejection("node1");

        // 重置
        stats.reset();

        let snapshot = stats.snapshot();
        assert_eq!(snapshot.total_checks, 0);
        assert_eq!(snapshot.allowed_count, 0);
        assert!(snapshot.node_rejections.is_empty());
    }

    #[test]
    fn test_atomic_chain_stats_concurrent_increment() {
        use std::sync::atomic::AtomicUsize;
        use std::thread;

        let stats = Arc::new(AtomicChainStats::new());
        let counter = Arc::new(AtomicUsize::new(0));
        let mut handles = vec![];

        // 并发增加计数
        for _ in 0..100 {
            let stats_clone = Arc::clone(&stats);
            let counter_clone = Arc::clone(&counter);
            handles.push(thread::spawn(move || {
                stats_clone.increment_total();
                stats_clone.increment_allowed();
                counter_clone.fetch_add(1, Ordering::Relaxed);
            }));
        }

        // 等待所有线程完成
        for handle in handles {
            handle.join().unwrap();
        }

        // 验证计数正确
        let snapshot = stats.snapshot();
        assert_eq!(snapshot.total_checks, 100);
        assert_eq!(snapshot.allowed_count, 100);
    }

    #[test]
    fn test_atomic_chain_stats_concurrent_node_rejections() {
        use std::thread;

        let stats = Arc::new(AtomicChainStats::new());
        let mut handles = vec![];

        // 并发增加节点拒绝计数
        for i in 0..50 {
            let stats_clone = Arc::clone(&stats);
            handles.push(thread::spawn(move || {
                stats_clone.increment_node_rejection("node1");
                stats_clone.increment_node_rejection(&format!("node{}", i % 5));
            }));
        }

        // 等待所有线程完成
        for handle in handles {
            handle.join().unwrap();
        }

        // 验证计数正确
        let snapshot = stats.snapshot();

        // node1 应该有 50 + 10 = 60 次拒绝(每个线程增加一次,加上 i % 5 == 1 的情况)
        let node1_count = snapshot
            .node_rejections
            .iter()
            .find(|(id, _)| id == "node1")
            .map(|(_, count)| *count)
            .unwrap_or(0);

        // node1 被每个线程调用一次(50次),加上 i % 5 == 1 的情况(10次)
        assert_eq!(node1_count, 60);

        // 总共有 5 个不同的节点(node0 到 node4)
        assert_eq!(snapshot.node_rejections.len(), 5);
    }

    #[tokio::test]
    async fn test_decision_chain_stats_sync() {
        let limiter = Arc::new(TokenBucketLimiter::new(5, 1));
        let node = DecisionNode::with_dependencies(
            "node1".to_string(),
            "Token Bucket".to_string(),
            limiter,
            100,
        );

        let chain = DecisionChain::with_dependencies(vec![node]);

        // 发送10个请求
        for _ in 0..10 {
            chain.check().await.unwrap();
        }

        // 使用同步方法获取统计
        let stats = chain.stats_sync();
        assert_eq!(stats.total_checks, 10);
        assert_eq!(stats.allowed_count, 5);
        assert_eq!(stats.rejected_count, 5);
    }

    #[test]
    fn test_decision_chain_reset_stats_sync() {
        let limiter = Arc::new(TokenBucketLimiter::new(10, 1));
        let node = DecisionNode::with_dependencies(
            "node1".to_string(),
            "Token Bucket".to_string(),
            limiter,
            100,
        );

        let chain = DecisionChain::with_dependencies(vec![node]);

        // 使用同步重置
        chain.reset_stats_sync();

        let stats = chain.stats_sync();
        assert_eq!(stats.total_checks, 0);
    }

    #[test]
    fn test_atomic_chain_stats_high_concurrency() {
        use std::thread;

        let stats = Arc::new(AtomicChainStats::new());
        let mut handles = vec![];

        // 高并发测试:1000个线程
        for _ in 0..1000 {
            let stats_clone = Arc::clone(&stats);
            handles.push(thread::spawn(move || {
                // 每个线程执行多次操作
                for _ in 0..100 {
                    stats_clone.increment_total();
                    stats_clone.increment_allowed();
                }
            }));
        }

        // 等待所有线程完成
        for handle in handles {
            handle.join().unwrap();
        }

        // 验证计数正确
        let snapshot = stats.snapshot();
        assert_eq!(snapshot.total_checks, 100_000);
        assert_eq!(snapshot.allowed_count, 100_000);
    }

    // ==================== 并发安全测试 ====================

    /// 测试 DecisionChain 高并发检查的线程安全
    ///
    /// 验证在高并发情况下,决策链不会出现数据竞争或死锁
    #[tokio::test]
    async fn test_decision_chain_high_concurrency_safety() {
        let limiter = Arc::new(TokenBucketLimiter::new(10000, 1000));
        let node = DecisionNode::with_dependencies(
            "concurrent_node".to_string(),
            "Concurrent Test".to_string(),
            limiter,
            100,
        );

        let chain = Arc::new(DecisionChain::with_dependencies(vec![node]));
        let mut handles = vec![];

        // 使用 barrier 确保所有任务同时开始
        let barrier = Arc::new(tokio::sync::Barrier::new(100));
        let start_signal = Arc::new(std::sync::atomic::AtomicBool::new(false));

        for _ in 0..100 {
            let chain_clone = Arc::clone(&chain);
            let barrier_clone = Arc::clone(&barrier);
            let start_signal_clone = Arc::clone(&start_signal);
            handles.push(tokio::spawn(async move {
                // 等待所有任务准备就绪
                barrier_clone.wait().await;

                // 等待开始信号
                while !start_signal_clone.load(std::sync::atomic::Ordering::SeqCst) {
                    std::hint::spin_loop();
                }

                // 每个任务发送 100 个请求
                let mut local_allowed = 0;
                for _ in 0..100 {
                    if let Ok(Decision::Allowed(_)) = chain_clone.check().await {
                        local_allowed += 1;
                    }
                }
                local_allowed
            }));
        }

        // 设置开始信号
        start_signal.store(true, std::sync::atomic::Ordering::SeqCst);

        let mut total_allowed = 0;
        for handle in handles {
            total_allowed += handle.await.unwrap();
        }

        // 不应该超过令牌桶限制(允许 5% 的误差)
        assert!(
            total_allowed <= 10500,
            "Total allowed: {}, expected <= 10500",
            total_allowed
        );
    }

    /// 测试多节点决策链并发安全
    #[tokio::test]
    async fn test_multi_node_chain_concurrent_safety() {
        let limiter1 = Arc::new(TokenBucketLimiter::new(5000, 100));
        let limiter2 = Arc::new(ShardedSlidingWindowLimiter::new(
            Duration::from_secs(1),
            5000,
        ));
        let limiter3 = Arc::new(FixedWindowLimiter::new(Duration::from_secs(10), 5000));

        let node1 = DecisionNode::with_dependencies(
            "token_bucket".to_string(),
            "Token Bucket".to_string(),
            limiter1,
            100,
        );
        let node2 = DecisionNode::with_dependencies(
            "sliding_window".to_string(),
            "Sliding Window".to_string(),
            limiter2,
            50,
        );
        let node3 = DecisionNode::with_dependencies(
            "fixed_window".to_string(),
            "Fixed Window".to_string(),
            limiter3,
            25,
        );

        let chain = Arc::new(DecisionChain::with_dependencies(vec![node1, node2, node3]));
        let mut handles = vec![];

        let barrier = Arc::new(tokio::sync::Barrier::new(50));
        let start_signal = Arc::new(std::sync::atomic::AtomicBool::new(false));

        for _ in 0..50 {
            let chain_clone = Arc::clone(&chain);
            let barrier_clone = Arc::clone(&barrier);
            let start_signal_clone = Arc::clone(&start_signal);
            handles.push(tokio::spawn(async move {
                barrier_clone.wait().await;

                while !start_signal_clone.load(std::sync::atomic::Ordering::SeqCst) {
                    std::hint::spin_loop();
                }

                let mut local_allowed = 0;
                for _ in 0..100 {
                    if let Ok(Decision::Allowed(_)) = chain_clone.check().await {
                        local_allowed += 1;
                    }
                }
                local_allowed
            }));
        }

        start_signal.store(true, std::sync::atomic::Ordering::SeqCst);

        let mut total_allowed = 0;
        for handle in handles {
            total_allowed += handle.await.unwrap();
        }

        // 应该被最严格的限流器限制
        assert!(
            total_allowed <= 5500,
            "Total allowed: {}, expected <= 5500",
            total_allowed
        );
    }

    /// 测试决策链统计并发安全
    #[tokio::test]
    async fn test_chain_stats_concurrent_safety() {
        let limiter = Arc::new(TokenBucketLimiter::new(10000, 1000));
        let node = DecisionNode::with_dependencies(
            "stats_node".to_string(),
            "Stats Test".to_string(),
            limiter,
            100,
        );

        let chain = Arc::new(DecisionChain::with_dependencies(vec![node]));
        let mut handles = vec![];

        // 并发执行检查和统计读取
        for _ in 0..100 {
            let chain_clone = Arc::clone(&chain);
            handles.push(tokio::spawn(async move {
                for _ in 0..10 {
                    let _ = chain_clone.check().await;
                    // 并发读取统计
                    let _ = chain_clone.stats_sync();
                }
            }));
        }

        for handle in handles {
            handle.await.unwrap();
        }

        // 验证统计一致性
        let stats = chain.stats_sync();
        assert!(stats.total_checks <= 10000);
    }

    /// 测试决策链无死锁
    #[tokio::test]
    async fn test_chain_no_deadlock() {
        let limiter = Arc::new(TokenBucketLimiter::new(100000, 10000));
        let node = DecisionNode::with_dependencies(
            "deadlock_test".to_string(),
            "Deadlock Test".to_string(),
            limiter,
            100,
        );

        let chain = Arc::new(DecisionChain::with_dependencies(vec![node]));
        let mut handles = vec![];

        // 大量并发任务
        for _ in 0..500 {
            let chain_clone = Arc::clone(&chain);
            handles.push(tokio::spawn(async move {
                for _ in 0..50 {
                    let _ = chain_clone.check().await;
                }
            }));
        }

        // 使用超时确保不会死锁
        let result = tokio::time::timeout(Duration::from_secs(10), async {
            for handle in handles {
                let _ = handle.await;
            }
        })
        .await;

        assert!(result.is_ok(), "Test timed out - possible deadlock");
    }

    /// 测试节点动态启用/禁用的并发安全
    #[tokio::test]
    async fn test_node_enable_disable_concurrent_safety() {
        let limiter = Arc::new(TokenBucketLimiter::new(10000, 1000));
        let node = DecisionNode::with_dependencies(
            "toggle_node".to_string(),
            "Toggle Test".to_string(),
            limiter,
            100,
        );

        let chain = Arc::new(tokio::sync::RwLock::new(DecisionChain::with_dependencies(
            vec![node],
        )));
        let mut handles = vec![];

        // 并发执行检查和节点状态切换
        for i in 0..100 {
            let chain_clone = Arc::clone(&chain);
            handles.push(tokio::spawn(async move {
                if i % 2 == 0 {
                    // 检查请求
                    let chain = chain_clone.read().await;
                    let _ = chain.check().await;
                } else {
                    // 切换节点状态
                    let mut chain = chain_clone.write().await;
                    if i % 4 == 1 {
                        chain.disable_node("toggle_node");
                    } else {
                        chain.enable_node("toggle_node");
                    }
                }
            }));
        }

        for handle in handles {
            handle.await.unwrap();
        }

        // 验证最终状态一致
        let chain = chain.read().await;
        let stats = chain.stats_sync();
        assert!(stats.total_checks <= 10000);
    }

    /// 测试决策链边界条件 - 空链
    #[tokio::test]
    async fn test_empty_chain_boundary() {
        let chain = DecisionChain::with_dependencies(vec![]);

        // 空链应该总是允许
        for _ in 0..100 {
            let decision = chain.check().await.unwrap();
            assert_eq!(decision, Decision::allowed_default());
        }

        let stats = chain.stats_sync();
        assert_eq!(stats.total_checks, 100);
        assert_eq!(stats.allowed_count, 100);
    }

    /// 测试决策链边界条件 - 所有节点禁用
    #[tokio::test]
    async fn test_all_nodes_disabled_boundary() {
        let limiter = Arc::new(TokenBucketLimiter::new(0, 1)); // 容量为 0,会拒绝所有请求
        let node = DecisionNode::with_dependencies(
            "disabled_node".to_string(),
            "Disabled Node".to_string(),
            limiter,
            100,
        )
        .with_enabled(false);

        let chain = DecisionChain::with_dependencies(vec![node]);

        // 所有节点禁用时应该允许
        let decision = chain.check().await.unwrap();
        assert_eq!(decision, Decision::allowed_default());
    }

    /// 测试决策链成本边界条件
    #[tokio::test]
    async fn test_chain_cost_boundary() {
        let limiter = Arc::new(TokenBucketLimiter::new(100, 1));
        let node = DecisionNode::with_dependencies(
            "cost_node".to_string(),
            "Cost Test".to_string(),
            limiter,
            100,
        )
        .with_cost(50); // 每次检查消耗 50 个令牌

        let chain = DecisionChain::with_dependencies(vec![node]);

        // 第一次检查应该成功(消耗 50 个令牌)
        let decision = chain.check().await.unwrap();
        assert_eq!(decision, Decision::allowed_default());

        // 第二次检查应该成功(消耗剩余 50 个令牌)
        let decision = chain.check().await.unwrap();
        assert_eq!(decision, Decision::allowed_default());

        // 第三次检查应该失败(没有足够的令牌)
        let decision = chain.check().await.unwrap();
        assert!(matches!(decision, Decision::Rejected(_)));
    }

    // ==================== DecisionNode::new() 测试 ====================

    #[test]
    fn test_decision_node_new_backward_compat() {
        let limiter = Arc::new(TokenBucketLimiter::new(100, 10));
        let node = DecisionNode::new("node1".to_string(), "new method".to_string(), limiter, 100);
        assert_eq!(node.id, "node1");
        assert_eq!(node.name, "new method");
        assert_eq!(node.priority, 100);
        assert!(node.enabled);
        assert!(node.short_circuit);
        assert_eq!(node.cost, 1);
    }

    // ==================== DecisionChain Builder 测试 ====================

    #[test]
    fn test_decision_chain_builder_method() {
        let builder = DecisionChain::builder();
        let chain = builder.build();
        assert_eq!(chain.node_count(), 0);
    }

    #[test]
    fn test_decision_chain_builder_default_trait() {
        let _builder = DecisionChainBuilder::default();
    }

    // ==================== DecisionChain Error Path 测试 ====================

    struct ErrorLimiter;

    #[async_trait]
    impl Limiter for ErrorLimiter {
        async fn allow(&self, _cost: u64) -> Result<bool, LimiteronError> {
            Err(LimiteronError::LimitError("limit error".to_string()))
        }
    }

    #[tokio::test]
    async fn test_decision_chain_check_error_path() {
        let node = DecisionNode::with_dependencies(
            "error_node".to_string(),
            "Error Limiter".to_string(),
            Arc::new(ErrorLimiter),
            100,
        );
        let chain = DecisionChain::with_dependencies(vec![node]);
        let result = chain.check().await;
        assert!(result.is_err());

        let stats = chain.stats_sync();
        assert_eq!(stats.total_checks, 1);
        assert_eq!(stats.error_count, 1);
    }

    #[tokio::test]
    async fn test_decision_chain_error_path_second_node() {
        let ok = Arc::new(TokenBucketLimiter::new(100, 10));
        let ok_node = DecisionNode::with_dependencies("ok".to_string(), "OK".to_string(), ok, 100);
        let err_node = DecisionNode::with_dependencies(
            "err".to_string(),
            "Error".to_string(),
            Arc::new(ErrorLimiter),
            50,
        );
        let chain = DecisionChain::with_dependencies(vec![ok_node, err_node]);
        let result = chain.check().await;
        assert!(result.is_err());

        let stats = chain.stats_sync();
        assert_eq!(stats.total_checks, 1);
        assert_eq!(stats.error_count, 1);
    }

    // ==================== DecisionChain Non-Short-Circuit 测试 ====================

    #[tokio::test]
    async fn test_decision_chain_non_short_circuit_final() {
        let pass = Arc::new(MockLimiter::new(true));
        let limited = Arc::new(TokenBucketLimiter::new(3, 1));

        let node1 = DecisionNode::with_dependencies(
            "pass".to_string(),
            "Pass Through".to_string(),
            pass,
            100,
        );
        let node2 = DecisionNode::with_dependencies(
            "limited".to_string(),
            "Limited".to_string(),
            limited,
            50,
        )
        .with_short_circuit(false);

        let chain = DecisionChain::with_dependencies(vec![node1, node2]);

        for _ in 0..3 {
            let decision = chain.check().await.unwrap();
            assert_eq!(decision, Decision::allowed_default());
        }

        let decision = chain.check().await.unwrap();
        assert!(matches!(decision, Decision::Rejected(_)));
        if let Decision::Rejected(meta) = decision {
            assert!(meta.reason.contains("Limited"));
        }

        let stats = chain.stats_sync();
        assert_eq!(stats.total_checks, 5);
        assert_eq!(stats.rejected_count, 1);
    }

    // ==================== DecisionChain Node Not Found 测试 ====================

    #[tokio::test]
    async fn test_decision_chain_enable_node_not_found() {
        let limiter = Arc::new(TokenBucketLimiter::new(10, 1));
        let node =
            DecisionNode::with_dependencies("node1".to_string(), "Node".to_string(), limiter, 100);
        let mut chain = DecisionChain::with_dependencies(vec![node]);
        assert!(!chain.enable_node("nonexistent"));
    }

    #[tokio::test]
    async fn test_decision_chain_disable_node_not_found() {
        let limiter = Arc::new(TokenBucketLimiter::new(10, 1));
        let node =
            DecisionNode::with_dependencies("node1".to_string(), "Node".to_string(), limiter, 100);
        let mut chain = DecisionChain::with_dependencies(vec![node]);
        assert!(!chain.disable_node("nonexistent"));
    }

    #[tokio::test]
    async fn test_decision_chain_set_short_circuit_found() {
        let spy = Arc::new(SpyLimiter::new());
        let reject = Arc::new(MockLimiter::new(true));

        let node1 = DecisionNode::with_dependencies(
            "node1".to_string(),
            "Mock".to_string(),
            reject.clone(),
            100,
        );
        let node2 = DecisionNode::with_dependencies(
            "node2".to_string(),
            "Spy".to_string(),
            spy.clone(),
            50,
        );

        let mut chain = DecisionChain::with_dependencies(vec![node1, node2]);

        chain.check().await.unwrap();
        assert_eq!(spy.calls.load(std::sync::atomic::Ordering::SeqCst), 1);

        assert!(chain.set_short_circuit("node1", true));

        reject.set_allowed(false);
        let decision = chain.check().await.unwrap();
        assert!(matches!(decision, Decision::Rejected(_)));
        assert_eq!(spy.calls.load(std::sync::atomic::Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn test_decision_chain_set_short_circuit_not_found() {
        let limiter = Arc::new(TokenBucketLimiter::new(10, 1));
        let node =
            DecisionNode::with_dependencies("node1".to_string(), "Node".to_string(), limiter, 100);
        let mut chain = DecisionChain::with_dependencies(vec![node]);
        assert!(!chain.set_short_circuit("nonexistent", true));
    }

    // ========================================================================
    // stats race condition 修复测试
    //
    // 验证策略:
    // AtomicChainStats 的 fetch_add/store 原使用 Ordering::Relaxed,不保证
    // 跨原子变量的可见性顺序。DecisionChain::check 的调用模式是先 increment_total
    // 后 increment_allowed/rejected/error,但 Relaxed 下 snapshot 可能读到
    // total 已 increment 而分类计数尚未 increment 的中间态,违反业务不变式
    // allowed + rejected + error >= total(每次分类必伴随 total)。
    // SeqCst 保证代码顺序即全局可见顺序,消除此类中间态观测。
    // ========================================================================

    #[test]
    fn test_atomic_chain_stats_concurrent_snapshot_monotonic() {
        use std::sync::Arc;
        use std::sync::atomic::{AtomicBool, AtomicU64, Ordering as StdOrdering};
        use std::thread;
        use std::time::Duration;

        let stats = Arc::new(AtomicChainStats::new());
        let stop = Arc::new(AtomicBool::new(false));
        let violations = Arc::new(AtomicU64::new(0));

        // 工作线程:持续 increment 各计数器
        let stats_worker = Arc::clone(&stats);
        let stop_worker = Arc::clone(&stop);
        let worker = thread::spawn(move || {
            while !stop_worker.load(StdOrdering::SeqCst) {
                stats_worker.increment_total();
                stats_worker.increment_allowed();
                stats_worker.increment_rejected();
                stats_worker.increment_error();
            }
        });

        // 快照线程:持续 snapshot,验证计数器单调非递减
        let stats_snap = Arc::clone(&stats);
        let stop_snap = Arc::clone(&stop);
        let violations_snap = Arc::clone(&violations);
        let snapshotter = thread::spawn(move || {
            let mut prev = stats_snap.snapshot();
            for _ in 0..1000 {
                let curr = stats_snap.snapshot();
                if curr.total_checks < prev.total_checks
                    || curr.allowed_count < prev.allowed_count
                    || curr.rejected_count < prev.rejected_count
                    || curr.error_count < prev.error_count
                {
                    violations_snap.fetch_add(1, StdOrdering::SeqCst);
                }
                prev = curr;
            }
            let _ = stop_snap;
        });

        thread::sleep(Duration::from_millis(200));
        stop.store(true, StdOrdering::SeqCst);
        worker.join().unwrap();
        snapshotter.join().unwrap();

        assert_eq!(
            violations.load(StdOrdering::SeqCst),
            0,
            "snapshot 观测到计数器倒退:违反单调性(increment 期间不应有 reset)"
        );
    }

    #[test]
    fn test_atomic_chain_stats_check_pattern_eventual_consistency() {
        use std::sync::Arc;
        use std::thread;

        // 模拟 DecisionChain::check 的统计更新模式:
        //   拒绝:increment_total + increment_rejected
        //   错误:increment_total + increment_error
        //   允许:increment_total + increment_allowed
        // 验证"最终一致性":所有工作线程结束后,fetch_add 的原子性保证
        // total == allowed + rejected + error(每轮 total+1 且恰好一个分类+1)。
        // 注意:snapshot() 内部多个 load 非原子,无法在并发期间验证
        // 跨计数器不变式;只能在所有 increment 完成后验证最终值。
        // SeqCst 保证 fetch_add 的全局顺序,确保最终计数无丢失更新。
        let stats = Arc::new(AtomicChainStats::new());
        const THREADS: usize = 8;
        const ITERS: u64 = 1000;

        let mut handles = vec![];
        for _ in 0..THREADS {
            let stats_clone = Arc::clone(&stats);
            handles.push(thread::spawn(move || {
                for i in 0..ITERS {
                    stats_clone.increment_total();
                    match i % 3 {
                        0 => stats_clone.increment_allowed(),
                        1 => stats_clone.increment_rejected(),
                        _ => stats_clone.increment_error(),
                    }
                }
            }));
        }
        for h in handles {
            h.join().unwrap();
        }

        let snap = stats.snapshot();
        let expected_total = (THREADS as u64) * ITERS;
        let classified = snap.allowed_count + snap.rejected_count + snap.error_count;
        assert_eq!(snap.total_checks, expected_total, "total 计数丢失更新");
        assert_eq!(classified, expected_total, "分类计数丢失更新");
        // 每轮恰好一个分类+1,所以 classified == total
        assert_eq!(
            classified, snap.total_checks,
            "SeqCst 应保证 fetch_add 无丢失更新:classified 应等于 total"
        );
    }
}