aria2-protocol 0.3.10

Multi-protocol networking stack for aria2-rust: HTTP/HTTPS client, FTP/SFTP, full BitTorrent (DHT/PEX/MSE), and Metalink V3/V4 parser
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
use super::*;

#[test]
fn test_controller_initialization() {
    let controller = LedbatController::new();

    assert_eq!(controller.get_window_size(), LEDBAT_MIN_CWND * DEFAULT_MSS);
    assert_eq!(controller.get_bytes_in_flight(), 0);
    assert!(controller.can_send());
    assert!(!controller.is_slow_start());
    assert_eq!(controller.get_target_delay(), LEDBAT_TARGET_DELAY);
}

#[test]
fn test_controller_with_custom_mss() {
    let controller = LedbatController::with_mss(1400);
    assert_eq!(controller.get_mss(), 1400);
    assert_eq!(controller.get_window_size(), LEDBAT_MIN_CWND * 1400);
}

#[test]
fn test_controller_with_custom_target_delay() {
    let controller = LedbatController::with_target_delay(Duration::from_millis(200));
    assert_eq!(controller.get_target_delay(), Duration::from_millis(200));
}

#[test]
fn custom_mss_and_target_delay_keep_window_math_defined() {
    let zero_mss = LedbatController::with_mss(0);
    assert_eq!(zero_mss.get_mss(), 1);
    assert_eq!(zero_mss.get_window_packets(), LEDBAT_MIN_CWND);

    let huge_mss = LedbatController::with_mss(u32::MAX);
    assert_eq!(huge_mss.get_mss(), u32::MAX / LEDBAT_MAX_CWND);
    assert_eq!(huge_mss.get_window_packets(), LEDBAT_MIN_CWND);

    let mut zero_delay = LedbatController::with_target_delay(Duration::ZERO);
    assert_eq!(zero_delay.get_target_delay(), Duration::from_micros(1));
    for _ in 0..12 {
        zero_delay.on_ack_received(0, 1400);
    }
    assert!(zero_delay.get_window_size() >= LEDBAT_MIN_CWND * zero_delay.get_mss());

    let huge_delay = LedbatController::with_target_delay(Duration::MAX);
    assert_eq!(
        huge_delay.get_target_delay(),
        Duration::from_micros(u64::MAX)
    );
}

#[test]
fn test_on_data_sent() {
    let mut controller = LedbatController::new();

    controller.on_data_sent(1500);
    assert_eq!(controller.get_bytes_in_flight(), 1500);
    assert!(controller.time_since_last_send().is_some());

    controller.on_data_sent(500);
    assert_eq!(controller.get_bytes_in_flight(), 2000);
}

#[test]
fn test_can_send() {
    let mut controller = LedbatController::new();

    // Initially can send
    assert!(controller.can_send());

    // Fill the window
    let window = controller.get_window_size();
    controller.on_data_sent(window);
    assert!(!controller.can_send());

    // ACK some data
    controller.on_ack_received(50_000, 1500);
    assert!(controller.can_send());
}

#[test]
fn test_available_window() {
    let mut controller = LedbatController::new();

    let initial_window = controller.available_window();
    assert_eq!(initial_window, controller.get_window_size());

    controller.on_data_sent(1500);
    let available = controller.available_window();
    assert_eq!(available, controller.get_window_size() - 1500);
}

#[test]
fn rfc_initial_window_increases_at_most_one_mss_per_ack() {
    let mut controller = LedbatController::new();

    let initial_cwnd = controller.get_window_size();

    controller.on_data_sent(initial_cwnd);
    controller.on_ack_received(50_000, 1500);

    assert!(controller.get_window_size() > initial_cwnd);
    assert!(controller.get_window_size() <= initial_cwnd + controller.get_mss());
    assert!(!controller.is_slow_start());
}

