nntp-proxy 0.5.1

NNTP proxy server with per-command backend multiplexing, caching, metrics, and TUI dashboard
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
//! Tokio runtime configuration and common utilities for binary targets
//!
//! This module provides:
//! - Testable runtime configuration and builder logic
//! - Shared utilities used across multiple binary targets to reduce duplication
//! - Shutdown signal handling

use crate::types::ThreadCount;
use anyhow::{Context, Result};

const TOKIO_WORKER_THREAD_STACK_SIZE: usize = 8 * 1024 * 1024;

/// Runtime configuration
#[derive(Debug, Clone)]
pub struct RuntimeConfig {
    /// Number of worker threads
    worker_threads: usize,
    /// Whether to enable CPU pinning (Linux only)
    enable_cpu_pinning: bool,
}

impl RuntimeConfig {
    /// Create runtime config from optional thread count
    ///
    /// If `threads` is None, defaults to 1 thread.
    /// If `threads` is Some(ThreadCount(0)), uses number of CPU cores.
    /// Single-threaded runtime is used if threads == 1.
    #[must_use]
    pub fn from_args(threads: Option<ThreadCount>) -> Self {
        let worker_threads = threads.map_or(1, |t| t.get());

        Self {
            worker_threads,
            enable_cpu_pinning: true,
        }
    }

    /// Disable CPU pinning
    #[must_use]
    pub const fn without_cpu_pinning(mut self) -> Self {
        self.enable_cpu_pinning = false;
        self
    }

    /// Get number of worker threads
    #[must_use]
    pub const fn worker_threads(&self) -> usize {
        self.worker_threads
    }

    /// Check if single-threaded
    #[must_use]
    pub const fn is_single_threaded(&self) -> bool {
        self.worker_threads == 1
    }

    /// Build the tokio runtime
    ///
    /// Creates either a current-thread or multi-threaded runtime based on
    /// the configured worker thread count. Applies CPU pinning if enabled.
    ///
    /// # Errors
    /// Returns error if runtime creation fails or CPU pinning fails
    pub fn build_runtime(self) -> Result<tokio::runtime::Runtime> {
        let rt = if self.is_single_threaded() {
            tracing::info!("Starting NNTP proxy with single-threaded runtime");
            tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()?
        } else {
            let num_cpus = std::thread::available_parallelism().map_or(1, std::num::NonZero::get);
            tracing::info!(
                "Starting NNTP proxy with {} worker threads (detected {} CPUs)",
                self.worker_threads,
                num_cpus
            );
            tokio::runtime::Builder::new_multi_thread()
                .worker_threads(self.worker_threads)
                .thread_stack_size(TOKIO_WORKER_THREAD_STACK_SIZE)
                .enable_all()
                .build()?
        };

        if self.enable_cpu_pinning {
            pin_to_cpu_cores(self.worker_threads);
        }

        Ok(rt)
    }
}

impl Default for RuntimeConfig {
    fn default() -> Self {
        Self::from_args(None)
    }
}

/// Pin current process to specific CPU cores for optimal performance
///
/// This is a best-effort operation - failures are logged but not fatal.
///
/// # Arguments
/// * `num_cores` - Number of CPU cores to pin to (`0..num_cores`)
#[cfg(target_os = "linux")]
fn pin_to_cpu_cores(num_cores: usize) {
    use nix::sched::{CpuSet, sched_setaffinity};
    use nix::unistd::Pid;

    let mut cpu_set = CpuSet::new();
    for core in 0..num_cores {
        let _ = cpu_set.set(core);
    }

    match sched_setaffinity(Pid::from_raw(0), &cpu_set) {
        Ok(()) => {
            tracing::info!(
                "Successfully pinned process to {} CPU cores for optimal performance",
                num_cores
            );
        }
        Err(e) => {
            tracing::warn!(
                "Failed to set CPU affinity: {}, continuing without pinning",
                e
            );
        }
    }
}

#[cfg(not(target_os = "linux"))]
fn pin_to_cpu_cores(_num_cores: usize) {
    tracing::info!("CPU pinning not available on this platform");
}

/// Wait for shutdown signal (Ctrl+C or SIGTERM on Unix)
///
/// This is a common utility for all binary targets. Handler installation
/// failures are logged and treated as an immediate shutdown signal.
pub async fn shutdown_signal() {
    use tracing::warn;

    let ctrl_c = async {
        if let Err(error) = tokio::signal::ctrl_c().await {
            warn!("Failed to install Ctrl+C handler: {error}");
        }
    };

    #[cfg(unix)]
    let terminate = async {
        match tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) {
            Ok(mut signal) => {
                signal.recv().await;
            }
            Err(error) => warn!("Failed to install SIGTERM handler: {error}"),
        }
    };

    #[cfg(not(unix))]
    let terminate = std::future::pending::<()>();

    tokio::select! {
        () = ctrl_c => {},
        () = terminate => {},
    }
}

// ============================================================================
// Binary Utilities - Shared code for the unified nntp-proxy entrypoint.
// ============================================================================

/// Load configuration and log server information
///
/// Common pattern across all binary targets - load config and display backends.
///
/// # Errors
/// Returns error if configuration loading fails
pub fn load_and_log_config(
    config_path: &str,
) -> Result<(crate::config::Config, crate::config::ConfigSource)> {
    use crate::load_config_with_fallback;
    use tracing::info;

    let (config, source) = load_config_with_fallback(config_path)?;

    info!("Loaded configuration from {}", source.description());
    info!("Loaded {} backend servers:", config.servers.len());
    for server in &config.servers {
        info!("  - {} ({}:{})", server.name, server.host, server.port);
    }

    Ok((config, source))
}

/// Extract listen address from CLI args or config
///
/// Prefers CLI args over config values.
#[must_use]
pub fn resolve_listen_address(
    host_arg: Option<&str>,
    port_arg: Option<crate::types::Port>,
    config: &crate::config::Config,
) -> (String, crate::types::Port) {
    let host = host_arg.map_or_else(|| config.proxy.host.clone(), String::from);
    let port = port_arg.unwrap_or(config.proxy.port);
    (host, port)
}

