inklog 0.3.0-rc.4

Enterprise-grade Rust logging infrastructure
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
// Copyright (c) 2026 Kirky.X
// SPDX-License-Identifier: MIT
//! Worker thread management for log sinks.

use super::LoggerManager;
use super::recovery::SinkControlMessage;
use crate::InklogConfig;
use crate::Metrics;
use crate::support::io::LogSink;
use crate::{InklogError, LogRecord};
use chrono::Utc;
use crossbeam_channel::{Receiver, Sender, bounded};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Duration, Instant};

/// DatabaseSink 工厂闭包类型
#[cfg(any(
    feature = "sqlite",
    feature = "postgres",
    feature = "mysql",
    feature = "duckdb"
))]
type DbSinkFactory = Box<
    dyn Fn(
            Arc<dyn crate::integrations::Database>,
            Arc<Metrics>,
        ) -> Result<Box<dyn LogSink>, InklogError>
        + Send
        + Sync,
>;

/// Parameters for worker threads
pub(crate) struct WorkerParams {
    pub(crate) config: InklogConfig,
    pub(crate) receiver: Receiver<Arc<LogRecord>>,
    pub(crate) console_receiver: Receiver<Arc<LogRecord>>,
    pub(crate) control_rx: Receiver<SinkControlMessage>,
    pub(crate) control_tx: Sender<SinkControlMessage>,
    pub(crate) metrics: Arc<Metrics>,
    /// Console sink(生产中从不被替换,直接共享句柄,无锁)
    pub(crate) console_sink: Arc<dyn LogSink>,
    /// 同上:锁内仅克隆 `Option<Arc<dyn LogSink>>`,异步写在锁外执行
    pub(crate) error_sink: Arc<Mutex<Option<Arc<dyn LogSink>>>>,
    pub(crate) effective_capacity: Arc<AtomicUsize>,
    /// FileSink 工厂闭包(用于初始创建和恢复,打破具体类型依赖)
    pub(crate) file_sink_factory:
        Box<dyn Fn() -> Result<Box<dyn LogSink>, InklogError> + Send + Sync>,
    /// DatabaseSink 工厂闭包(用于初始创建和恢复,内部处理 set_metrics)
    #[cfg(any(
        feature = "sqlite",
        feature = "postgres",
        feature = "mysql",
        feature = "duckdb"
    ))]
    pub(crate) db_sink_factory: DbSinkFactory,
    /// 注入的数据库依赖(DI 模式)
    #[cfg(any(
        feature = "sqlite",
        feature = "postgres",
        feature = "mysql",
        feature = "duckdb"
    ))]
    pub(crate) database: Option<Arc<dyn crate::integrations::Database>>,
    /// 数据库 sink 专用数据 channel 接收端(独立于 file worker 的 receiver)
    #[cfg(any(
        feature = "sqlite",
        feature = "postgres",
        feature = "mysql",
        feature = "duckdb"
    ))]
    pub(crate) db_receiver: Option<Receiver<Arc<LogRecord>>>,
    /// 动态注册的第三方 sink:每项拥有独立 channel 接收端,
    /// 由通用 SinkWorker 消费。每个条目独立命名用于健康上报。
    pub(crate) custom_sinks: Vec<CustomSinkEntry>,
}

/// 动态注册 sink 的 worker 条目。
pub(crate) struct CustomSinkEntry {
    /// 健康上报与指标使用的 sink 名("custom-N")
    pub(crate) name: String,
    /// 第三方 sink 实例(零核心改动接入)
    pub(crate) sink: Arc<dyn LogSink>,
    /// 该 sink 专属 channel 的接收端
    pub(crate) receiver: Receiver<Arc<LogRecord>>,
}

/// `start_workers` 返回值类型别名,避免 clippy `type_complexity` 警告。
/// 第一项为 worker 线程句柄,第二项为每个 worker 对应的 shutdown 信号 sender。
pub(crate) type WorkerStartResult =
    Result<(Vec<tokio::task::JoinHandle<()>>, Vec<Sender<()>>), InklogError>;

/// 自定义 sink 写失败重试上限(与内置 worker 的 WRITE_MAX_ATTEMPTS 一致)。
const CUSTOM_SINK_WRITE_ATTEMPTS: u32 = 3;

/// 通用 SinkWorker 主循环,消费动态注册 sink 的专属 channel。
///
/// 语义与内置 file/db worker 对齐:
/// - 记录产生→处理的延迟计入 metrics;
/// - 写失败重试(最多 3 次,间隔 10ms×attempt),重试耗尽计 sink_error 并
///   降级写 console,绝不无声丢弃;
/// - shutdown 信号后限时排水,最后 flush + shutdown sink;
/// - channel 断开(所有发送端丢弃)且排空后 worker 退出。
pub(crate) fn run_custom_sink_worker(
    runtime_handle: &tokio::runtime::Handle,
    metrics: &Metrics,
    console_sink: &Arc<dyn LogSink>,
    entry: &CustomSinkEntry,
    receiver: &Receiver<Arc<LogRecord>>,
    shutdown: &Receiver<()>,
) {
    loop {
        if shutdown.try_recv().is_ok() {
            // Drain with 5s timeout
            let deadline = Instant::now() + Duration::from_secs(5);
            while let Ok(record) = receiver.try_recv() {
                metrics.record_latency(record_age(&record));
                let _ = runtime_handle.block_on(async { entry.sink.write(&record).await });
                if Instant::now() > deadline {
                    break;
                }
            }
            let _ = runtime_handle.block_on(async { entry.sink.flush().await });
            let _ = runtime_handle.block_on(async { entry.sink.shutdown().await });
            break;
        }

        match receiver.recv_timeout(Duration::from_millis(100)) {
            Ok(record) => {
                metrics.record_latency(record_age(&record));
                let mut written = false;
                for attempt in 1..=CUSTOM_SINK_WRITE_ATTEMPTS {
                    match runtime_handle.block_on(async { entry.sink.write(&record).await }) {
                        Ok(_) => {
                            metrics.inc_logs_written();
                            metrics.update_sink_health(&entry.name, true, None);
                            written = true;
                            break;
                        }
                        Err(e) => {
                            tracing::error!(
                                error = %e,
                                attempt,
                                "custom sink '{}' write failed",
                                entry.name
                            );
                            if attempt == CUSTOM_SINK_WRITE_ATTEMPTS {
                                metrics.inc_sink_error();
                                metrics.update_sink_health(&entry.name, false, Some(e.to_string()));
                                let _ = runtime_handle
                                    .block_on(async { console_sink.write(&record).await });
                            } else {
                                thread::sleep(Duration::from_millis(10 * attempt as u64));
                            }
                        }
                    }
                }
                let _ = written;
            }
            Err(crossbeam_channel::RecvTimeoutError::Timeout) => {
                // Idle tick: flush buffered records periodically
                let _ = runtime_handle.block_on(async { entry.sink.flush().await });
            }
            // 记录发送端全部丢弃且通道已空:worker 退出(与内置 worker 一致,
            // 避免 tokio Runtime drop 等待 blocking 任务时永久挂死)
            Err(crossbeam_channel::RecvTimeoutError::Disconnected) => break,
        }
    }
}

// ============================================================================
// Extracted pure functions (testable without runtime/threads)
// ============================================================================

/// Check whether auto-recovery should be attempted based on consecutive
/// failure count and elapsed time since the last failure.
pub(crate) fn should_auto_recover(
    consecutive_failures: u32,
    last_failure_time: Option<Instant>,
) -> bool {
    consecutive_failures > 5
        && last_failure_time
            .map(|t| t.elapsed() > Duration::from_secs(60))
            .unwrap_or(false)
}

/// Check whether a recovery attempt should be made, respecting a cooldown
/// period between attempts.
pub(crate) fn should_attempt_recovery(last_attempt: Option<&Instant>, cooldown: Duration) -> bool {
    match last_attempt {
        None => true,
        Some(inst) => inst.elapsed() > cooldown,
    }
}

/// Result of classifying a [`SinkControlMessage`] for a specific target sink.
pub(crate) enum ControlAction {
    /// Attempt to recover the target sink.
    Recover,
    /// Message is for a different sink; ignore.
    Ignore,
}