#[test]
fn one_minute_base_delay_tracks_the_minimum_delay() {
    let mut controller = LedbatController::new();

    // Send enough to fill window
    controller.on_data_sent(controller.get_window_size());

    // First, establish a low base delay with multiple samples
    controller.on_ack_received(50_000, 1500); // 50ms - low delay
    controller.on_ack_received(45_000, 1500); // 45ms - even lower
    controller.on_ack_received(48_000, 1500); // 48ms

    // Now we have 3 samples, base_delay should be ~45ms
    assert_eq!(
        controller.get_base_delay(),
        Some(Duration::from_micros(45_000))
    );

    // ACK with high delay (above target)
    let high_delay = LEDBAT_TARGET_DELAY.as_micros() as u64 + 50_000; // 150ms
    controller.on_ack_received(high_delay, 1500);

    // The controller reacts because queuing_delay (150-45=105ms) is above
    // the target.
    assert!(!controller.is_slow_start());
}

#[test]
fn controller_has_no_slow_start_phase() {
    let mut controller = LedbatController::new();

    // Send and ACK multiple times with low delay
    for _ in 0..15 {
        controller.on_data_sent(1500);
        controller.on_ack_received(50_000, 1500);
    }

    // This accessor stays false because the controller has no slow-start phase.
    assert!(!controller.is_slow_start());
}

#[test]
fn test_congestion_avoidance_below_target() {
    let mut controller = LedbatController::new();

    let initial_cwnd = controller.get_window_size();

    // ACK with queuing delay below target (should increase cwnd)
    let low_delay = 50_000; // 50ms, well below 100ms target
    controller.base_delay = Some(20_000); // 20ms base
    controller.current_delay = low_delay;

    controller.on_ack_received(low_delay, 1500);

    // cwnd should increase slightly
    assert!(controller.get_window_size() >= initial_cwnd);
}

#[test]
fn test_congestion_avoidance_above_target() {
    let mut controller = LedbatController::new();

    controller.cwnd = 10_000;

    // Establish a base delay first
    controller.bytes_in_flight = controller.cwnd;
    controller.on_ack_received(20_000, 1500); // 20ms - this will be base
    controller.bytes_in_flight = controller.cwnd;
    controller.on_ack_received(25_000, 1500); // 25ms
    controller.bytes_in_flight = controller.cwnd;
    controller.on_ack_received(22_000, 1500); // 22ms

    // Now we have 3 samples, base_delay should be 20ms
    assert_eq!(
        controller.get_base_delay(),
        Some(Duration::from_micros(20_000))
    );

    // Get cwnd after establishing base delay
    let cwnd_before_high_delay = controller.get_window_size();

    // ACK with queuing delay above target (should decrease cwnd)
    let high_delay = 150_000; // 150ms, queuing = 130ms > target (100ms)
    controller.bytes_in_flight = controller.cwnd;
    controller.on_ack_received(high_delay, 1500);

    // cwnd should decrease from the value before high delay
    assert!(controller.get_window_size() < cwnd_before_high_delay);

    // Continue with high delays to further reduce cwnd.
    for _ in 0..50 {
        controller.bytes_in_flight = controller.cwnd;
        controller.on_ack_received(high_delay, 1500);
    }

    // After multiple high delay ACKs, cwnd should be significantly lower
    assert!(controller.get_window_size() < cwnd_before_high_delay - 500);
}

#[test]
fn test_on_timeout() {
    let mut controller = LedbatController::new();

    controller.cwnd = 10_000;
    controller.bytes_in_flight = 10_000;
    // Timeout
    controller.on_timeout();

    assert_eq!(controller.get_window_size(), DEFAULT_MSS);
    assert!(controller.get_bytes_in_flight() > 0);
    assert!(!controller.is_slow_start());
}

#[test]
fn test_on_loss() {
    let mut controller = LedbatController::new();

    // Increase cwnd
    controller.cwnd = 10_000;
    controller.bytes_in_flight = 10_000;
    let cwnd_before = controller.get_window_size();

    // Loss
    controller.on_loss();

    // Should reduce by half
    assert!(controller.get_window_size() <= cwnd_before / 2);
    assert!(!controller.is_slow_start());
}