/// Bind TCP listener and log startup information
///
/// # Errors
/// Returns error if binding fails
pub async fn bind_listener(
    host: &str,
    port: crate::types::Port,
    routing_mode: crate::RoutingMode,
) -> Result<tokio::net::TcpListener> {
    use tracing::info;

    let listen_addr = format!("{}:{}", host, port.get());
    let listener = tokio::net::TcpListener::bind(&listen_addr)
        .await
        .with_context(|| format!("Failed to bind NNTP proxy listener at {listen_addr}"))?;

    info!("NNTP proxy listening on {} ({})", listen_addr, routing_mode);

    Ok(listener)
}

/// Spawn background task to prewarm connection pools
///
/// Returns immediately without blocking. Logs errors but doesn't fail.
pub fn spawn_connection_prewarming(proxy: &std::sync::Arc<crate::NntpProxy>) {
    use std::sync::Arc;
    use tracing::{info, warn};

    let proxy = Arc::clone(proxy);
    tokio::spawn(async move {
        info!("Prewarming connection pools...");
        if let Err(e) = proxy.prewarm_connections().await {
            warn!("Failed to prewarm connection pools: {}", e);
            return;
        }
        info!("Connection pools ready");
    });
}

/// Spawn background task to periodically flush connection stats
///
/// Flushes every 30 seconds to ensure metrics are up-to-date.
pub fn spawn_stats_flusher(stats: &crate::metrics::ConnectionStatsAggregator) {
    let stats = stats.clone();
    tokio::spawn(async move {
        let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(30));
        interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);

        loop {
            interval.tick().await;
            stats.flush();
        }
    });
}

/// Spawn background task to periodically log cache statistics
///
/// Only spawns if cache is enabled AND debug logging is enabled.
/// Logs every 60 seconds.
pub fn spawn_cache_stats_logger(proxy: &std::sync::Arc<crate::NntpProxy>) {
    use std::sync::Arc;
    use tracing::debug;

    // Only spawn if debug logging is enabled
    if !tracing::enabled!(tracing::Level::DEBUG) {
        return;
    }

    // Cache is always present now
    let cache = Arc::clone(proxy.cache());
    tokio::spawn(async move {
        let mut interval =
            tokio::time::interval(crate::constants::duration_polyfill::from_minutes(1));
        interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);

        loop {
            interval.tick().await;
            let entries = cache.entry_count();
            let size_bytes = cache.weighted_size();
            let hit_rate = cache.hit_rate();
            debug!(
                "Cache stats: entries={}, size={} ({:.1}% hit rate)",
                entries,
                crate::formatting::format_bytes(size_bytes),
                hit_rate
            );
        }
    });
}

fn response_write_metrics_interval() -> Option<std::time::Duration> {
    let raw = std::env::var_os("NNTP_PROXY_RESPONSE_WRITE_METRICS_SECS")?;
    let raw = raw.to_string_lossy();
    let secs = raw.parse::<u64>().ok()?;
    (secs > 0).then(|| std::time::Duration::from_secs(secs))
}

fn client_writer_lock_metrics_interval() -> Option<std::time::Duration> {
    let raw = std::env::var_os("NNTP_PROXY_CLIENT_WRITER_LOCK_METRICS_SECS")?;
    let raw = raw.to_string_lossy();
    let secs = raw.parse::<u64>().ok()?;
    (secs > 0).then(|| std::time::Duration::from_secs(secs))
}

/// Spawn background task to periodically log `ChunkedResponse::write_all_to` activity.
///
/// Enable this only for targeted profiling sessions:
/// `NNTP_PROXY_RESPONSE_WRITE_METRICS_SECS=<seconds>`.
pub fn spawn_response_write_metrics_logger() {
    use tracing::info;

    let Some(period) = response_write_metrics_interval() else {
        return;
    };

    let mut previous = crate::pool::buffer::response_write_metrics_snapshot();

    tokio::spawn(async move {
        let mut interval = tokio::time::interval(period);
        interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);

        loop {
            interval.tick().await;
            let current = crate::pool::buffer::response_write_metrics_snapshot();
            let responses_delta = current.responses.saturating_sub(previous.responses);
            let chunks_delta = current
                .chunks_written
                .saturating_sub(previous.chunks_written);
            let bytes_delta = current.bytes_written.saturating_sub(previous.bytes_written);
            let tiny_chunks_delta = current.tiny_chunks.saturating_sub(previous.tiny_chunks);
            let tiny_chunk_bytes_delta = current
                .tiny_chunk_bytes
                .saturating_sub(previous.tiny_chunk_bytes);
            let small_chunks_delta = current.small_chunks.saturating_sub(previous.small_chunks);
            let small_chunk_bytes_delta = current
                .small_chunk_bytes
                .saturating_sub(previous.small_chunk_bytes);
            let avg_chunks_milli = chunks_delta
                .saturating_mul(1000)
                .checked_div(responses_delta)
                .unwrap_or(0);

            info!(
                responses_delta = responses_delta,
                single_chunk_delta = current
                    .single_chunk_responses
                    .saturating_sub(previous.single_chunk_responses),
                multi_chunk_delta = current
                    .multi_chunk_responses
                    .saturating_sub(previous.multi_chunk_responses),
                chunks_delta = chunks_delta,
                bytes_delta = bytes_delta,
                tiny_chunks_delta = tiny_chunks_delta,
                tiny_chunk_bytes_delta = tiny_chunk_bytes_delta,
                small_chunks_delta = small_chunks_delta,
                small_chunk_bytes_delta = small_chunk_bytes_delta,
                avg_chunks_milli = avg_chunks_milli,
                max_chunks_seen = current.max_chunks_per_response,
                "Response write metrics"
            );

            previous = current;
        }
    });
}