/// Classify a control message relative to a target sink name.
pub(crate) fn classify_control_message(
    msg: &SinkControlMessage,
    target_sink: &str,
) -> ControlAction {
    match msg {
        SinkControlMessage::RecoverSink(name) if name == target_sink => ControlAction::Recover,
        _ => ControlAction::Ignore,
    }
}

/// Compute the new adaptive channel capacity given current usage.
///
/// Returns the updated capacity value.
#[allow(clippy::too_many_arguments)]
pub(crate) fn update_adaptive_capacity(
    current_eff: usize,
    channel_len: usize,
    min_capacity: usize,
    max_capacity: usize,
    expand_threshold_percent: u8,
    shrink_threshold_percent: u8,
    shrink_wait: Duration,
    low_usage_since: &mut Option<Instant>,
) -> usize {
    let usage = if current_eff > 0 {
        channel_len as f64 / current_eff as f64
    } else {
        0.0
    };
    let usage_percent = (usage * 100.0).round() as u8;

    if usage_percent >= expand_threshold_percent && current_eff < max_capacity {
        let grow_to = (current_eff + current_eff / 2).min(max_capacity);
        *low_usage_since = None;
        grow_to
    } else if usage_percent <= shrink_threshold_percent && current_eff > min_capacity {
        match low_usage_since {
            None => {
                *low_usage_since = Some(Instant::now());
                current_eff
            }
            Some(inst) => {
                if inst.elapsed() >= shrink_wait {
                    let shrink_to = (current_eff.saturating_mul(70) / 100).max(min_capacity);
                    *low_usage_since = None;
                    shrink_to
                } else {
                    current_eff
                }
            }
        }
    } else {
        *low_usage_since = None;
        current_eff
    }
}

/// sink 工厂失败重试的初始退避:1s 起,每轮翻倍,上限 30s。
/// 持续重试而非放弃,下游存储恢复后 worker 自动恢复写入。
const FACTORY_RETRY_INITIAL_BACKOFF: Duration = Duration::from_secs(1);
const FACTORY_RETRY_MAX_BACKOFF: Duration = Duration::from_secs(30);

/// 测试钩子:非零时覆盖工厂重试的初始退避(毫秒),避免测试等待真实的秒级退避。
/// 用完全限定路径声明,避免非测试构建出现未使用的导入。
#[cfg(test)]
static FACTORY_RETRY_INITIAL_BACKOFF_MS: std::sync::atomic::AtomicU64 =
    std::sync::atomic::AtomicU64::new(0);

/// 当前生效的工厂重试初始退避(测试可注入更短值)。
fn factory_retry_initial_backoff() -> Duration {
    #[cfg(test)]
    {
        let ms = FACTORY_RETRY_INITIAL_BACKOFF_MS.load(Ordering::Relaxed);
        if ms > 0 {
            return Duration::from_millis(ms);
        }
    }
    FACTORY_RETRY_INITIAL_BACKOFF
}

/// sink 写失败重试上限(含首次写入):达到该次数后计 sink_error、
/// 健康置 false 并降级写 console。
const WRITE_MAX_ATTEMPTS: u32 = 3;

/// 同类 sink worker 间的静态差异:健康/降级记录使用的名字与文案 key。
struct SinkWorkerDescriptor {
    /// 健康状态与降级记录使用的 sink 名
    name: &'static str,
    /// 日志文案中的 sink 标签
    label: &'static str,
    /// error.log 记录的 target
    error_target: &'static str,
    /// 手动恢复(控制消息)文案 key
    recovery_received_key: &'static str,
    recovered_key: &'static str,
    recovery_failed_key: &'static str,
    /// 自动恢复文案 key
    auto_recovery_key: &'static str,
    auto_recovery_ok_key: &'static str,
}

const FILE_SINK_WORKER: SinkWorkerDescriptor = SinkWorkerDescriptor {
    name: "file",
    label: "File",
    error_target: "inklog::file_sink",
    recovery_received_key: "sink-file_recovery_received",
    recovered_key: "sink-file_recovered",
    recovery_failed_key: "sink-file_recovery_failed",
    auto_recovery_key: "sink-file_auto_recovery",
    auto_recovery_ok_key: "sink-file_auto_recovery_ok",
};

#[cfg(any(
    feature = "sqlite",
    feature = "postgres",
    feature = "mysql",
    feature = "duckdb"
))]
const DB_SINK_WORKER: SinkWorkerDescriptor = SinkWorkerDescriptor {
    name: "database",
    label: "Database",
    error_target: "inklog::database_sink",
    recovery_received_key: "sink-db_recovery_received",
    recovered_key: "sink-db_recovered",
    recovery_failed_key: "sink-db_recovery_failed",
    auto_recovery_key: "sink-db_auto_recovery",
    auto_recovery_ok_key: "sink-db_auto_recovery_ok",
};

/// sink 工厂的动态引用(初始创建/工厂重试/恢复共用)。
type SinkFactory<'a> = &'a mut dyn FnMut() -> Result<Box<dyn LogSink>, InklogError>;

/// 跨记录维护的写失败状态(驱动重试计数与自动恢复判定)。
#[derive(Default)]
struct FailureState {
    consecutive_failures: u32,
    last_failure_time: Option<Instant>,
}

/// 单个 sink worker 的跨记录可变状态。
struct SinkWorkerState {
    /// 当前 sink;None 表示降级模式(工厂持续失败)
    sink: Option<Box<dyn LogSink>>,
    failures: FailureState,
    /// 工厂重试退避(1s 起指数翻倍,上限 30s)
    factory_backoff: Duration,
    last_factory_attempt: Instant,
}

/// sink worker 共享上下文:drain/主循环共用的重试、降级与恢复逻辑。
struct SinkWorker<'a> {
    desc: &'static SinkWorkerDescriptor,
    runtime_handle: &'a tokio::runtime::Handle,
    metrics: &'a Metrics,
    console_sink: &'a Arc<dyn LogSink>,
    error_sink: &'a Arc<Mutex<Option<Arc<dyn LogSink>>>>,
}

/// 记录从产生到被处理的延迟。
fn record_age(record: &LogRecord) -> Duration {
    Utc::now()
        .signed_duration_since(record.timestamp)
        .to_std()
        .unwrap_or(Duration::ZERO)
}