#[test]
fn test_reset() {
    let mut controller = LedbatController::new();

    // Modify state
    controller.on_data_sent(1500);
    controller.on_ack_received(50_000, 1500);
    controller.on_data_sent(1500);

    // Reset
    controller.reset();

    assert_eq!(controller.get_window_size(), LEDBAT_MIN_CWND * DEFAULT_MSS);
    assert_eq!(controller.get_bytes_in_flight(), 0);
    assert!(!controller.is_slow_start());
    assert!(controller.get_base_delay().is_none());
    assert_eq!(controller.get_current_delay(), Duration::ZERO);
}

#[test]
fn test_delay_measurements() {
    let mut controller = LedbatController::new();

    // First ACK
    controller.on_ack_received(50_000, 1500);
    assert_eq!(
        controller.get_base_delay(),
        Some(Duration::from_micros(50_000))
    );
    assert_eq!(
        controller.get_current_delay(),
        Duration::from_micros(50_000)
    );

    // Second ACK with higher delay
    controller.on_ack_received(80_000, 1500);
    assert_eq!(
        controller.get_base_delay(),
        Some(Duration::from_micros(50_000))
    ); // Min stays
    assert_eq!(
        controller.get_current_delay(),
        Duration::from_micros(80_000)
    );

    // Third ACK with lower delay
    controller.on_ack_received(30_000, 1500);
    assert_eq!(
        controller.get_base_delay(),
        Some(Duration::from_micros(30_000))
    ); // New min
    assert_eq!(
        controller.get_current_delay(),
        Duration::from_micros(30_000)
    );
}

#[test]
fn base_delay_minimum_ages_out_after_ten_minute_windows() {
    let mut controller = LedbatController::new();
    controller.update_base_delay_at(20_000, 0);
    for _ in 0..100 {
        controller.update_base_delay_at(50_000, 0);
    }
    assert_eq!(
        controller.get_base_delay(),
        Some(Duration::from_micros(20_000))
    );

    for minute in 1..10 {
        controller.update_base_delay_at(50_000, minute);
    }
    assert_eq!(
        controller.get_base_delay(),
        Some(Duration::from_micros(20_000))
    );

    controller.update_base_delay_at(50_000, 10);
    assert_eq!(
        controller.get_base_delay(),
        Some(Duration::from_micros(50_000))
    );
}

#[test]
fn test_queuing_delay() {
    let mut controller = LedbatController::new();

    controller.base_delay = Some(50_000); // 50ms
    controller.current_delay = 150_000; // 150ms

    let queuing = controller.get_queuing_delay();
    assert_eq!(queuing, Duration::from_micros(100_000)); // 100ms
}

#[test]
fn test_window_bounds() {
    let mut controller = LedbatController::new();

    // Try to increase cwnd beyond max
    controller.cwnd = LEDBAT_MAX_CWND * DEFAULT_MSS + 10000;
    controller.bytes_in_flight = u32::MAX;
    controller.on_ack_received(50_000, 1500);

    // Should be clamped to max
    assert_eq!(controller.get_window_size(), LEDBAT_MAX_CWND * DEFAULT_MSS);
}

#[test]
fn test_minimum_window() {
    let mut controller = LedbatController::new();

    // Force cwnd below minimum
    controller.cwnd = 100; // Below minimum
    controller.on_ack_received(200_000, 1500); // High delay to reduce cwnd

    // Should be clamped to minimum
    assert!(controller.get_window_size() >= LEDBAT_MIN_CWND * DEFAULT_MSS);
}

#[test]
fn test_bytes_in_flight_tracking() {
    let mut controller = LedbatController::new();

    // Send data
    controller.on_data_sent(1500);
    assert_eq!(controller.get_bytes_in_flight(), 1500);

    controller.on_data_sent(1500);
    assert_eq!(controller.get_bytes_in_flight(), 3000);

    // ACK some data
    controller.on_ack_received(50_000, 1500);
    assert_eq!(controller.get_bytes_in_flight(), 1500);

    // ACK remaining
    controller.on_ack_received(50_000, 1500);
    assert_eq!(controller.get_bytes_in_flight(), 0);
}