/// Spawn background task to periodically log client-writer mutex contention.
///
/// Enable this only for targeted profiling sessions:
/// `NNTP_PROXY_CLIENT_WRITER_LOCK_METRICS_SECS=<seconds>`.
pub fn spawn_client_writer_lock_metrics_logger() {
    use tracing::info;

    let Some(period) = client_writer_lock_metrics_interval() else {
        return;
    };

    let mut previous = crate::session::shared_client_writer::client_writer_lock_metrics_snapshot();

    tokio::spawn(async move {
        let mut interval = tokio::time::interval(period);
        interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);

        loop {
            interval.tick().await;
            let current =
                crate::session::shared_client_writer::client_writer_lock_metrics_snapshot();
            let requests_delta = current.lock_requests.saturating_sub(previous.lock_requests);
            let wait_nanos_delta = current.wait_nanos.saturating_sub(previous.wait_nanos);
            let avg_wait_nanos = if current
                .contended_locks
                .saturating_sub(previous.contended_locks)
                == 0
            {
                0
            } else {
                wait_nanos_delta
                    / (current
                        .contended_locks
                        .saturating_sub(previous.contended_locks) as u64)
            };

            info!(
                requests_delta = requests_delta,
                immediate_delta = current
                    .immediate_locks
                    .saturating_sub(previous.immediate_locks),
                contended_delta = current
                    .contended_locks
                    .saturating_sub(previous.contended_locks),
                wait_nanos_delta = wait_nanos_delta,
                avg_wait_nanos = avg_wait_nanos,
                max_wait_nanos_seen = current.max_wait_nanos,
                "Client writer lock metrics"
            );

            previous = current;
        }
    });
}

#[cfg(tokio_unstable)]
#[derive(Clone, Copy, Debug)]
struct TokioRuntimeMetricsSnapshot {
    alive_tasks: usize,
    workers: usize,
    global_queue_depth: usize,
    blocking_queue_depth: usize,
    remote_schedule_count: u64,
    budget_forced_yield_count: u64,
    worker_local_schedule_count: u64,
    worker_overflow_count: u64,
    worker_steal_count: u64,
    worker_steal_operations: u64,
    worker_poll_count: u64,
    worker_park_count: u64,
    worker_noop_count: u64,
    worker_busy_total_ns: u128,
    worker_busy_min_ns: u128,
    worker_busy_max_ns: u128,
}

#[cfg(tokio_unstable)]
impl TokioRuntimeMetricsSnapshot {
    fn capture(metrics: &tokio::runtime::RuntimeMetrics) -> Self {
        let workers = metrics.num_workers();
        let mut worker_local_schedule_count = 0u64;
        let mut worker_overflow_count = 0u64;
        let mut worker_steal_count = 0u64;
        let mut worker_steal_operations = 0u64;
        let mut worker_poll_count = 0u64;
        let mut worker_park_count = 0u64;
        let mut worker_noop_count = 0u64;
        let mut worker_busy_total_ns = 0u128;
        let mut worker_busy_min_ns = u128::MAX;
        let mut worker_busy_max_ns = 0u128;

        for worker in 0..workers {
            worker_local_schedule_count += metrics.worker_local_schedule_count(worker);
            worker_overflow_count += metrics.worker_overflow_count(worker);
            worker_steal_count += metrics.worker_steal_count(worker);
            worker_steal_operations += metrics.worker_steal_operations(worker);
            worker_poll_count += metrics.worker_poll_count(worker);
            worker_park_count += metrics.worker_park_count(worker);
            worker_noop_count += metrics.worker_noop_count(worker);

            let busy_ns = metrics.worker_total_busy_duration(worker).as_nanos();
            worker_busy_total_ns += busy_ns;
            worker_busy_min_ns = worker_busy_min_ns.min(busy_ns);
            worker_busy_max_ns = worker_busy_max_ns.max(busy_ns);
        }

        Self {
            alive_tasks: metrics.num_alive_tasks(),
            workers,
            global_queue_depth: metrics.global_queue_depth(),
            blocking_queue_depth: metrics.blocking_queue_depth(),
            remote_schedule_count: metrics.remote_schedule_count(),
            budget_forced_yield_count: metrics.budget_forced_yield_count(),
            worker_local_schedule_count,
            worker_overflow_count,
            worker_steal_count,
            worker_steal_operations,
            worker_poll_count,
            worker_park_count,
            worker_noop_count,
            worker_busy_total_ns,
            worker_busy_min_ns: if workers == 0 { 0 } else { worker_busy_min_ns },
            worker_busy_max_ns,
        }
    }
}

#[cfg(tokio_unstable)]
fn tokio_runtime_metrics_interval() -> Option<std::time::Duration> {
    let raw = std::env::var_os("NNTP_PROXY_TOKIO_RUNTIME_METRICS_SECS")?;
    let raw = raw.to_string_lossy();
    let secs = raw.parse::<u64>().ok()?;
    (secs > 0).then(|| std::time::Duration::from_secs(secs))
}