impl SinkWorker<'_> {
    /// 初始创建 sink;失败进入降级模式(指数退避持续重试工厂,
    /// 期间到达的记录写入 error sink 并计为 failed),worker 保持存活。
    fn create_initial_state(&self, create_sink: SinkFactory<'_>) -> SinkWorkerState {
        let sink = match create_sink() {
            Ok(sink) => Some(sink),
            Err(e) => {
                tracing::error!(
                    error = %e,
                    "{} sink factory failed on startup; entering degraded retry mode (exponential backoff: 1s doubling up to 30s, retrying indefinitely); records arriving during retry are forwarded to the error sink and counted as failed",
                    self.desc.label
                );
                self.metrics
                    .update_sink_health(self.desc.name, false, Some(e.to_string()));
                None
            }
        };
        SinkWorkerState {
            sink,
            failures: FailureState::default(),
            factory_backoff: factory_retry_initial_backoff(),
            last_factory_attempt: Instant::now(),
        }
    }

    /// 处理单条记录:记录延迟;降级模式保底写 error sink;
    /// 否则带重试写入,最终失败后触发自动恢复。
    fn handle_record(
        &self,
        state: &mut SinkWorkerState,
        record: &Arc<LogRecord>,
        create_sink: SinkFactory<'_>,
    ) {
        self.metrics.record_latency(record_age(record));

        // 降级模式(工厂持续失败):无 sink 可写——保底到
        // error sink 并计为 failed,绝不无声丢弃
        let Some(sink) = state.sink.as_mut() else {
            self.handle_sink_unavailable(record);
            return;
        };

        let write_succeeded = self.write_with_retry(sink, record, &mut state.failures);
        if !write_succeeded {
            self.maybe_auto_recover(&mut state.failures, sink, create_sink);
        }
    }

    /// 降级模式(工厂持续失败)下到达记录的降级处理:
    /// 尽力写入 error sink 保留内容,并递增 failed/dropped 指标与 sink 健康状态,
    /// 绝不无声丢弃。锁内仅取句柄,异步写在锁外执行(与热路径约定一致)。
    fn handle_sink_unavailable(&self, record: &Arc<LogRecord>) {
        self.metrics.inc_sink_error();
        self.metrics.inc_logs_dropped();
        self.metrics.update_sink_health(
            self.desc.name,
            false,
            Some("sink unavailable: factory keeps failing".to_string()),
        );
        let error_sink_handle = self.error_sink.lock().ok().and_then(|guard| guard.clone());
        if let Some(error_sink) = error_sink_handle {
            let _ = self
                .runtime_handle
                .block_on(async { error_sink.write(record).await });
        }
    }

    /// 单条记录的带重试写入;返回是否最终成功。
    /// 契约:最多 3 次尝试,失败间隔 sleep(10ms×attempt);第 3 次失败后
    /// 计 sink_error、健康置 false 并降级写 console。
    fn write_with_retry(
        &self,
        sink: &mut Box<dyn LogSink>,
        record: &Arc<LogRecord>,
        failures: &mut FailureState,
    ) -> bool {
        let mut attempts = 0;
        while attempts < WRITE_MAX_ATTEMPTS {
            match self
                .runtime_handle
                .block_on(async { sink.write(record).await })
            {
                Ok(_) => {
                    self.metrics.inc_logs_written();
                    self.metrics.update_sink_health(self.desc.name, true, None);
                    failures.consecutive_failures = 0;
                    failures.last_failure_time = None;
                    return true;
                }
                Err(e) => {
                    attempts += 1;
                    failures.consecutive_failures += 1;
                    failures.last_failure_time = Some(Instant::now());

                    self.write_error_log(&e);

                    if attempts == WRITE_MAX_ATTEMPTS {
                        self.metrics.inc_sink_error();
                        self.metrics
                            .update_sink_health(self.desc.name, false, Some(e.to_string()));
                        self.fallback_to_console(record);
                    } else {
                        thread::sleep(Duration::from_millis(10 * attempts as u64));
                    }
                }
            }
        }
        false
    }

    /// 写 error.log。锁内仅取句柄,异步写在锁外执行。
    fn write_error_log(&self, error: &InklogError) {
        let error_sink_handle = self.error_sink.lock().ok().and_then(|guard| guard.clone());
        let Some(error_sink) = error_sink_handle else {
            return;
        };
        let error_record = LogRecord {
            timestamp: Utc::now(),
            level: "ERROR".to_string(),
            target: self.desc.error_target.to_string(),
            message: format!("{} sink error: {}", self.desc.label, error),
            fields: Default::default(),
            file: None,
            line: None,
            thread_id: thread::current().name().unwrap_or("unknown").to_string(),
            trace_id: None,
            span_id: None,
        };
        let _ = self
            .runtime_handle
            .block_on(async { error_sink.write(&error_record).await });
    }

    /// 重试耗尽后的 console 降级。
    fn fallback_to_console(&self, record: &Arc<LogRecord>) {
        let _ = self
            .runtime_handle
            .block_on(async { self.console_sink.write(record).await });
    }

    /// 写失败后的自动恢复触发(连续失败 > 5 且距上次失败 > 60s)。
    fn maybe_auto_recover(
        &self,
        failures: &mut FailureState,
        sink: &mut Box<dyn LogSink>,
        create_sink: SinkFactory<'_>,
    ) {
        if !should_auto_recover(failures.consecutive_failures, failures.last_failure_time) {
            return;
        }
        tracing::warn!("{}", crate::i18n::tr(self.desc.auto_recovery_key));
        if let Ok(new_sink) = create_sink() {
            *sink = new_sink;
            failures.consecutive_failures = 0;
            failures.last_failure_time = None;
            self.metrics.update_sink_health(self.desc.name, true, None);
            tracing::info!("{}", crate::i18n::tr(self.desc.auto_recovery_ok_key));
        }
    }

    /// 处理一条控制消息(每次循环迭代最多一条,与主循环节奏一致)。
    fn handle_control_message(
        &self,
        state: &mut SinkWorkerState,
        msg: &SinkControlMessage,
        create_sink: SinkFactory<'_>,
    ) {
        match classify_control_message(msg, self.desc.name) {
            ControlAction::Recover => {
                tracing::info!("{}", crate::i18n::tr(self.desc.recovery_received_key));
                if let Ok(new_sink) = create_sink() {
                    state.sink = Some(new_sink);
                    state.factory_backoff = factory_retry_initial_backoff();
                    state.failures.consecutive_failures = 0;
                    state.failures.last_failure_time = None;
                    self.metrics.update_sink_health(self.desc.name, true, None);
                    tracing::info!("{}", crate::i18n::tr(self.desc.recovered_key));
                } else {
                    tracing::error!("{}", crate::i18n::tr(self.desc.recovery_failed_key));
                }
            }
            ControlAction::Ignore => {}
        }
    }

    /// 降级模式下按指数退避重试工厂(1s→2s→…上限 30s),持续重试而非放弃。
    /// 用时间判断而非 sleep,循环保持即时响应 shutdown 与控制消息;
    /// 放在 recv 之前,降级路径跳过记录处理时也不会跳过重试。
    fn retry_factory_if_due(&self, state: &mut SinkWorkerState, create_sink: SinkFactory<'_>) {
        if state.sink.is_some() || state.last_factory_attempt.elapsed() < state.factory_backoff {
            return;
        }
        match create_sink() {
            Ok(new_sink) => {
                state.sink = Some(new_sink);
                state.factory_backoff = factory_retry_initial_backoff();
                self.metrics.update_sink_health(self.desc.name, true, None);
                tracing::info!(
                    "{} sink factory succeeded after retry; worker resumed normal writes",
                    self.desc.label
                );
            }
            Err(e) => {
                tracing::error!(
                    error = %e,
                    next_retry_in_ms = state.factory_backoff.as_millis() as u64,
                    "{} sink factory retry failed; keeping the worker alive and retrying with exponential backoff",
                    self.desc.label
                );
                state.factory_backoff = (state.factory_backoff * 2).min(FACTORY_RETRY_MAX_BACKOFF);
            }
        }
        state.last_factory_attempt = Instant::now();
    }

    /// shutdown 后的排水:限时尽力写完 channel 剩余记录,最后 shutdown sink。
    fn drain(
        &self,
        state: &mut SinkWorkerState,
        receiver: &Receiver<Arc<LogRecord>>,
        timeout: Duration,
        create_sink: SinkFactory<'_>,
    ) {
        let deadline = Instant::now() + timeout;
        while let Ok(record) = receiver.try_recv() {
            self.handle_record(state, &record, create_sink);
            if Instant::now() > deadline {
                break;
            }
        }
        if let Some(sink) = state.sink.as_ref() {
            let _ = self
                .runtime_handle
                .block_on(async { sink.shutdown().await });
        }
    }

    /// 空闲超时:flush sink 缓冲(降级模式下无 sink 可 flush)。
    fn flush_idle(&self, state: &SinkWorkerState) {
        if let Some(sink) = state.sink.as_ref() {
            let _ = self.runtime_handle.block_on(async { sink.flush().await });
        }
    }
}