#[test]
fn test_delay_sample_count() {
    let controller = LedbatController::new();
    assert_eq!(controller.delay_sample_count(), 0);
    assert!(!controller.has_delay_samples());

    let mut controller = controller;
    controller.on_ack_received(50_000, 1500);
    assert_eq!(controller.delay_sample_count(), 1);
    assert!(controller.has_delay_samples());
}

#[test]
fn test_ledbat_queue_delay_scenario() {
    // Test scenario based on RFC 6817 principles
    let mut controller = LedbatController::new();

    // Probe with low delays, bounded to one MSS of growth per ACK.
    for i in 0..3 {
        controller.bytes_in_flight = controller.cwnd;
        controller.on_ack_received(50_000 - i * 1000, 1500); // Decreasing delays
        println!(
            "Iteration {}: cwnd = {}, slow_start = {}, base_delay = {:?}",
            i,
            controller.get_window_size(),
            controller.is_slow_start(),
            controller.get_base_delay()
        );
    }

    // The congestion-avoidance controller grows within the RFC flight cap.
    assert!(controller.get_window_size() > LEDBAT_MIN_CWND * DEFAULT_MSS);

    // Now we have 3 samples with base_delay ~47ms
    assert!(controller.get_base_delay().is_some());

    // Simulate congestion (high delay)
    for i in 0..3 {
        controller.bytes_in_flight = controller.cwnd;
        controller.on_ack_received(150_000, 1500); // 150ms delay, queuing = 103ms > target
        println!(
            "Congestion {}: cwnd = {}, slow_start = {}, queuing_delay = {:?}",
            i,
            controller.get_window_size(),
            controller.is_slow_start(),
            controller.get_queuing_delay()
        );
    }

    // cwnd should have decreased due to high delay
    // This accessor remains false.
    assert!(!controller.is_slow_start());
}

#[test]
fn test_gain_factor() {
    // Verify that the gain factor is applied correctly
    let mut controller = LedbatController::new();
    controller.cwnd = 10000;

    // Establish a base delay first
    controller.bytes_in_flight = controller.cwnd;
    controller.on_ack_received(20_000, 1500); // 20ms - this will be base
    controller.bytes_in_flight = controller.cwnd;
    controller.on_ack_received(25_000, 1500); // 25ms
    controller.bytes_in_flight = controller.cwnd;
    controller.on_ack_received(22_000, 1500); // 22ms

    // Now we have 3 samples, base_delay should be 20ms
    assert_eq!(
        controller.get_base_delay(),
        Some(Duration::from_micros(20_000))
    );

    let initial_cwnd = controller.get_window_size();

    // Queuing delay = 70ms - 20ms = 50ms, target = 100ms
    // delay_factor = (100 - 50) / 100 = 0.5
    // delta = GAIN * off_target * bytes_acked * MSS / cwnd
    controller.on_data_sent(10_000);
    controller.on_ack_received(70_000, 1500);

    let new_cwnd = controller.get_window_size();
    // The RFC normalization keeps this increase below one ACK-sized step.
    assert!(new_cwnd > initial_cwnd);
    assert!(new_cwnd < initial_cwnd + 100); // RFC-normalized growth
}

#[test]
fn test_saturating_operations() {
    let mut controller = LedbatController::new();

    // Test that operations don't overflow
    controller.on_data_sent(u32::MAX / 2);
    controller.on_data_sent(u32::MAX / 2);
    // bytes_in_flight should saturate, not overflow
    // Note: bytes_in_flight is u32, so it's always <= u32::MAX by definition

    // Test ACK with more bytes than in flight
    controller.on_ack_received(50_000, u32::MAX);
    assert_eq!(controller.get_bytes_in_flight(), 0);
}

#[test]
fn test_time_since_last_send() {
    let controller = LedbatController::new();
    assert!(controller.time_since_last_send().is_none());

    let mut controller = controller;
    controller.on_data_sent(1500);
    let elapsed = controller.time_since_last_send();
    assert!(elapsed.is_some());
    assert!(elapsed.unwrap() < Duration::from_millis(100));
}