/// Spawn background task to periodically log Tokio runtime scheduler metrics.
///
/// Enable this only for targeted profiling sessions:
/// `NNTP_PROXY_TOKIO_RUNTIME_METRICS_SECS=<seconds>`.
#[cfg(tokio_unstable)]
pub fn spawn_tokio_runtime_metrics_logger() {
    use tracing::info;

    let Some(period) = tokio_runtime_metrics_interval() else {
        return;
    };

    let handle = tokio::runtime::Handle::current();
    let mut previous = TokioRuntimeMetricsSnapshot::capture(&handle.metrics());

    tokio::spawn(async move {
        let mut interval = tokio::time::interval(period);
        interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);

        loop {
            interval.tick().await;
            let current = TokioRuntimeMetricsSnapshot::capture(&handle.metrics());

            let busy_total_delta_ns = current
                .worker_busy_total_ns
                .saturating_sub(previous.worker_busy_total_ns);
            let busy_min_delta_ns = current
                .worker_busy_min_ns
                .saturating_sub(previous.worker_busy_min_ns);
            let busy_max_delta_ns = current
                .worker_busy_max_ns
                .saturating_sub(previous.worker_busy_max_ns);

            info!(
                workers = current.workers,
                alive_tasks = current.alive_tasks,
                global_queue_depth = current.global_queue_depth,
                blocking_queue_depth = current.blocking_queue_depth,
                remote_schedule_delta = current
                    .remote_schedule_count
                    .saturating_sub(previous.remote_schedule_count),
                budget_forced_yield_delta = current
                    .budget_forced_yield_count
                    .saturating_sub(previous.budget_forced_yield_count),
                worker_local_schedule_delta = current
                    .worker_local_schedule_count
                    .saturating_sub(previous.worker_local_schedule_count),
                worker_overflow_delta = current
                    .worker_overflow_count
                    .saturating_sub(previous.worker_overflow_count),
                worker_steal_delta = current
                    .worker_steal_count
                    .saturating_sub(previous.worker_steal_count),
                worker_steal_ops_delta = current
                    .worker_steal_operations
                    .saturating_sub(previous.worker_steal_operations),
                worker_poll_delta = current
                    .worker_poll_count
                    .saturating_sub(previous.worker_poll_count),
                worker_park_delta = current
                    .worker_park_count
                    .saturating_sub(previous.worker_park_count),
                worker_noop_delta = current
                    .worker_noop_count
                    .saturating_sub(previous.worker_noop_count),
                worker_busy_total_ms_delta = busy_total_delta_ns / 1_000_000,
                worker_busy_min_ms_delta = busy_min_delta_ns / 1_000_000,
                worker_busy_max_ms_delta = busy_max_delta_ns / 1_000_000,
                "Tokio runtime metrics"
            );

            previous = current;
        }
    });
}

#[cfg(not(tokio_unstable))]
pub fn spawn_tokio_runtime_metrics_logger() {
    if std::env::var_os("NNTP_PROXY_TOKIO_RUNTIME_METRICS_SECS").is_some() {
        tracing::warn!("NNTP_PROXY_TOKIO_RUNTIME_METRICS_SECS requires a tokio_unstable build");
    }
}

/// Persist runtime state that should survive process restarts.
///
/// Saves metrics unconditionally and availability state when the proxy uses
/// the dedicated availability index.
///
/// # Errors
/// Returns an error if metrics or availability persistence fails, but still
/// attempts both writes so one failure does not suppress the other.
pub async fn persist_runtime_state(
    proxy: &std::sync::Arc<crate::NntpProxy>,
    stats_path: std::path::PathBuf,
    availability_path: Option<std::path::PathBuf>,
    server_names: Vec<String>,
) -> Result<()> {
    let mut first_error = None;

    let metrics = proxy.metrics().clone();
    let metrics_path = stats_path.clone();
    handle_shutdown_metrics_save_result(
        &mut first_error,
        &stats_path,
        tokio::task::spawn_blocking(move || metrics.save_to_disk(&metrics_path, &server_names))
            .await,
    );

    if let Some(path) = availability_path {
        let cache = proxy.cache().clone();
        let save_path = path.clone();
        handle_shutdown_availability_save_result(
            &mut first_error,
            &path,
            tokio::task::spawn_blocking(move || cache.save_to_disk(&save_path)).await,
        );
    }

    if let Some(error) = first_error {
        Err(error)
    } else {
        Ok(())
    }
}

fn remember_first_persistence_error(slot: &mut Option<anyhow::Error>, error: anyhow::Error) {
    if slot.is_none() {
        *slot = Some(error);
    }
}

fn handle_shutdown_metrics_save_result(
    first_error: &mut Option<anyhow::Error>,
    stats_path: &std::path::Path,
    result: Result<Result<()>, tokio::task::JoinError>,
) {
    use tracing::{info, warn};

    match result {
        Ok(Ok(())) => info!("Metrics saved to {}", stats_path.display()),
        Ok(Err(e)) => {
            warn!("Failed to save metrics on shutdown: {}", e);
            remember_first_persistence_error(first_error, e);
        }
        Err(e) => {
            warn!("Failed to join metrics save task on shutdown: {}", e);
            remember_first_persistence_error(first_error, e.into());
        }
    }
}

fn handle_shutdown_availability_save_result(
    first_error: &mut Option<anyhow::Error>,
    availability_path: &std::path::Path,
    result: Result<Result<bool>, tokio::task::JoinError>,
) {
    use tracing::{info, warn};

    match result {
        Ok(Ok(true)) => info!(
            "Availability index saved to {}",
            availability_path.display()
        ),
        Ok(Ok(false)) => {}
        Ok(Err(e)) => {
            warn!("Failed to save availability index on shutdown: {}", e);
            remember_first_persistence_error(first_error, e);
        }
        Err(e) => {
            warn!("Failed to join availability save task on shutdown: {}", e);
            remember_first_persistence_error(first_error, e.into());
        }
    }
}

fn handle_periodic_metrics_save_result(
    stats_path: &std::path::Path,
    result: Result<Result<()>, tokio::task::JoinError>,
) {
    use tracing::warn;

    match result {
        Ok(Ok(())) => {}
        Ok(Err(e)) => warn!("Failed to save metrics to {}: {}", stats_path.display(), e),
        Err(e) => warn!("Failed to save metrics to {}: {}", stats_path.display(), e),
    }
}

fn handle_periodic_availability_save_result(
    availability_path: &std::path::Path,
    result: Result<Result<bool>, tokio::task::JoinError>,
) {
    use tracing::warn;

    match result {
        Ok(Ok(_)) => {}
        Ok(Err(e)) => warn!(
            "Failed to save availability index to {}: {}",
            availability_path.display(),
            e
        ),
        Err(e) => warn!(
            "Failed to save availability index to {}: {}",
            availability_path.display(),
            e
        ),
    }
}