impl LoggerManager {
    pub(crate) fn start_workers(params: WorkerParams) -> WorkerStartResult {
        let runtime_handle = tokio::runtime::Handle::current();
        let WorkerParams {
            config,
            receiver,
            console_receiver,
            control_rx,
            control_tx,
            metrics,
            console_sink,
            error_sink,
            effective_capacity,
            file_sink_factory,
            #[cfg(any(
                feature = "sqlite",
                feature = "postgres",
                feature = "mysql",
                feature = "duckdb"
            ))]
            db_sink_factory,
            #[cfg(any(
                feature = "sqlite",
                feature = "postgres",
                feature = "mysql",
                feature = "duckdb"
            ))]
            database,
            #[cfg(any(
                feature = "sqlite",
                feature = "postgres",
                feature = "mysql",
                feature = "duckdb"
            ))]
            db_receiver,
            custom_sinks,
        } = params;
        let file_config = config.file_sink.clone();
        #[cfg(any(
            feature = "sqlite",
            feature = "postgres",
            feature = "mysql",
            feature = "duckdb"
        ))]
        let db_config = config.database_sink.clone();

        // database 依赖由调用方(build_detached,async 上下文)保证有效:DI 注入优先,
        // 未注入且 db sink 启用时由 build_detached 在当前 runtime 上创建默认 DbNexusAdapter。
        // 核正:原先在此处经 Handle::current().block_on 同步创建——start_workers 在
        // runtime 线程上被调用时必然 panic(runtime-in-runtime),故上移至 async 层。

        // Thread 0: Console Sink (dedicated for lock-free hot path)
        // 每个 worker 拥有独立的 shutdown channel,确保广播信号能被每个 worker 接收
        // (MPMC channel 的 send() 只能被一个 receiver 消费,共享 channel 会导致
        // 只有首个 worker 收到信号、其余 worker 死循环)
        let (shutdown_tx_console, shutdown_console) = bounded(1);
        let metrics_console = metrics.clone();
        let console_sink_console = console_sink.clone();
        let handle_console = {
            let runtime_handle = runtime_handle.clone();
            tokio::task::spawn_blocking(move || {
                metrics_console.active_workers.inc();
                loop {
                    // Check for shutdown
                    if shutdown_console.try_recv().is_ok() {
                        // Drain with 5s timeout (console is fast)
                        let deadline = Instant::now() + Duration::from_secs(5);
                        while let Ok(record) = console_receiver.try_recv() {
                            metrics_console.record_latency(record_age(&record));

                            if runtime_handle
                                .block_on(async { console_sink_console.write(&record).await })
                                .is_err()
                            {
                                metrics_console.inc_sink_error();
                            }

                            if Instant::now() > deadline {
                                break;
                            }
                        }
                        break;
                    }

                    // Process console logs with timeout
                    match console_receiver.recv_timeout(Duration::from_millis(100)) {
                        Ok(record) => {
                            metrics_console.record_latency(record_age(&record));

                            match runtime_handle
                                .block_on(async { console_sink_console.write(&record).await })
                            {
                                Ok(_) => {
                                    metrics_console.inc_logs_written();
                                    metrics_console.update_sink_health("console", true, None);
                                }
                                Err(_) => {
                                    metrics_console.inc_sink_error();
                                    metrics_console.update_sink_health(
                                        "console",
                                        false,
                                        Some("Write error".to_string()),
                                    );
                                }
                            }
                        }
                        Err(crossbeam_channel::RecvTimeoutError::Timeout) => {
                            // Timeout, continue loop
                        }
                        Err(crossbeam_channel::RecvTimeoutError::Disconnected) => {
                            break;
                        }
                    }
                }
                metrics_console.active_workers.dec();
            })
        };

        // Thread 1: File Sink
        let rx_file = receiver.clone();
        let (shutdown_tx_file, shutdown_file) = bounded(1);
        let metrics_file = metrics.clone();
        let console_sink_file = console_sink.clone();
        let error_sink_file = error_sink.clone();
        let control_rx_file = control_rx.clone();
        let handle_file = {
            let runtime_handle = runtime_handle.clone();
            tokio::task::spawn_blocking(move || {
                metrics_file.active_workers.inc();
                if let Some(cfg) = file_config
                    && cfg.enabled
                {
                    // 工厂启动失败不退出 worker:进入降级模式(见
                    // create_initial_state / retry_factory_if_due)——error 日志
                    // + 指数退避持续重试工厂,期间到达的记录写入 error sink 并
                    // 计为 failed(handle_sink_unavailable),绝不无声丢弃。
                    let worker = SinkWorker {
                        desc: &FILE_SINK_WORKER,
                        runtime_handle: &runtime_handle,
                        metrics: &metrics_file,
                        console_sink: &console_sink_file,
                        error_sink: &error_sink_file,
                    };
                    let mut create_sink = || file_sink_factory();
                    let mut state = worker.create_initial_state(&mut create_sink);

                    loop {
                        // Check for shutdown
                        if shutdown_file.try_recv().is_ok() {
                            // Drain with 30s timeout
                            worker.drain(
                                &mut state,
                                &rx_file,
                                Duration::from_secs(30),
                                &mut create_sink,
                            );
                            break;
                        }

                        // Check for control messages
                        if let Ok(control_msg) = control_rx_file.try_recv() {
                            worker.handle_control_message(
                                &mut state,
                                &control_msg,
                                &mut create_sink,
                            );
                        }

                        // 降级模式:工厂重试放在 recv 之前,跳过记录处理时也不会跳过重试
                        worker.retry_factory_if_due(&mut state, &mut create_sink);

                        match rx_file.recv_timeout(Duration::from_millis(100)) {
                            Ok(record) => {
                                worker.handle_record(&mut state, &record, &mut create_sink);
                            }
                            Err(crossbeam_channel::RecvTimeoutError::Timeout) => {
                                // Timeout, flush buffer
                                worker.flush_idle(&state);
                            }
                            // 记录发送端全部丢弃且通道已空:worker 退出而非空转
                            // (否则 tokio Runtime drop 等待 blocking 任务时永久挂死)
                            Err(crossbeam_channel::RecvTimeoutError::Disconnected) => break,
                        }
                    }
                }
                metrics_file.active_workers.dec();
            })
        };

        // Thread 2: DB Sink
        #[cfg(any(
            feature = "sqlite",
            feature = "postgres",
            feature = "mysql",
            feature = "duckdb"
        ))]
        let (shutdown_tx_db, shutdown_db) = bounded(1);
        #[cfg(any(
            feature = "sqlite",
            feature = "postgres",
            feature = "mysql",
            feature = "duckdb"
        ))]
        let metrics_db = metrics.clone();
        #[cfg(any(
            feature = "sqlite",
            feature = "postgres",
            feature = "mysql",
            feature = "duckdb"
        ))]
        let console_sink_db = console_sink.clone();
        #[cfg(any(
            feature = "sqlite",
            feature = "postgres",
            feature = "mysql",
            feature = "duckdb"
        ))]
        let error_sink_db = error_sink.clone();
        #[cfg(any(
            feature = "sqlite",
            feature = "postgres",
            feature = "mysql",
            feature = "duckdb"
        ))]
        let control_rx_db = control_rx.clone();
        #[cfg(any(
            feature = "sqlite",
            feature = "postgres",
            feature = "mysql",
            feature = "duckdb"
        ))]
        let handle_db = {
            let runtime_handle = runtime_handle.clone();
            tokio::task::spawn_blocking(move || {
                metrics_db.active_workers.inc();
                // database 依赖由调用方(build_detached,async 上下文)保证有效
                if let Some(cfg) = db_config
                    && cfg.enabled
                    && let Some(ref db) = database
                    && let Some(rx_db) = db_receiver
                {
                    let worker = SinkWorker {
                        desc: &DB_SINK_WORKER,
                        runtime_handle: &runtime_handle,
                        metrics: &metrics_db,
                        console_sink: &console_sink_db,
                        error_sink: &error_sink_db,
                    };
                    let mut create_sink = || db_sink_factory(db.clone(), metrics_db.clone());
                    let mut state = worker.create_initial_state(&mut create_sink);

                    loop {
                        // Check for shutdown
                        if shutdown_db.try_recv().is_ok() {
                            // Drain with 30s timeout
                            worker.drain(
                                &mut state,
                                &rx_db,
                                Duration::from_secs(30),
                                &mut create_sink,
                            );
                            break;
                        }

                        // Check for control messages
                        if let Ok(control_msg) = control_rx_db.try_recv() {
                            worker.handle_control_message(
                                &mut state,
                                &control_msg,
                                &mut create_sink,
                            );
                        }

                        // 降级模式:工厂重试放在 recv 之前,跳过记录处理时也不会跳过重试
                        worker.retry_factory_if_due(&mut state, &mut create_sink);

                        match rx_db.recv_timeout(Duration::from_millis(100)) {
                            Ok(record) => {
                                worker.handle_record(&mut state, &record, &mut create_sink);
                            }
                            Err(crossbeam_channel::RecvTimeoutError::Timeout) => {
                                // Timeout, flush buffer
                                worker.flush_idle(&state);
                            }
                            // 同 file worker:发送端全部丢弃且通道已空时退出
                            Err(crossbeam_channel::RecvTimeoutError::Disconnected) => break,
                        }
                    }
                }
                metrics_db.active_workers.dec();
            })
        };

        // dynamic third-party sinks — one generic SinkWorker per entry,
        // each consuming its own dedicated channel (no MPMC contention with
        // the built-in file/db workers).
        let mut custom_handles = Vec::with_capacity(custom_sinks.len());
        let mut custom_shutdown_txs = Vec::with_capacity(custom_sinks.len());
        for entry in custom_sinks {
            let (shutdown_tx, shutdown_rx) = bounded(1);
            let metrics_custom = metrics.clone();
            let console_sink_custom = console_sink.clone();
            let runtime_handle_custom = runtime_handle.clone();
            custom_handles.push(tokio::task::spawn_blocking(move || {
                metrics_custom.active_workers.inc();
                run_custom_sink_worker(
                    &runtime_handle_custom,
                    &metrics_custom,
                    &console_sink_custom,
                    &entry,
                    &entry.receiver,
                    &shutdown_rx,
                );
                metrics_custom.active_workers.dec();
            }));
            custom_shutdown_txs.push(shutdown_tx);
        }

        // Health Check Thread
        let (shutdown_tx_health, shutdown_health) = bounded(1);
        let metrics_health = metrics.clone();
        let effective_capacity_health = effective_capacity.clone();
        let handle_health = tokio::task::spawn_blocking(move || {
            let mut last_recovery_attempt = std::collections::HashMap::<String, Instant>::new();
            let mut low_usage_since: Option<Instant> = None;
            let check_interval = Duration::from_secs(1);

            loop {
                match shutdown_health.recv_timeout(check_interval) {
                    // 正常 shutdown 信号
                    Ok(_) => break,
                    Err(crossbeam_channel::RecvTimeoutError::Timeout) => {}
                    // 发送端已全部丢弃(如 manager 未调 shutdown 即被丢弃):
                    // 必须退出,否则 recv_timeout 立即返回 Disconnected,
                    // 本线程全速空转且 tokio Runtime drop 永久等待
                    Err(crossbeam_channel::RecvTimeoutError::Disconnected) => break,
                }

                // Active recovery logic with control channel
                let current_eff = effective_capacity_health.load(Ordering::Relaxed);
                let channel_len_now = receiver.len();
                let status = metrics_health.get_status(channel_len_now, current_eff);

                // Adaptive capacity strategy
                if config.performance.channel_strategy == crate::ChannelStrategy::Adaptive {
                    let new_cap = update_adaptive_capacity(
                        current_eff,
                        channel_len_now,
                        config.performance.min_capacity,
                        config.performance.max_capacity,
                        config.performance.expand_threshold_percent,
                        config.performance.shrink_threshold_percent,
                        Duration::from_secs(config.performance.shrink_wait_seconds),
                        &mut low_usage_since,
                    );
                    effective_capacity_health.store(new_cap, Ordering::Relaxed);
                }
                for (name, sink_status) in status.sinks {
                    if !sink_status.status.is_operational() {
                        let mut args = fluent_bundle::FluentArgs::new();
                        args.set("name", name.clone());
                        args.set("error", format!("{:?}", sink_status.last_error));
                        tracing::warn!("{}", crate::i18n::tr_args("sink-health_unhealthy", args));

                        // Check if we should attempt recovery
                        let should_recover = should_attempt_recovery(
                            last_recovery_attempt.get(&name),
                            Duration::from_secs(30),
                        );

                        if should_recover && sink_status.consecutive_failures > 3 {
                            let mut args = fluent_bundle::FluentArgs::new();
                            args.set("name", name.clone());
                            tracing::warn!(
                                "{}",
                                crate::i18n::tr_args("sink-health_attempting_recovery", args)
                            );

                            // Send recovery command
                            if let Err(e) =
                                control_tx.send(SinkControlMessage::RecoverSink(name.clone()))
                            {
                                let mut args = fluent_bundle::FluentArgs::new();
                                args.set("name", name.clone());
                                args.set("err", e.to_string());
                                tracing::error!(
                                    "{}",
                                    crate::i18n::tr_args("sink-health_send_failed", args)
                                );
                            } else {
                                last_recovery_attempt.insert(name.clone(), Instant::now());
                                tracing::info!(
                                    "Health Check: Recovery command sent for sink '{}'",
                                    name
                                );
                            }
                        }

                        // If error count is very high, trigger critical alert
                        if sink_status.consecutive_failures > 10 {
                            tracing::error!(
                                "CRITICAL: Sink '{}' has high error count ({})",
                                name,
                                sink_status.consecutive_failures
                            );
                        }
                    } else {
                        // Sink is healthy, clear recovery cooldown
                        last_recovery_attempt.remove(&name);
                    }
                }
            }
        });

        #[cfg(any(
            feature = "sqlite",
            feature = "postgres",
            feature = "mysql",
            feature = "duckdb"
        ))]
        let mut handles = vec![handle_console, handle_file, handle_db, handle_health];
        #[cfg(not(any(
            feature = "sqlite",
            feature = "postgres",
            feature = "mysql",
            feature = "duckdb"
        )))]
        let mut handles = vec![handle_console, handle_file, handle_health];
        handles.extend(custom_handles);

        // shutdown_txs 与 handles 一一对应,保持 cfg 一致性
        #[cfg(any(
            feature = "sqlite",
            feature = "postgres",
            feature = "mysql",
            feature = "duckdb"
        ))]
        let mut shutdown_txs = vec![
            shutdown_tx_console,
            shutdown_tx_file,
            shutdown_tx_db,
            shutdown_tx_health,
        ];
        #[cfg(not(any(
            feature = "sqlite",
            feature = "postgres",
            feature = "mysql",
            feature = "duckdb"
        )))]
        let mut shutdown_txs = vec![shutdown_tx_console, shutdown_tx_file, shutdown_tx_health];
        shutdown_txs.extend(custom_shutdown_txs);

        Ok((handles, shutdown_txs))
    }
}

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

    // ========================================================================
    // should_auto_recover
    // ========================================================================

    #[test]
    fn test_should_auto_recover_low_failures() {
        assert!(!should_auto_recover(
            5,
            Some(Instant::now() - Duration::from_secs(120))
        ));
        assert!(!should_auto_recover(
            0,
            Some(Instant::now() - Duration::from_secs(120))
        ));
    }

    #[test]
    fn test_should_auto_recover_high_failures_no_time() {
        assert!(!should_auto_recover(10, None));
    }

    #[test]
    fn test_should_auto_recover_high_failures_recent() {
        assert!(!should_auto_recover(
            10,
            Some(Instant::now() - Duration::from_secs(30))
        ));
    }

    #[test]
    fn test_should_auto_recover_high_failures_old() {
        assert!(should_auto_recover(
            6,
            Some(Instant::now() - Duration::from_secs(61))
        ));
        assert!(should_auto_recover(
            100,
            Some(Instant::now() - Duration::from_secs(300))
        ));
    }

    // ========================================================================
    // should_attempt_recovery
    // ========================================================================

    #[test]
    fn test_should_attempt_recovery_never() {
        assert!(should_attempt_recovery(None, Duration::from_secs(30)));
    }

    #[test]
    fn test_should_attempt_recovery_within_cooldown() {
        let recent = Instant::now() - Duration::from_secs(10);
        assert!(!should_attempt_recovery(
            Some(&recent),
            Duration::from_secs(30)
        ));
    }

    #[test]
    fn test_should_attempt_recovery_after_cooldown() {
        let old = Instant::now() - Duration::from_secs(60);
        assert!(should_attempt_recovery(Some(&old), Duration::from_secs(30)));
    }

    // ========================================================================
    // classify_control_message
    // ========================================================================

    #[test]
    fn test_classify_control_recover_matching() {
        let msg = SinkControlMessage::RecoverSink("file".to_string());
        assert!(matches!(
            classify_control_message(&msg, "file"),
            ControlAction::Recover
        ));
    }

    #[test]
    fn test_classify_control_recover_non_matching() {
        let msg = SinkControlMessage::RecoverSink("database".to_string());
        assert!(matches!(
            classify_control_message(&msg, "file"),
            ControlAction::Ignore
        ));
    }

    // ========================================================================
    // update_adaptive_capacity
    // ========================================================================

    #[test]
    fn test_update_adaptive_capacity_expand() {
        let mut low_usage_since: Option<Instant> = None;
        // 80% usage → should expand
        let new_cap = update_adaptive_capacity(
            100,
            80,
            50,
            200,
            70,
            30,
            Duration::from_secs(60),
            &mut low_usage_since,
        );
        assert_eq!(new_cap, 150); // 100 + 100/2
        assert!(low_usage_since.is_none());
    }

    #[test]
    fn test_update_adaptive_capacity_shrink_after_wait() {
        let mut low_usage_since = Some(Instant::now() - Duration::from_secs(120));
        // 10% usage, low for 120s > 60s wait → should shrink
        let new_cap = update_adaptive_capacity(
            100,
            10,
            50,
            200,
            70,
            30,
            Duration::from_secs(60),
            &mut low_usage_since,
        );
        assert_eq!(new_cap, 70); // 100 * 70 / 100
        assert!(low_usage_since.is_none());
    }

    #[test]
    fn test_update_adaptive_capacity_shrink_starts_timer() {
        let mut low_usage_since: Option<Instant> = None;
        // 10% usage, first time → start timer, keep capacity
        let new_cap = update_adaptive_capacity(
            100,
            10,
            50,
            200,
            70,
            30,
            Duration::from_secs(60),
            &mut low_usage_since,
        );
        assert_eq!(new_cap, 100);
        assert!(low_usage_since.is_some());
    }

    #[test]
    fn test_update_adaptive_capacity_stable() {
        let mut low_usage_since: Option<Instant> = None;
        // 50% usage, between thresholds → no change
        let new_cap = update_adaptive_capacity(
            100,
            50,
            50,
            200,
            70,
            30,
            Duration::from_secs(60),
            &mut low_usage_since,
        );
        assert_eq!(new_cap, 100);
    }

    #[test]
    fn test_update_adaptive_capacity_respects_max() {
        let mut low_usage_since: Option<Instant> = None;
        // 90% usage but already at max → stay at max
        let new_cap = update_adaptive_capacity(
            200,
            180,
            50,
            200,
            70,
            30,
            Duration::from_secs(60),
            &mut low_usage_since,
        );
        assert_eq!(new_cap, 200);
    }

    #[test]
    fn test_update_adaptive_capacity_respects_min() {
        let mut low_usage_since = Some(Instant::now() - Duration::from_secs(120));
        // 0% usage, at min → stay at min
        let new_cap = update_adaptive_capacity(
            50,
            0,
            50,
            200,
            70,
            30,
            Duration::from_secs(60),
            &mut low_usage_since,
        );
        assert_eq!(new_cap, 50);
    }

    // ========================================================================
    // Console worker 并发回归测试:多任务并发写 console sink 不死锁/不 panic,
    // 所有 worker 在 shutdown 广播后均能终止
    // ========================================================================

    #[test]
    fn test_console_worker_concurrent_writes_terminate_without_deadlock() {
        let runtime = tokio::runtime::Builder::new_multi_thread()
            .worker_threads(2)
            .enable_all()
            .build()
            .expect("Failed to build test runtime");

        let config = InklogConfig::default();
        let (file_tx, file_rx) = bounded::<Arc<LogRecord>>(100);
        let (console_tx, console_rx) = bounded::<Arc<LogRecord>>(2048);
        let (control_tx, control_rx) = bounded(10);
        let metrics = Arc::new(Metrics::new());
        let effective_capacity = Arc::new(AtomicUsize::new(2048));
        let console_sink = Arc::new(crate::support::io::ConsoleSink::new(
            config.console_sink.clone().unwrap_or_default(),
            crate::LogTemplate::new(&config.global.format),
        )) as Arc<dyn LogSink>;
        let error_sink: Arc<Mutex<Option<Arc<dyn LogSink>>>> = Arc::new(Mutex::new(None));

        let params = WorkerParams {
            config,
            receiver: file_rx,
            console_receiver: console_rx,
            control_rx,
            control_tx,
            metrics: metrics.clone(),
            console_sink,
            error_sink,
            effective_capacity,
            file_sink_factory: Box::new(|| {
                Err(InklogError::ConfigError("unused in test".to_string()))
            }),
            #[cfg(any(
                feature = "sqlite",
                feature = "postgres",
                feature = "mysql",
                feature = "duckdb"
            ))]
            db_sink_factory: Box::new(|_db, _metrics| {
                Err(InklogError::ConfigError("unused in test".to_string()))
            }),
            #[cfg(any(
                feature = "sqlite",
                feature = "postgres",
                feature = "mysql",
                feature = "duckdb"
            ))]
            database: None,
            #[cfg(any(
                feature = "sqlite",
                feature = "postgres",
                feature = "mysql",
                feature = "duckdb"
            ))]
            db_receiver: None,
            custom_sinks: Vec::new(),
        };

        let (handles, shutdown_txs) = runtime
            .block_on(async { LoggerManager::start_workers(params).expect("start workers") });
        let _ = file_tx;

        // 多任务并发向 console channel 投递记录
        let producers: Vec<_> = (0..4)
            .map(|t| {
                let tx = console_tx.clone();
                thread::spawn(move || {
                    for i in 0..100 {
                        let record = Arc::new(LogRecord {
                            timestamp: Utc::now(),
                            level: "INFO".to_string(),
                            target: format!("concurrent::{t}"),
                            message: format!("concurrent write {t}-{i}"),
                            fields: Default::default(),
                            file: None,
                            line: None,
                            thread_id: "test".to_string(),
                            trace_id: None,
                            span_id: None,
                        });
                        if tx.send(record).is_err() {
                            break;
                        }
                    }
                })
            })
            .collect();
        for producer in producers {
            producer.join().expect("producer thread panicked");
        }
        drop(console_tx);

        // 广播 shutdown 并等待所有 worker 终止;限时未结束即视为死锁回归
        for tx in &shutdown_txs {
            let _ = tx.send_timeout((), Duration::from_secs(2));
        }
        let deadline = Instant::now() + Duration::from_secs(15);
        let mut all_finished = true;
        for handle in handles {
            while !handle.is_finished() {
                if Instant::now() > deadline {
                    all_finished = false;
                    break;
                }
                thread::sleep(Duration::from_millis(10));
            }
            handle.abort();
        }
        assert!(all_finished, "workers must terminate without deadlock");
        assert_eq!(metrics.sink_errors(), 0, "console writes must not fail");
    }

    // ========================================================================
    // 缺陷回归:file sink 工厂启动失败时 worker 不得空转/静默丢弃——
    // 降级期间的记录必须计为 failed、worker 保持存活持续重试,
    // 且 shutdown 语义不变。
    // ========================================================================

    #[test]
    #[serial_test::serial]
    fn test_file_worker_failing_factory_counts_failed_and_exits_on_sender_drop() {
        // 注入毫秒级退避,避免测试等待真实的秒级退避
        FACTORY_RETRY_INITIAL_BACKOFF_MS.store(20, Ordering::Relaxed);

        let runtime = tokio::runtime::Builder::new_multi_thread()
            .worker_threads(2)
            .enable_all()
            .build()
            .expect("Failed to build test runtime");

        let config = InklogConfig {
            file_sink: Some(crate::FileSinkConfig {
                enabled: true,
                ..Default::default()
            }),
            ..Default::default()
        };
        let (file_tx, file_rx) = bounded::<Arc<LogRecord>>(100);
        let (_console_tx, console_rx) = bounded::<Arc<LogRecord>>(100);
        let (control_tx, control_rx) = bounded(10);
        let metrics = Arc::new(Metrics::new());
        let effective_capacity = Arc::new(AtomicUsize::new(100));
        let console_sink = Arc::new(crate::support::io::ConsoleSink::new(
            config.console_sink.clone().unwrap_or_default(),
            crate::LogTemplate::new(&config.global.format),
        )) as Arc<dyn LogSink>;
        let error_sink: Arc<Mutex<Option<Arc<dyn LogSink>>>> = Arc::new(Mutex::new(None));

        let params = WorkerParams {
            config,
            receiver: file_rx,
            console_receiver: console_rx,
            control_rx,
            control_tx,
            metrics: metrics.clone(),
            console_sink,
            error_sink,
            effective_capacity,
            // 必然失败的工厂:模拟 FileSink::new 因路径/权限等原因持续失败
            file_sink_factory: Box::new(|| {
                Err(InklogError::ConfigError(
                    "factory always fails in this test".to_string(),
                ))
            }),
            #[cfg(any(
                feature = "sqlite",
                feature = "postgres",
                feature = "mysql",
                feature = "duckdb"
            ))]
            db_sink_factory: Box::new(|_db, _metrics| {
                Err(InklogError::ConfigError("unused in test".to_string()))
            }),
            #[cfg(any(
                feature = "sqlite",
                feature = "postgres",
                feature = "mysql",
                feature = "duckdb"
            ))]
            database: None,
            #[cfg(any(
                feature = "sqlite",
                feature = "postgres",
                feature = "mysql",
                feature = "duckdb"
            ))]
            db_receiver: None,
            custom_sinks: Vec::new(),
        };

        let (handles, shutdown_txs) = runtime
            .block_on(async { LoggerManager::start_workers(params).expect("start workers") });

        // 向 file channel 投递 N 条记录:降级模式下应被计为 failed 而非无声丢弃
        const N: u64 = 5;
        for i in 0..N {
            let record = Arc::new(LogRecord {
                timestamp: Utc::now(),
                level: "INFO".to_string(),
                target: "degraded::factory".to_string(),
                message: format!("degraded record {i}"),
                fields: Default::default(),
                file: None,
                line: None,
                thread_id: "test".to_string(),
                trace_id: None,
                span_id: None,
            });
            file_tx.send(record).expect("send record");
        }
        drop(file_tx);

        // 等待 worker 消费并计数(退避 20ms,循环 tick 100ms)
        let deadline = Instant::now() + Duration::from_secs(5);
        while metrics.sink_errors() < N && Instant::now() < deadline {
            thread::sleep(Duration::from_millis(20));
        }

        // 1) N 条记录全部被计为 failed/dropped,而非静默丢失
        assert!(
            metrics.sink_errors() >= N,
            "records arriving while the factory fails must be counted as failed, got: {}",
            metrics.sink_errors()
        );
        assert!(
            metrics.logs_dropped() >= N,
            "records arriving while the factory fails must be counted as dropped, got: {}",
            metrics.logs_dropped()
        );
        // 2) 记录发送端全部丢弃且通道排空后,worker 应在有限时间内退出
        //    (即使工厂仍在失败)——而非空转驻留;tokio Runtime drop 会等待
        //    blocking 任务,空转驻留会让整个测试进程永久挂死
        let deadline = Instant::now() + Duration::from_secs(5);
        while !handles[1].is_finished() && Instant::now() < deadline {
            thread::sleep(Duration::from_millis(10));
        }
        assert!(
            handles[1].is_finished(),
            "file worker must exit after all record senders are dropped, even with a failing factory"
        );

        // 3) shutdown 语义不变:降级模式下广播后所有 worker 均终止
        for tx in &shutdown_txs {
            let _ = tx.send_timeout((), Duration::from_secs(2));
        }
        let deadline = Instant::now() + Duration::from_secs(15);
        let mut all_finished = true;
        for handle in handles {
            while !handle.is_finished() {
                if Instant::now() > deadline {
                    all_finished = false;
                    break;
                }
                thread::sleep(Duration::from_millis(10));
            }
            handle.abort();
        }
        assert!(
            all_finished,
            "workers must terminate after shutdown even in degraded retry mode"
        );

        // 恢复测试钩子,避免影响其他测试
        FACTORY_RETRY_INITIAL_BACKOFF_MS.store(0, Ordering::Relaxed);
    }

    // ========================================================================
    // 漂移修复回归:db worker 主循环写失败分支必须与 drain 路径一致地写
    // error.log("Database sink error: ..."),重试耗尽后计 sink_error。
    // ========================================================================

    /// 捕获写入内容的 error sink(验证 error.log 记录)。
    /// 仅 db worker 测试使用,随其 cfg 门控。
    #[cfg(any(
        feature = "sqlite",
        feature = "postgres",
        feature = "mysql",
        feature = "duckdb"
    ))]
    struct CapturingSink {
        messages: Mutex<Vec<String>>,
    }

    #[cfg(any(
        feature = "sqlite",
        feature = "postgres",
        feature = "mysql",
        feature = "duckdb"
    ))]
    #[async_trait::async_trait]
    impl LogSink for CapturingSink {
        async fn write(&self, record: &LogRecord) -> Result<(), InklogError> {
            self.messages.lock().unwrap().push(record.message.clone());
            Ok(())
        }

        async fn flush(&self) -> Result<(), InklogError> {
            Ok(())
        }

        async fn shutdown(&self) -> Result<(), InklogError> {
            Ok(())
        }
    }

    /// 写入必然失败的 db sink(模拟运行期写库失败)。
    #[cfg(any(
        feature = "sqlite",
        feature = "postgres",
        feature = "mysql",
        feature = "duckdb"
    ))]
    struct FailingDbSink;

    #[cfg(any(
        feature = "sqlite",
        feature = "postgres",
        feature = "mysql",
        feature = "duckdb"
    ))]
    #[async_trait::async_trait]
    impl LogSink for FailingDbSink {
        async fn write(&self, _record: &LogRecord) -> Result<(), InklogError> {
            Err(InklogError::DatabaseError {
                message: "mock db write failure".to_string(),
                source: None,
            })
        }

        async fn flush(&self) -> Result<(), InklogError> {
            Ok(())
        }

        async fn shutdown(&self) -> Result<(), InklogError> {
            Ok(())
        }
    }

    #[cfg(any(
        feature = "sqlite",
        feature = "postgres",
        feature = "mysql",
        feature = "duckdb"
    ))]
    #[test]
    fn test_db_worker_write_failure_writes_error_log_in_main_loop() {
        let runtime = tokio::runtime::Builder::new_multi_thread()
            .worker_threads(2)
            .enable_all()
            .build()
            .expect("Failed to build test runtime");

        let mut config = InklogConfig::default();
        // db worker 主体受 db_config.enabled 守卫,必须显式启用才会进入主循环
        config.database_sink = Some(crate::domain::config::DatabaseSinkConfig {
            enabled: true,
            ..Default::default()
        });
        let (_file_tx, file_rx) = bounded::<Arc<LogRecord>>(100);
        let (_console_tx, console_rx) = bounded::<Arc<LogRecord>>(100);
        let (db_tx, db_rx) = bounded::<Arc<LogRecord>>(100);
        let (control_tx, control_rx) = bounded(10);
        let metrics = Arc::new(Metrics::new());
        let effective_capacity = Arc::new(AtomicUsize::new(100));
        let console_sink = Arc::new(crate::support::io::ConsoleSink::new(
            config.console_sink.clone().unwrap_or_default(),
            crate::LogTemplate::new(&config.global.format),
        )) as Arc<dyn LogSink>;
        let captured = Arc::new(CapturingSink {
            messages: Mutex::new(Vec::new()),
        });
        let error_sink: Arc<Mutex<Option<Arc<dyn LogSink>>>> =
            Arc::new(Mutex::new(Some(captured.clone() as Arc<dyn LogSink>)));

        let params = WorkerParams {
            config,
            receiver: file_rx,
            console_receiver: console_rx,
            control_rx,
            control_tx,
            metrics: metrics.clone(),
            console_sink,
            error_sink,
            effective_capacity,
            file_sink_factory: Box::new(|| {
                Err(InklogError::ConfigError("unused in test".to_string()))
            }),
            db_sink_factory: Box::new(|_db, _metrics| {
                Ok(Box::new(FailingDbSink) as Box<dyn LogSink>)
            }),
            database: Some(Arc::new(crate::integrations::MockDatabaseAdapter::new())
                as Arc<dyn crate::integrations::Database>),
            db_receiver: Some(db_rx),
            custom_sinks: Vec::new(),
        };

        let (handles, shutdown_txs) = runtime
            .block_on(async { LoggerManager::start_workers(params).expect("start workers") });

        // 向 db channel 投递 N 条记录:主循环写失败应计 sink_error 并写 error.log
        const N: u64 = 3;
        for i in 0..N {
            let record = Arc::new(LogRecord {
                timestamp: Utc::now(),
                level: "INFO".to_string(),
                target: "db::write_failure".to_string(),
                message: format!("db write failure record {i}"),
                fields: Default::default(),
                file: None,
                line: None,
                thread_id: "test".to_string(),
                trace_id: None,
                span_id: None,
            });
            db_tx.send(record).expect("send db record");
        }
        drop(db_tx);

        // 等待 worker 完成 3 次重试并计数(退避 10ms+20ms/条)
        let deadline = Instant::now() + Duration::from_secs(5);
        while metrics.sink_errors() < N && Instant::now() < deadline {
            thread::sleep(Duration::from_millis(20));
        }

        // 1) 重试耗尽后计 sink_error(含 db 主循环,与 file worker 一致)
        assert!(
            metrics.sink_errors() >= N,
            "db main-loop write failures must be counted as sink errors, got: {}",
            metrics.sink_errors()
        );
        // 2) error.log 与 drain 路径一致:主循环失败分支写入 "Database sink error: ..."
        //    (每次失败尝试各写一条,N 条记录 × 3 次尝试)
        let error_messages = captured.messages.lock().unwrap();
        assert!(
            error_messages.len() >= N as usize,
            "db main-loop failures must write error.log, got: {}",
            error_messages.len()
        );
        assert!(
            error_messages
                .iter()
                .all(|m| m.starts_with("Database sink error: ")),
            "error.log records must come from the database sink failure path, got: {:?}",
            *error_messages
        );
        drop(error_messages);

        // 3) shutdown 语义不变
        for tx in &shutdown_txs {
            let _ = tx.send_timeout((), Duration::from_secs(2));
        }
        let deadline = Instant::now() + Duration::from_secs(15);
        let mut all_finished = true;
        for handle in handles {
            while !handle.is_finished() {
                if Instant::now() > deadline {
                    all_finished = false;
                    break;
                }
                thread::sleep(Duration::from_millis(10));
            }
            handle.abort();
        }
        assert!(all_finished, "workers must terminate after shutdown");
    }
}

