raknet-rust 0.2.0

Asynchronous, high-performance RakNet transport library for Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
use std::time::Duration;

use crate::error::ConfigValidationError;
use crate::protocol::constants;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AckNackFlushProfile {
    LowLatency,
    Balanced,
    Throughput,
    Custom,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AckNackPriority {
    NackFirst,
    AckFirst,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BackpressureMode {
    Delay,
    Shed,
    Disconnect,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CongestionProfile {
    Conservative,
    HighLatency,
    Custom,
}

#[derive(Debug, Clone, Copy)]
pub struct AckNackFlushSettings {
    pub ack_flush_interval: Duration,
    pub nack_flush_interval: Duration,
    pub ack_max_ranges_per_datagram: usize,
    pub nack_max_ranges_per_datagram: usize,
    pub ack_nack_priority: AckNackPriority,
}

#[derive(Debug, Clone, Copy)]
pub struct CongestionSettings {
    pub resend_rto: Duration,
    pub min_resend_rto: Duration,
    pub max_resend_rto: Duration,
    pub initial_congestion_window: f64,
    pub min_congestion_window: f64,
    pub max_congestion_window: f64,
    pub congestion_slow_start_threshold: f64,
    pub congestion_additive_gain: f64,
    pub congestion_multiplicative_decrease_nack: f64,
    pub congestion_multiplicative_decrease_timeout: f64,
    pub congestion_high_rtt_threshold_ms: f64,
    pub congestion_high_rtt_additive_scale: f64,
    pub congestion_nack_backoff_cooldown: Duration,
}

impl AckNackFlushProfile {
    pub fn settings(self) -> AckNackFlushSettings {
        match self {
            Self::LowLatency => AckNackFlushSettings {
                ack_flush_interval: Duration::from_millis(4),
                nack_flush_interval: Duration::from_millis(1),
                ack_max_ranges_per_datagram: 24,
                nack_max_ranges_per_datagram: 64,
                ack_nack_priority: AckNackPriority::NackFirst,
            },
            Self::Balanced => AckNackFlushSettings {
                ack_flush_interval: Duration::from_millis(10),
                nack_flush_interval: Duration::from_millis(2),
                ack_max_ranges_per_datagram: 48,
                nack_max_ranges_per_datagram: 96,
                ack_nack_priority: AckNackPriority::NackFirst,
            },
            Self::Throughput => AckNackFlushSettings {
                ack_flush_interval: Duration::from_millis(24),
                nack_flush_interval: Duration::from_millis(4),
                ack_max_ranges_per_datagram: 96,
                nack_max_ranges_per_datagram: 128,
                ack_nack_priority: AckNackPriority::NackFirst,
            },
            Self::Custom => AckNackFlushSettings {
                ack_flush_interval: Duration::from_millis(10),
                nack_flush_interval: Duration::from_millis(2),
                ack_max_ranges_per_datagram: 48,
                nack_max_ranges_per_datagram: 96,
                ack_nack_priority: AckNackPriority::NackFirst,
            },
        }
    }
}

impl CongestionProfile {
    pub fn settings(self) -> CongestionSettings {
        match self {
            Self::Conservative => CongestionSettings {
                resend_rto: Duration::from_millis(250),
                min_resend_rto: Duration::from_millis(80),
                max_resend_rto: Duration::from_millis(2_000),
                initial_congestion_window: 64.0,
                min_congestion_window: 8.0,
                max_congestion_window: 1024.0,
                congestion_slow_start_threshold: 128.0,
                congestion_additive_gain: 1.0,
                congestion_multiplicative_decrease_nack: 0.85,
                congestion_multiplicative_decrease_timeout: 0.6,
                congestion_high_rtt_threshold_ms: 180.0,
                congestion_high_rtt_additive_scale: 0.6,
                congestion_nack_backoff_cooldown: Duration::from_millis(50),
            },
            Self::HighLatency => CongestionSettings {
                resend_rto: Duration::from_millis(350),
                min_resend_rto: Duration::from_millis(120),
                max_resend_rto: Duration::from_millis(3_000),
                initial_congestion_window: 48.0,
                min_congestion_window: 8.0,
                max_congestion_window: 768.0,
                congestion_slow_start_threshold: 96.0,
                congestion_additive_gain: 0.85,
                congestion_multiplicative_decrease_nack: 0.92,
                congestion_multiplicative_decrease_timeout: 0.75,
                congestion_high_rtt_threshold_ms: 140.0,
                congestion_high_rtt_additive_scale: 0.85,
                congestion_nack_backoff_cooldown: Duration::from_millis(100),
            },
            Self::Custom => Self::Conservative.settings(),
        }
    }
}

#[derive(Debug, Clone)]
pub struct SessionTunables {
    pub ack_nack_flush_profile: AckNackFlushProfile,
    pub congestion_profile: CongestionProfile,
    pub ack_flush_interval: Duration,
    pub nack_flush_interval: Duration,
    pub ack_max_ranges_per_datagram: usize,
    pub nack_max_ranges_per_datagram: usize,
    pub ack_nack_priority: AckNackPriority,
    pub ack_queue_capacity: usize,
    pub backpressure_mode: BackpressureMode,
    pub reliable_window: usize,
    pub split_ttl: Duration,
    pub max_split_parts: u32,
    pub max_concurrent_splits: usize,
    pub max_ordering_channels: usize,
    pub max_ordered_pending_per_channel: usize,
    pub max_order_gap: u32,
    pub resend_rto: Duration,
    pub min_resend_rto: Duration,
    pub max_resend_rto: Duration,
    pub initial_congestion_window: f64,
    pub min_congestion_window: f64,
    pub max_congestion_window: f64,
    pub congestion_slow_start_threshold: f64,
    pub congestion_additive_gain: f64,
    pub congestion_multiplicative_decrease_nack: f64,
    pub congestion_multiplicative_decrease_timeout: f64,
    pub congestion_high_rtt_threshold_ms: f64,
    pub congestion_high_rtt_additive_scale: f64,
    pub congestion_nack_backoff_cooldown: Duration,
    pub pacing_enabled: bool,
    pub pacing_start_full: bool,
    pub pacing_gain: f64,
    pub pacing_min_rate_bytes_per_sec: f64,
    pub pacing_max_rate_bytes_per_sec: f64,
    pub pacing_max_burst_bytes: usize,
    pub outgoing_queue_max_frames: usize,
    pub outgoing_queue_max_bytes: usize,
    pub outgoing_queue_soft_ratio: f64,
    /// Best-effort zeroize for payload buffers that are dropped or abandoned
    /// before successful delivery. This may add CPU cost under heavy shedding.
    pub best_effort_zeroize_dropped_payloads: bool,
}

impl Default for SessionTunables {
    fn default() -> Self {
        let ack_nack_profile = AckNackFlushProfile::Balanced;
        let ack_nack_settings = ack_nack_profile.settings();
        let congestion_profile = CongestionProfile::Conservative;
        let congestion_settings = congestion_profile.settings();
        Self {
            ack_nack_flush_profile: ack_nack_profile,
            congestion_profile,
            ack_flush_interval: ack_nack_settings.ack_flush_interval,
            nack_flush_interval: ack_nack_settings.nack_flush_interval,
            ack_max_ranges_per_datagram: ack_nack_settings.ack_max_ranges_per_datagram,
            nack_max_ranges_per_datagram: ack_nack_settings.nack_max_ranges_per_datagram,
            ack_nack_priority: ack_nack_settings.ack_nack_priority,
            ack_queue_capacity: 1024,
            backpressure_mode: BackpressureMode::Shed,
            reliable_window: constants::MAX_ACK_SEQUENCES as usize,
            split_ttl: Duration::from_millis(constants::SPLIT_REASSEMBLY_TTL_MS),
            max_split_parts: constants::MAX_SPLIT_PARTS,
            max_concurrent_splits: constants::MAX_INFLIGHT_SPLIT_COMPOUNDS_PER_PEER,
            max_ordering_channels: 16,
            max_ordered_pending_per_channel: 2048,
            max_order_gap: constants::MAX_ACK_SEQUENCES as u32,
            resend_rto: congestion_settings.resend_rto,
            min_resend_rto: congestion_settings.min_resend_rto,
            max_resend_rto: congestion_settings.max_resend_rto,
            initial_congestion_window: congestion_settings.initial_congestion_window,
            min_congestion_window: congestion_settings.min_congestion_window,
            max_congestion_window: congestion_settings.max_congestion_window,
            congestion_slow_start_threshold: congestion_settings.congestion_slow_start_threshold,
            congestion_additive_gain: congestion_settings.congestion_additive_gain,
            congestion_multiplicative_decrease_nack: congestion_settings
                .congestion_multiplicative_decrease_nack,
            congestion_multiplicative_decrease_timeout: congestion_settings
                .congestion_multiplicative_decrease_timeout,
            congestion_high_rtt_threshold_ms: congestion_settings.congestion_high_rtt_threshold_ms,
            congestion_high_rtt_additive_scale: congestion_settings
                .congestion_high_rtt_additive_scale,
            congestion_nack_backoff_cooldown: congestion_settings.congestion_nack_backoff_cooldown,
            pacing_enabled: true,
            pacing_start_full: true,
            pacing_gain: 1.0,
            pacing_min_rate_bytes_per_sec: 24.0 * 1024.0,
            pacing_max_rate_bytes_per_sec: 32.0 * 1024.0 * 1024.0,
            pacing_max_burst_bytes: 128 * 1024,
            outgoing_queue_max_frames: 8192,
            outgoing_queue_max_bytes: 8 * 1024 * 1024,
            outgoing_queue_soft_ratio: 0.85,
            best_effort_zeroize_dropped_payloads: false,
        }
    }
}

impl SessionTunables {
    pub fn resolved_ack_nack_flush_settings(&self) -> AckNackFlushSettings {
        match self.ack_nack_flush_profile {
            AckNackFlushProfile::LowLatency
            | AckNackFlushProfile::Balanced
            | AckNackFlushProfile::Throughput => self.ack_nack_flush_profile.settings(),
            AckNackFlushProfile::Custom => AckNackFlushSettings {
                ack_flush_interval: self.ack_flush_interval,
                nack_flush_interval: self.nack_flush_interval,
                ack_max_ranges_per_datagram: self.ack_max_ranges_per_datagram,
                nack_max_ranges_per_datagram: self.nack_max_ranges_per_datagram,
                ack_nack_priority: self.ack_nack_priority,
            },
        }
    }

    pub fn resolved_congestion_settings(&self) -> CongestionSettings {
        match self.congestion_profile {
            CongestionProfile::Conservative | CongestionProfile::HighLatency => {
                self.congestion_profile.settings()
            }
            CongestionProfile::Custom => CongestionSettings {
                resend_rto: self.resend_rto,
                min_resend_rto: self.min_resend_rto,
                max_resend_rto: self.max_resend_rto,
                initial_congestion_window: self.initial_congestion_window,
                min_congestion_window: self.min_congestion_window,
                max_congestion_window: self.max_congestion_window,
                congestion_slow_start_threshold: self.congestion_slow_start_threshold,
                congestion_additive_gain: self.congestion_additive_gain,
                congestion_multiplicative_decrease_nack: self
                    .congestion_multiplicative_decrease_nack,
                congestion_multiplicative_decrease_timeout: self
                    .congestion_multiplicative_decrease_timeout,
                congestion_high_rtt_threshold_ms: self.congestion_high_rtt_threshold_ms,
                congestion_high_rtt_additive_scale: self.congestion_high_rtt_additive_scale,
                congestion_nack_backoff_cooldown: self.congestion_nack_backoff_cooldown,
            },
        }
    }

    pub fn validate(&self) -> Result<(), ConfigValidationError> {
        let ack_nack = self.resolved_ack_nack_flush_settings();
        let congestion = self.resolved_congestion_settings();
        if ack_nack.ack_flush_interval.is_zero() {
            return Err(ConfigValidationError::new(
                "SessionTunables",
                "ack_flush_interval",
                "must be > 0",
            ));
        }
        if ack_nack.nack_flush_interval.is_zero() {
            return Err(ConfigValidationError::new(
                "SessionTunables",
                "nack_flush_interval",
                "must be > 0",
            ));
        }
        if ack_nack.ack_max_ranges_per_datagram == 0 {
            return Err(ConfigValidationError::new(
                "SessionTunables",
                "ack_max_ranges_per_datagram",
                "must be >= 1",
            ));
        }
        if ack_nack.nack_max_ranges_per_datagram == 0 {
            return Err(ConfigValidationError::new(
                "SessionTunables",
                "nack_max_ranges_per_datagram",
                "must be >= 1",
            ));
        }

        if self.ack_queue_capacity == 0 {
            return Err(ConfigValidationError::new(
                "SessionTunables",
                "ack_queue_capacity",
                "must be >= 1",
            ));
        }
        if self.reliable_window == 0 {
            return Err(ConfigValidationError::new(
                "SessionTunables",
                "reliable_window",
                "must be >= 1",
            ));
        }
        if self.split_ttl.is_zero() {
            return Err(ConfigValidationError::new(
                "SessionTunables",
                "split_ttl",
                "must be > 0",
            ));
        }
        if self.max_split_parts == 0 {
            return Err(ConfigValidationError::new(
                "SessionTunables",
                "max_split_parts",
                "must be >= 1",
            ));
        }
        if self.max_concurrent_splits == 0 {
            return Err(ConfigValidationError::new(
                "SessionTunables",
                "max_concurrent_splits",
                "must be >= 1",
            ));
        }
        if self.max_ordering_channels == 0 {
            return Err(ConfigValidationError::new(
                "SessionTunables",
                "max_ordering_channels",
                "must be >= 1",
            ));
        }
        if self.max_ordered_pending_per_channel == 0 {
            return Err(ConfigValidationError::new(
                "SessionTunables",
                "max_ordered_pending_per_channel",
                "must be >= 1",
            ));
        }
        if self.max_order_gap == 0 {
            return Err(ConfigValidationError::new(
                "SessionTunables",
                "max_order_gap",
                "must be >= 1",
            ));
        }
        if congestion.min_resend_rto.is_zero() {
            return Err(ConfigValidationError::new(
                "SessionTunables",
                "min_resend_rto",
                "must be > 0",
            ));
        }
        if congestion.max_resend_rto.is_zero() {
            return Err(ConfigValidationError::new(
                "SessionTunables",
                "max_resend_rto",
                "must be > 0",
            ));
        }
        if congestion.min_resend_rto > congestion.max_resend_rto {
            return Err(ConfigValidationError::new(
                "SessionTunables",
                "min_resend_rto",
                "must be <= max_resend_rto",
            ));
        }
        if congestion.resend_rto < congestion.min_resend_rto
            || congestion.resend_rto > congestion.max_resend_rto
        {
            return Err(ConfigValidationError::new(
                "SessionTunables",
                "resend_rto",
                "must be within [min_resend_rto, max_resend_rto]",
            ));
        }

        validate_positive_f64(
            congestion.initial_congestion_window,
            "initial_congestion_window",
        )?;
        validate_positive_f64(congestion.min_congestion_window, "min_congestion_window")?;
        validate_positive_f64(congestion.max_congestion_window, "max_congestion_window")?;
        if congestion.min_congestion_window > congestion.max_congestion_window {
            return Err(ConfigValidationError::new(
                "SessionTunables",
                "min_congestion_window",
                "must be <= max_congestion_window",
            ));
        }
        if congestion.initial_congestion_window < congestion.min_congestion_window
            || congestion.initial_congestion_window > congestion.max_congestion_window
        {
            return Err(ConfigValidationError::new(
                "SessionTunables",
                "initial_congestion_window",
                "must be within [min_congestion_window, max_congestion_window]",
            ));
        }
        if congestion.congestion_slow_start_threshold < congestion.min_congestion_window
            || congestion.congestion_slow_start_threshold > congestion.max_congestion_window
        {
            return Err(ConfigValidationError::new(
                "SessionTunables",
                "congestion_slow_start_threshold",
                "must be within [min_congestion_window, max_congestion_window]",
            ));
        }
        validate_positive_f64(
            congestion.congestion_additive_gain,
            "congestion_additive_gain",
        )?;
        validate_fraction(
            congestion.congestion_multiplicative_decrease_nack,
            "congestion_multiplicative_decrease_nack",
        )?;
        validate_fraction(
            congestion.congestion_multiplicative_decrease_timeout,
            "congestion_multiplicative_decrease_timeout",
        )?;
        validate_positive_f64(
            congestion.congestion_high_rtt_threshold_ms,
            "congestion_high_rtt_threshold_ms",
        )?;
        if !congestion.congestion_high_rtt_additive_scale.is_finite()
            || congestion.congestion_high_rtt_additive_scale <= 0.0
            || congestion.congestion_high_rtt_additive_scale > 1.0
        {
            return Err(ConfigValidationError::new(
                "SessionTunables",
                "congestion_high_rtt_additive_scale",
                "must be finite and within (0, 1]",
            ));
        }
        if congestion.congestion_nack_backoff_cooldown.is_zero() {
            return Err(ConfigValidationError::new(
                "SessionTunables",
                "congestion_nack_backoff_cooldown",
                "must be > 0",
            ));
        }

        validate_positive_f64(self.pacing_gain, "pacing_gain")?;
        validate_positive_f64(
            self.pacing_min_rate_bytes_per_sec,
            "pacing_min_rate_bytes_per_sec",
        )?;
        validate_positive_f64(
            self.pacing_max_rate_bytes_per_sec,
            "pacing_max_rate_bytes_per_sec",
        )?;
        if self.pacing_min_rate_bytes_per_sec > self.pacing_max_rate_bytes_per_sec {
            return Err(ConfigValidationError::new(
                "SessionTunables",
                "pacing_min_rate_bytes_per_sec",
                "must be <= pacing_max_rate_bytes_per_sec",
            ));
        }
        if self.pacing_max_burst_bytes == 0 {
            return Err(ConfigValidationError::new(
                "SessionTunables",
                "pacing_max_burst_bytes",
                "must be >= 1",
            ));
        }
        if self.outgoing_queue_max_frames == 0 {
            return Err(ConfigValidationError::new(
                "SessionTunables",
                "outgoing_queue_max_frames",
                "must be >= 1",
            ));
        }
        if self.outgoing_queue_max_bytes == 0 {
            return Err(ConfigValidationError::new(
                "SessionTunables",
                "outgoing_queue_max_bytes",
                "must be >= 1",
            ));
        }
        if !self.outgoing_queue_soft_ratio.is_finite()
            || self.outgoing_queue_soft_ratio <= 0.0
            || self.outgoing_queue_soft_ratio >= 1.0
        {
            return Err(ConfigValidationError::new(
                "SessionTunables",
                "outgoing_queue_soft_ratio",
                "must be finite and within (0, 1)",
            ));
        }

        Ok(())
    }
}

fn validate_positive_f64(value: f64, field: &'static str) -> Result<(), ConfigValidationError> {
    if !value.is_finite() || value <= 0.0 {
        return Err(ConfigValidationError::new(
            "SessionTunables",
            field,
            "must be finite and > 0",
        ));
    }
    Ok(())
}

fn validate_fraction(value: f64, field: &'static str) -> Result<(), ConfigValidationError> {
    if !value.is_finite() || value <= 0.0 || value >= 1.0 {
        return Err(ConfigValidationError::new(
            "SessionTunables",
            field,
            "must be finite and within (0, 1)",
        ));
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use super::{AckNackFlushProfile, AckNackPriority, CongestionProfile, SessionTunables};

    #[test]
    fn validate_accepts_default_values() {
        SessionTunables::default()
            .validate()
            .expect("default tunables must be valid");
    }

    #[test]
    fn validate_rejects_zero_ack_queue_capacity() {
        let tunables = SessionTunables {
            ack_queue_capacity: 0,
            ..SessionTunables::default()
        };
        let err = tunables
            .validate()
            .expect_err("ack_queue_capacity=0 must be rejected");
        assert_eq!(err.config, "SessionTunables");
        assert_eq!(err.field, "ack_queue_capacity");
    }

    #[test]
    fn validate_rejects_zero_custom_ack_flush_interval() {
        let tunables = SessionTunables {
            ack_nack_flush_profile: AckNackFlushProfile::Custom,
            ack_flush_interval: Duration::ZERO,
            ..SessionTunables::default()
        };
        let err = tunables
            .validate()
            .expect_err("ack_flush_interval=0 must be rejected for custom policy");
        assert_eq!(err.config, "SessionTunables");
        assert_eq!(err.field, "ack_flush_interval");
    }

    #[test]
    fn profile_resolution_uses_profile_defaults_when_not_custom() {
        let tunables = SessionTunables {
            ack_nack_flush_profile: AckNackFlushProfile::LowLatency,
            ack_flush_interval: Duration::from_secs(99),
            nack_flush_interval: Duration::from_secs(99),
            ack_max_ranges_per_datagram: 1,
            nack_max_ranges_per_datagram: 1,
            ack_nack_priority: AckNackPriority::AckFirst,
            ..SessionTunables::default()
        };

        let resolved = tunables.resolved_ack_nack_flush_settings();
        assert_eq!(resolved.ack_flush_interval, Duration::from_millis(4));
        assert_eq!(resolved.nack_flush_interval, Duration::from_millis(1));
        assert_eq!(resolved.ack_max_ranges_per_datagram, 24);
        assert_eq!(resolved.nack_max_ranges_per_datagram, 64);
        assert_eq!(resolved.ack_nack_priority, AckNackPriority::NackFirst);
    }

    #[test]
    fn congestion_profile_resolution_uses_profile_defaults_when_not_custom() {
        let tunables = SessionTunables {
            congestion_profile: CongestionProfile::HighLatency,
            resend_rto: Duration::from_millis(10),
            min_resend_rto: Duration::from_millis(5),
            max_resend_rto: Duration::from_millis(20),
            initial_congestion_window: 1.0,
            min_congestion_window: 1.0,
            max_congestion_window: 2.0,
            congestion_slow_start_threshold: 1.0,
            congestion_additive_gain: 9.0,
            congestion_multiplicative_decrease_nack: 0.2,
            congestion_multiplicative_decrease_timeout: 0.2,
            congestion_high_rtt_threshold_ms: 9.0,
            congestion_high_rtt_additive_scale: 0.2,
            congestion_nack_backoff_cooldown: Duration::from_millis(1),
            ..SessionTunables::default()
        };

        let resolved = tunables.resolved_congestion_settings();
        assert_eq!(resolved.resend_rto, Duration::from_millis(350));
        assert_eq!(resolved.min_resend_rto, Duration::from_millis(120));
        assert_eq!(resolved.max_resend_rto, Duration::from_millis(3_000));
        assert!((resolved.congestion_multiplicative_decrease_nack - 0.92).abs() < f64::EPSILON);
        assert!((resolved.congestion_multiplicative_decrease_timeout - 0.75).abs() < f64::EPSILON);
    }

    #[test]
    fn validate_ignores_manual_congestion_fields_when_profile_is_not_custom() {
        let tunables = SessionTunables {
            congestion_profile: CongestionProfile::Conservative,
            min_resend_rto: Duration::from_millis(500),
            max_resend_rto: Duration::from_millis(100),
            ..SessionTunables::default()
        };

        tunables
            .validate()
            .expect("manual congestion fields must be ignored for non-custom congestion profile");
    }

    #[test]
    fn validate_rejects_invalid_custom_congestion_ranges() {
        let tunables = SessionTunables {
            congestion_profile: CongestionProfile::Custom,
            min_resend_rto: Duration::from_millis(500),
            max_resend_rto: Duration::from_millis(100),
            ..SessionTunables::default()
        };
        let err = tunables
            .validate()
            .expect_err("custom congestion profile must reject invalid ranges");
        assert_eq!(err.config, "SessionTunables");
        assert_eq!(err.field, "min_resend_rto");
    }
}