/// Spawn graceful shutdown handler
///
/// Waits for shutdown signal, then:
/// 1. Sends shutdown notification via channel
/// 2. Calls `graceful_shutdown()` on proxy
///
/// Returns the shutdown receiver channel.
#[must_use]
pub fn spawn_shutdown_handler(
    proxy: &std::sync::Arc<crate::NntpProxy>,
    stats_path: std::path::PathBuf,
    availability_path: Option<std::path::PathBuf>,
    server_names: Vec<String>,
) -> tokio::sync::mpsc::Receiver<()> {
    use std::sync::Arc;
    use tracing::{info, warn};

    let (shutdown_tx, shutdown_rx) = tokio::sync::mpsc::channel::<()>(1);
    let proxy = Arc::clone(proxy);

    tokio::spawn(async move {
        shutdown_signal().await;
        info!("Shutdown signal received");

        if let Err(e) =
            persist_runtime_state(&proxy, stats_path, availability_path, server_names).await
        {
            warn!("Failed to persist runtime state on shutdown: {}", e);
        }

        // Notify listeners
        let _ = shutdown_tx.send(()).await;

        // Close idle connections
        proxy.graceful_shutdown().await;
        info!("Graceful shutdown complete");
    });

    shutdown_rx
}

/// Run the main accept loop for client connections
///
/// Accepts connections and spawns a task for each based on routing mode.
/// Exits when shutdown signal is received.
///
/// # Errors
/// Returns error if `listener.accept()` fails
pub async fn run_accept_loop(
    proxy: std::sync::Arc<crate::NntpProxy>,
    listener: tokio::net::TcpListener,
    mut shutdown_rx: tokio::sync::mpsc::Receiver<()>,
    routing_mode: crate::RoutingMode,
) -> Result<()> {
    use tracing::{error, info};

    let uses_per_command = routing_mode.supports_per_command_routing();
    let mut client_tasks = tokio::task::JoinSet::new();

    loop {
        tokio::select! {
            biased;

            _ = shutdown_rx.recv() => {
                info!("Shutdown initiated, stopping accept loop");
                break;
            }

            Some(join_result) = client_tasks.join_next(), if !client_tasks.is_empty() => {
                if let Err(error) = join_result
                    && !error.is_cancelled()
                {
                    error!("Client session task failed: {:?}", error);
                }
            }

            accept_result = listener.accept() => {
                let (stream, addr) = accept_result?;
                let proxy = proxy.clone();

                let client_task = async move {
                    let result = if uses_per_command {
                        proxy.handle_client_per_command_routing(stream, addr.into()).await
                    } else {
                        proxy.handle_client(stream, addr.into()).await
                    };

                    match result {
                        // Normal completion or client disconnect — no action needed
                        Ok(()) | Err(crate::session::SessionError::ClientDisconnect(_)) => {}
                        Err(crate::session::SessionError::Backend(e)) => {
                            error!("Error handling client {}: {:?}", addr, e);
                        }
                    }
                };

                client_tasks.spawn(client_task);
            }
        }
    }

    client_tasks.abort_all();
    while let Some(join_result) = client_tasks.join_next().await {
        if let Err(error) = join_result
            && !error.is_cancelled()
        {
            error!("Client session task failed during shutdown: {:?}", error);
        }
    }

    proxy.graceful_shutdown().await;
    info!("Proxy shutdown complete");

    Ok(())
}

// ============================================================================
// Metrics Persistence Utilities
// ============================================================================

/// Resolve the metrics stats file path
///
/// If `stats_file` is configured, use it; otherwise default to "stats.json" alongside config file.
///
/// # Arguments
/// * `config_path` - Path to the configuration file
/// * `configured_path` - Optional configured path from config file
#[must_use]
pub fn resolve_stats_file_path(
    config_path: &str,
    configured_path: Option<&std::path::PathBuf>,
) -> std::path::PathBuf {
    use std::path::Path;

    configured_path.map_or_else(
        || {
            // Default to stats.json alongside config file
            let config_dir = Path::new(config_path)
                .parent()
                .unwrap_or_else(|| Path::new("."));
            config_dir.join("stats.json")
        },
        std::clone::Clone::clone,
    )
}

/// Resolve the persistence path for the availability index when body storage is disabled.
///
/// Returns None unless `store_article_bodies = false`. If `availability_index_path` is
/// configured, use it; otherwise default to "availability.idx" alongside config.
#[must_use]
pub fn resolve_availability_file_path(
    config_path: &str,
    cache_config: Option<&crate::config::Cache>,
) -> Option<std::path::PathBuf> {
    use std::path::Path;

    let cache_config = cache_config?;
    if cache_config.store_article_bodies {
        return None;
    }

    Some(
        cache_config
            .availability_index_path
            .clone()
            .unwrap_or_else(|| {
                let config_dir = Path::new(config_path)
                    .parent()
                    .unwrap_or_else(|| Path::new("."));
                config_dir.join("availability.idx")
            }),
    )
}

/// Load metrics from disk if the stats file exists
///
/// Returns None if file doesn't exist, is empty, or can't be parsed.
/// Errors are logged but not fatal.
///
/// # Arguments
/// * `stats_path` - Path to the stats JSON file
/// * `server_names` - Current backend server names (for matching restored data)
pub fn load_metrics_from_disk(
    stats_path: &std::path::Path,
    server_names: &[String],
) -> Option<crate::metrics::MetricsStore> {
    use tracing::{info, warn};

    match crate::metrics::MetricsStore::load(stats_path, server_names) {
        Ok(Some(store)) => {
            info!("Restored metrics from {}", stats_path.display());
            Some(store)
        }
        Ok(None) => {
            info!(
                "Stats file {} is empty or doesn't exist, starting fresh",
                stats_path.display()
            );
            None
        }
        Err(e) => {
            warn!(
                "Failed to load stats from {}: {}, starting fresh",
                stats_path.display(),
                e
            );
            None
        }
    }
}