// =========================================================================
// 通用 SinkWorker(动态注册 sink)单元测试
// =========================================================================

#[cfg(test)]
mod custom_sink_worker_tests {
    use super::*;
    use parking_lot::Mutex;

    /// 捕获写入内容的内存 sink(第三方 sink 的测试替身)。
    struct MemorySink {
        records: Mutex<Vec<String>>,
        fail_times: AtomicUsize,
    }

    impl MemorySink {
        fn new(fail_times: usize) -> Self {
            Self {
                records: Mutex::new(Vec::new()),
                fail_times: AtomicUsize::new(fail_times),
            }
        }
    }

    #[async_trait::async_trait]
    impl LogSink for MemorySink {
        async fn write(&self, record: &LogRecord) -> Result<(), InklogError> {
            if self.fail_times.load(Ordering::SeqCst) > 0 {
                self.fail_times.fetch_sub(1, Ordering::SeqCst);
                return Err(InklogError::ConfigError("transient failure".to_string()));
            }
            self.records.lock().push(record.message.clone());
            Ok(())
        }

        async fn flush(&self) -> Result<(), InklogError> {
            Ok(())
        }

        async fn shutdown(&self) -> Result<(), InklogError> {
            Ok(())
        }
    }

    #[test]
    fn test_custom_sink_worker_consumes_records_and_drains_on_shutdown() {
        let runtime = tokio::runtime::Builder::new_multi_thread()
            .worker_threads(2)
            .enable_all()
            .build()
            .unwrap();

        let sink = Arc::new(MemorySink::new(0));
        let (tx, rx) = bounded::<Arc<LogRecord>>(100);
        let entry = CustomSinkEntry {
            name: "custom-0".to_string(),
            sink: sink.clone() as Arc<dyn LogSink>,
            receiver: rx,
        };
        let (_shutdown_tx, shutdown_rx) = bounded(1);
        let metrics = Arc::new(Metrics::new());
        let console_sink = Arc::new(crate::support::io::ConsoleSink::new(
            Default::default(),
            crate::LogTemplate::new("{timestamp} [{level}] {target} - {message}"),
        )) as Arc<dyn LogSink>;

        let handle = {
            let metrics = metrics.clone();
            let console = console_sink.clone();
            let runtime_handle = runtime.handle().clone();
            thread::spawn(move || {
                run_custom_sink_worker(
                    &runtime_handle,
                    &metrics,
                    &console,
                    &entry,
                    &entry.receiver,
                    &shutdown_rx,
                );
            })
        };

        // 投递 3 条记录
        for i in 0..3 {
            tx.send(Arc::new(LogRecord {
                timestamp: Utc::now(),
                level: "INFO".to_string(),
                target: "custom::sink".to_string(),
                message: format!("custom record {i}"),
                fields: Default::default(),
                file: None,
                line: None,
                thread_id: "test".to_string(),
                trace_id: None,
                span_id: None,
            }))
            .unwrap();
        }

        // 等待消费
        let deadline = Instant::now() + Duration::from_secs(5);
        while sink.records.lock().len() < 3 && Instant::now() < deadline {
            thread::sleep(Duration::from_millis(10));
        }
        assert_eq!(sink.records.lock().len(), 3, "all records must be written");
        assert!(metrics.logs_written() >= 3, "writes must be counted");

        // 记录发送端全部丢弃且通道排空 → worker 退出(Disconnected 路径)
        drop(tx);
        drop(_shutdown_tx);
        handle.join().expect("worker must exit cleanly");
    }