/// Load availability state from disk if the file exists.
///
/// Returns true when an availability-only cache was restored successfully.
pub fn load_availability_from_disk(
    cache: &std::sync::Arc<crate::cache::UnifiedCache>,
    availability_path: &std::path::Path,
) -> bool {
    use tracing::{info, warn};

    match cache.load_from_disk(availability_path) {
        Ok(true) => {
            info!(
                "Restored availability index from {}",
                availability_path.display()
            );
            true
        }
        Ok(false) => {
            info!(
                "Availability file {} is missing or not needed, starting fresh",
                availability_path.display()
            );
            false
        }
        Err(e) => {
            warn!(
                "Failed to load availability index from {}: {}, starting fresh",
                availability_path.display(),
                e
            );
            false
        }
    }
}

/// Spawn background task to periodically save metrics to disk
///
/// Saves every 30 seconds. Logs errors but doesn't fail.
pub fn spawn_metrics_saver(
    proxy: &std::sync::Arc<crate::NntpProxy>,
    stats_path: std::path::PathBuf,
    server_names: Vec<String>,
) {
    use std::sync::Arc;

    let proxy = Arc::clone(proxy);
    tokio::spawn(async move {
        let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(30));
        interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
        loop {
            interval.tick().await;
            let path = stats_path.clone();
            let names = server_names.clone();
            let metrics = proxy.metrics().clone();
            let save_path = path.clone();
            handle_periodic_metrics_save_result(
                &stats_path,
                tokio::task::spawn_blocking(move || metrics.save_to_disk(&save_path, &names)).await,
            );
        }
    });
}

/// Spawn background task to periodically save availability state to disk.
///
/// Saves every 30 seconds when the proxy uses the dedicated availability index.
pub fn spawn_availability_saver(
    proxy: &std::sync::Arc<crate::NntpProxy>,
    availability_path: Option<std::path::PathBuf>,
) {
    use std::sync::Arc;

    let Some(availability_path) = availability_path else {
        return;
    };

    let proxy = Arc::clone(proxy);
    tokio::spawn(async move {
        let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(30));
        interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
        loop {
            interval.tick().await;
            let cache = proxy.cache().clone();
            let path = availability_path.clone();
            let save_path = path.clone();
            handle_periodic_availability_save_result(
                &path,
                tokio::task::spawn_blocking(move || cache.save_to_disk(&save_path)).await,
            );
        }
    });
}

/// Spawn background task that periodically checks for and clears idle backend connections.
///
/// Runs every 60 seconds and delegates to [`NntpProxy::check_and_clear_stale_pools`],
/// which checks each backend independently against its configured `backend_idle_timeout`.
///
/// This prevents zombie connections from accumulating during extended idle periods,
/// which was causing connection limit cascades in the observed 6-day failure.
pub fn spawn_idle_connection_clearer(proxy: &std::sync::Arc<crate::NntpProxy>) {
    use std::sync::Arc;
    use std::time::Duration;

    /// How often to check for idle backends
    const CHECK_INTERVAL: Duration = crate::constants::duration_polyfill::from_minutes(1);

    let proxy = Arc::clone(proxy);

    tokio::spawn(async move {
        let mut interval = tokio::time::interval(CHECK_INTERVAL);
        interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);

        loop {
            interval.tick().await;
            proxy.check_and_clear_stale_pools();
        }
    });
}

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

    #[test]
    fn test_runtime_config_from_args_default() {
        let config = RuntimeConfig::from_args(None);

        // Should default to single-threaded (1 thread)
        assert_eq!(config.worker_threads(), 1);
        assert!(config.is_single_threaded());
        assert!(config.enable_cpu_pinning); // CPU pinning helps even with 1 thread
    }

    #[test]
    fn test_runtime_config_from_args_explicit() {
        let thread_count = ThreadCount::new(4).unwrap();
        let config = RuntimeConfig::from_args(Some(thread_count));

        assert_eq!(config.worker_threads(), 4);
        assert!(!config.is_single_threaded());
    }

    #[test]
    fn test_runtime_config_single_threaded() {
        let thread_count = ThreadCount::new(1).unwrap();
        let config = RuntimeConfig::from_args(Some(thread_count));

        assert_eq!(config.worker_threads(), 1);
        assert!(config.is_single_threaded());
    }

    #[test]
    fn test_runtime_config_multi_threaded() {
        let thread_count = ThreadCount::new(8).unwrap();
        let config = RuntimeConfig::from_args(Some(thread_count));

        assert_eq!(config.worker_threads(), 8);
        assert!(config.enable_cpu_pinning);
    }

    #[test]
    fn test_runtime_config_without_cpu_pinning() {
        let thread_count = ThreadCount::new(4).unwrap();
        let config = RuntimeConfig::from_args(Some(thread_count)).without_cpu_pinning();

        assert_eq!(config.worker_threads(), 4);
        assert!(!config.enable_cpu_pinning);
    }

    #[test]
    fn test_runtime_config_default() {
        let config = RuntimeConfig::default();

        // Should match from_args(None)
        let expected = RuntimeConfig::from_args(None);
        assert_eq!(config.worker_threads(), expected.worker_threads());
    }

    #[test]
    fn test_pin_to_cpu_cores_non_fatal() {
        // Should not panic even if pinning fails
        pin_to_cpu_cores(1);
    }

    // Edge case tests

    #[test]
    fn test_runtime_config_zero_threads_auto() {
        // ThreadCount(0) means auto-detect CPUs
        // We can't test ThreadCount::new(0) directly as it returns None,
        // but we can test the from_args behavior
        let config = RuntimeConfig::from_args(None);
        assert_eq!(config.worker_threads(), 1); // Defaults to 1
    }

    #[test]
    fn test_runtime_config_large_thread_count() {
        let thread_count = ThreadCount::new(128).unwrap();
        let config = RuntimeConfig::from_args(Some(thread_count));

        assert_eq!(config.worker_threads(), 128);
        assert!(!config.is_single_threaded());
    }

    #[test]
    fn test_runtime_config_clone() {
        let config = RuntimeConfig::from_args(Some(ThreadCount::new(4).unwrap()));
        let cloned = config.clone();

        assert_eq!(cloned.worker_threads(), config.worker_threads());
        assert_eq!(cloned.enable_cpu_pinning, config.enable_cpu_pinning);
    }

    #[test]
    fn test_runtime_config_debug() {
        let config = RuntimeConfig::from_args(Some(ThreadCount::new(2).unwrap()));
        let debug_str = format!("{config:?}");

        assert!(debug_str.contains("RuntimeConfig"));
        assert!(debug_str.contains("worker_threads"));
        assert!(debug_str.contains("enable_cpu_pinning"));
    }

    #[tokio::test]
    async fn test_bind_listener_context_mentions_proxy_listener() {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();

        let err = bind_listener(
            "127.0.0.1",
            Port::try_new(addr.port()).unwrap(),
            crate::RoutingMode::PerCommand,
        )
        .await
        .unwrap_err();

        let err = format!("{err:#}");
        assert!(err.contains("Failed to bind NNTP proxy listener"));
    }

    #[tokio::test]
    async fn test_accept_loop_aborts_active_clients_on_shutdown() {
        use std::sync::Arc;
        use tokio::io::AsyncReadExt;

        let proxy = Arc::new(
            crate::NntpProxy::new_sync(
                crate::create_default_config(),
                crate::RoutingMode::PerCommand,
            )
            .unwrap(),
        );
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        let (shutdown_tx, shutdown_rx) = tokio::sync::mpsc::channel(1);

        let accept_loop = tokio::spawn(run_accept_loop(
            proxy,
            listener,
            shutdown_rx,
            crate::RoutingMode::PerCommand,
        ));

        let mut client = tokio::net::TcpStream::connect(addr).await.unwrap();
        let mut greeting = Vec::new();
        let mut byte = [0; 1];
        while !greeting.ends_with(b"\n") {
            client.read_exact(&mut byte).await.unwrap();
            greeting.push(byte[0]);
        }
        assert!(greeting.starts_with(b"200") || greeting.starts_with(b"201"));

        shutdown_tx.send(()).await.unwrap();
        tokio::time::timeout(std::time::Duration::from_secs(3), accept_loop)
            .await
            .expect("accept loop should stop promptly after shutdown")
            .unwrap()
            .unwrap();

        let read_result =
            tokio::time::timeout(std::time::Duration::from_secs(3), client.read(&mut byte))
                .await
                .expect("client socket should close when active session is aborted");
        assert!(matches!(read_result, Ok(0) | Err(_)));
    }

    #[tokio::test]
    async fn test_accept_loop_aborts_all_active_clients_and_closes_listener() {
        use std::sync::Arc;
        use tokio::io::AsyncReadExt;

        let proxy = Arc::new(
            crate::NntpProxy::new_sync(
                crate::create_default_config(),
                crate::RoutingMode::PerCommand,
            )
            .unwrap(),
        );
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        let (shutdown_tx, shutdown_rx) = tokio::sync::mpsc::channel(1);

        let accept_loop = tokio::spawn(run_accept_loop(
            proxy,
            listener,
            shutdown_rx,
            crate::RoutingMode::PerCommand,
        ));

        let mut clients = Vec::new();
        for _ in 0..3 {
            let mut client = tokio::net::TcpStream::connect(addr).await.unwrap();
            let mut greeting = Vec::new();
            let mut byte = [0; 1];
            while !greeting.ends_with(b"\n") {
                client.read_exact(&mut byte).await.unwrap();
                greeting.push(byte[0]);
            }
            assert!(greeting.starts_with(b"200") || greeting.starts_with(b"201"));
            clients.push(client);
        }

        shutdown_tx.send(()).await.unwrap();
        tokio::time::timeout(std::time::Duration::from_secs(3), accept_loop)
            .await
            .expect("accept loop should stop promptly after shutdown")
            .unwrap()
            .unwrap();

        assert!(
            tokio::net::TcpStream::connect(addr).await.is_err(),
            "listener should be closed after accept-loop shutdown"
        );

        for mut client in clients {
            let mut byte = [0; 1];
            let read_result =
                tokio::time::timeout(std::time::Duration::from_secs(3), client.read(&mut byte))
                    .await
                    .expect("client socket should close when session is aborted");
            assert!(matches!(read_result, Ok(0) | Err(_)));
        }
    }

    // Builder pattern tests

    #[test]
    fn test_runtime_config_builder_chaining() {
        let config =
            RuntimeConfig::from_args(Some(ThreadCount::new(4).unwrap())).without_cpu_pinning();

        assert_eq!(config.worker_threads(), 4);
        assert!(!config.enable_cpu_pinning);
    }

    #[test]
    fn test_runtime_config_builder_multiple_calls() {
        let config = RuntimeConfig::from_args(Some(ThreadCount::new(8).unwrap()))
            .without_cpu_pinning()
            .without_cpu_pinning(); // Idempotent

        assert!(!config.enable_cpu_pinning);
    }

    // Getter tests

    #[test]
    fn test_worker_threads_getter() {
        let config = RuntimeConfig::from_args(Some(ThreadCount::new(6).unwrap()));
        assert_eq!(config.worker_threads(), 6);
    }

    #[test]
    fn test_is_single_threaded_true() {
        let config = RuntimeConfig::from_args(Some(ThreadCount::new(1).unwrap()));
        assert!(config.is_single_threaded());
    }

    #[test]
    fn test_is_single_threaded_false() {
        let config = RuntimeConfig::from_args(Some(ThreadCount::new(2).unwrap()));
        assert!(!config.is_single_threaded());
    }

    #[test]
    fn test_is_single_threaded_false_for_large() {
        let config = RuntimeConfig::from_args(Some(ThreadCount::new(100).unwrap()));
        assert!(!config.is_single_threaded());
    }

    // CPU pinning platform-specific tests

    #[test]
    #[cfg(target_os = "linux")]
    fn test_pin_to_cpu_cores_linux_single_core() {
        pin_to_cpu_cores(1);
    }

    #[test]
    #[cfg(target_os = "linux")]
    fn test_pin_to_cpu_cores_linux_multi_core() {
        pin_to_cpu_cores(4);
    }

    #[test]
    #[cfg(target_os = "linux")]
    fn test_pin_to_cpu_cores_linux_zero_cores() {
        // Edge case: 0 cores should still succeed (no-op)
        pin_to_cpu_cores(0);
    }

    #[test]
    #[cfg(target_os = "linux")]
    fn test_pin_to_cpu_cores_linux_many_cores() {
        // Try to pin to more cores than available - should not panic
        pin_to_cpu_cores(1024);
    }

    #[test]
    #[cfg(not(target_os = "linux"))]
    fn test_pin_to_cpu_cores_non_linux() {
        // Non-Linux platforms should gracefully no-op
        pin_to_cpu_cores(4);
    }

    // Default implementation tests

    #[test]
    fn test_default_matches_from_args_none() {
        let default_config = RuntimeConfig::default();
        let explicit_config = RuntimeConfig::from_args(None);

        assert_eq!(
            default_config.worker_threads(),
            explicit_config.worker_threads()
        );
        assert_eq!(
            default_config.enable_cpu_pinning,
            explicit_config.enable_cpu_pinning
        );
    }

    #[test]
    fn test_default_is_single_threaded() {
        let config = RuntimeConfig::default();
        assert!(config.is_single_threaded());
    }

    #[test]
    fn test_default_has_cpu_pinning_enabled() {
        let config = RuntimeConfig::default();
        assert!(config.enable_cpu_pinning);
    }

    // Configuration combination tests

    #[test]
    fn test_config_single_threaded_with_pinning() {
        let config = RuntimeConfig::from_args(Some(ThreadCount::new(1).unwrap()));
        assert!(config.is_single_threaded());
        assert!(config.enable_cpu_pinning);
    }

    #[test]
    fn test_config_single_threaded_without_pinning() {
        let config =
            RuntimeConfig::from_args(Some(ThreadCount::new(1).unwrap())).without_cpu_pinning();
        assert!(config.is_single_threaded());
        assert!(!config.enable_cpu_pinning);
    }

    #[test]
    fn test_config_multi_threaded_with_pinning() {
        let config = RuntimeConfig::from_args(Some(ThreadCount::new(4).unwrap()));
        assert!(!config.is_single_threaded());
        assert!(config.enable_cpu_pinning);
    }

    #[test]
    fn test_config_multi_threaded_without_pinning() {
        let config =
            RuntimeConfig::from_args(Some(ThreadCount::new(4).unwrap())).without_cpu_pinning();
        assert!(!config.is_single_threaded());
        assert!(!config.enable_cpu_pinning);
    }

    // ========================================================================
    // Metrics Persistence Tests
    // ========================================================================

    #[test]
    fn test_resolve_stats_file_path_with_configured() {
        use std::path::PathBuf;

        let configured = PathBuf::from("/custom/path/stats.json");
        let result = resolve_stats_file_path("config.toml", Some(&configured));

        assert_eq!(result, configured);
    }

    #[test]
    fn test_resolve_stats_file_path_default() {
        let result = resolve_stats_file_path("/etc/nntp-proxy/config.toml", None);

        // Should be /etc/nntp-proxy/stats.json
        assert_eq!(result.file_name().unwrap(), "stats.json");
        assert!(result.to_string_lossy().contains("nntp-proxy"));
    }

    #[test]
    fn test_resolve_stats_file_path_bare_filename() {
        let result = resolve_stats_file_path("config.toml", None);

        // Bare filename should resolve to ./stats.json
        assert_eq!(result.file_name().unwrap(), "stats.json");
    }

    #[test]
    fn test_load_metrics_from_disk_missing_file() {
        use std::path::Path;

        let nonexistent_path = Path::new("/tmp/this-does-not-exist-12345.json");
        let result = load_metrics_from_disk(nonexistent_path, &[]);

        assert!(result.is_none(), "Missing file should return None");
    }

    #[test]
    fn test_resolve_availability_file_path_none_for_full_cache() {
        let cache = crate::config::Cache {
            store_article_bodies: true,
            ..Default::default()
        };
        assert!(resolve_availability_file_path("config.toml", Some(&cache)).is_none());
    }

    #[test]
    fn test_resolve_availability_file_path_none_without_cache_config() {
        assert!(resolve_availability_file_path("config.toml", None).is_none());
    }

    #[test]
    fn test_resolve_availability_file_path_default_for_availability_only() {
        let cache = crate::config::Cache {
            store_article_bodies: false,
            ..Default::default()
        };

        let result = resolve_availability_file_path("/etc/nntp-proxy/config.toml", Some(&cache))
            .expect("availability-only mode should resolve a path");

        assert_eq!(result.file_name().unwrap(), "availability.idx");
        assert!(result.to_string_lossy().contains("nntp-proxy"));
    }

    #[test]
    fn test_resolve_availability_file_path_with_configured() {
        let cache = crate::config::Cache {
            store_article_bodies: false,
            availability_index_path: Some(std::path::PathBuf::from(
                "/custom/path/availability.idx",
            )),
            ..Default::default()
        };

        let result = resolve_availability_file_path("config.toml", Some(&cache))
            .expect("configured path should be used");

        assert_eq!(result, cache.availability_index_path.unwrap());
    }
}