    #[test]
    fn test_custom_sink_worker_retries_and_falls_back_to_console() {
        let runtime = tokio::runtime::Builder::new_multi_thread()
            .worker_threads(2)
            .enable_all()
            .build()
            .unwrap();

        // 前 3 次写失败(单条记录的 3 次重试全部耗尽)→ 计 sink_error 并降级 console
        let sink = Arc::new(MemorySink::new(3));
        let (tx, rx) = bounded::<Arc<LogRecord>>(10);
        let entry = CustomSinkEntry {
            name: "custom-fail".to_string(),
            sink: sink.clone() as Arc<dyn LogSink>,
            receiver: rx,
        };
        let (_shutdown_tx, shutdown_rx) = bounded::<()>(1);
        let metrics = Arc::new(Metrics::new());
        let console_sink = Arc::new(crate::support::io::ConsoleSink::new(
            Default::default(),
            crate::LogTemplate::new("t"),
        )) as Arc<dyn LogSink>;

        {
            let metrics = metrics.clone();
            let console = console_sink.clone();
            let runtime_handle = runtime.handle().clone();
            thread::spawn(move || {
                run_custom_sink_worker(
                    &runtime_handle,
                    &metrics,
                    &console,
                    &entry,
                    &entry.receiver,
                    &shutdown_rx,
                );
            });
        }

        tx.send(Arc::new(LogRecord {
            timestamp: Utc::now(),
            level: "INFO".to_string(),
            target: "custom::fail".to_string(),
            message: "doomed record".to_string(),
            fields: Default::default(),
            file: None,
            line: None,
            thread_id: "test".to_string(),
            trace_id: None,
            span_id: None,
        }))
        .unwrap();

        // 等待重试耗尽(10ms + 20ms 间隔)
        let deadline = Instant::now() + Duration::from_secs(5);
        while metrics.sink_errors() < 1 && Instant::now() < deadline {
            thread::sleep(Duration::from_millis(10));
        }
        assert!(
            metrics.sink_errors() >= 1,
            "exhausted retries must count a sink error"
        );
        assert_eq!(
            sink.records.lock().len(),
            0,
            "record must not be written after all attempts failed"
        );

        drop(tx);
    }
}