aria2-core 0.3.4

aria2-rust core download engine: multi-protocol segmented downloads, rate limiting, configuration, session persistence, and BitTorrent seeding
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
use std::collections::HashSet;
use std::time::Duration;

use crate::constants;
use crate::error::{Aria2Error, RecoverableError};

#[derive(Debug, Clone)]
pub struct RetryPolicy {
    /// Maximum total attempts. `0` means unlimited, matching aria2's
    /// `--max-tries` contract. The field name is retained for source
    /// compatibility with existing internal callers.
    pub max_retries: u32,
    pub base_wait_ms: u64,
    pub max_wait_ms: u64,
    pub backoff_factor: f64,
    pub retryable_http_codes: HashSet<u16>,
    pub max_retries_per_server: u32,
}

impl Default for RetryPolicy {
    fn default() -> Self {
        let codes: HashSet<u16> = constants::RETRYABLE_HTTP_CODES.iter().copied().collect();
        Self {
            max_retries: constants::DEFAULT_MAX_RETRIES,
            base_wait_ms: 1000,
            max_wait_ms: 30000,
            backoff_factor: 2.0,
            retryable_http_codes: codes,
            max_retries_per_server: u32::MAX,
        }
    }
}

impl RetryPolicy {
    #[allow(clippy::field_reassign_with_default)]
    pub fn new(max_retries: u32, base_wait_ms: u64) -> Self {
        let mut policy = Self::default();
        policy.max_retries = max_retries;
        policy.base_wait_ms = base_wait_ms;
        policy
    }

    pub fn with_max_retries(mut self, max_retries: u32) -> Self {
        self.max_retries = max_retries;
        self
    }

    pub fn with_max_per_server(mut self, n: u32) -> Self {
        self.max_retries_per_server = n;
        self
    }

    pub fn with_max_wait_ms(mut self, ms: u64) -> Self {
        self.max_wait_ms = ms;
        self
    }

    /// Return the configured maximum total attempts.
    pub fn max_tries(&self) -> u32 {
        self.max_retries
    }

    /// Return whether another attempt is allowed after `attempts` attempts
    /// have already been made. `0` is the public unlimited value.
    pub fn can_retry_after(&self, attempts: u32) -> bool {
        self.max_retries == 0 || attempts < self.max_retries
    }

    pub fn compute_wait(&self, attempt: u32) -> Option<Duration> {
        if attempt == 0 {
            return None;
        }
        Some(self.backoff_duration(attempt.saturating_sub(1)))
    }

    /// Compute wait duration using exponential backoff (Duration-based API).
    ///
    /// This is the direct-duration form of [`compute_wait`](Self::compute_wait)
    /// for callers that need the initial wait as `attempt = 0`.
    pub fn wait_duration(&self, attempt: u32) -> Duration {
        self.backoff_duration(attempt)
    }

    fn backoff_duration(&self, exponent: u32) -> Duration {
        let raw = (self.base_wait_ms as f64) * self.backoff_factor.powi(exponent.min(20) as i32);
        let millis = raw.max(0.0).min(self.max_wait_ms as f64) as u64;
        Duration::from_millis(millis)
    }

    /// Check whether a retry should be attempted after a zero-based attempt.
    ///
    /// `attempt` is the index of the failed attempt, so attempt `0` is the
    /// first request. The policy stores total attempts; `0` means unlimited.
    pub fn should_retry(&self, attempt: u32, error: &Aria2Error) -> bool {
        self.can_retry_after(attempt.saturating_add(1)) && self.is_retryable_error(error)
    }

    fn is_retryable_error(&self, error: &Aria2Error) -> bool {
        match error {
            Aria2Error::Recoverable(RecoverableError::Timeout)
            | Aria2Error::Recoverable(RecoverableError::TemporaryNetworkFailure { .. }) => true,
            Aria2Error::Recoverable(RecoverableError::ServerError { code }) => {
                self.should_retry_http(*code)
            }
            _ => false,
        }
    }

    pub fn should_retry_http(&self, status_code: u16) -> bool {
        // Match aria2's HttpSkipResponseCommand boundary. Internal Server
        // Error is terminal, while a busy gateway is retried only when the
        // caller configured a non-zero retry wait. Gateway Timeout is the
        // exception and remains retryable even without a delay.
        match status_code {
            500 => false,
            502 | 503 if self.base_wait_ms == 0 => false,
            _ => self.retryable_http_codes.contains(&status_code),
        }
    }

    pub fn should_retry_error(&self, error_str: &str) -> bool {
        let lower = error_str.to_lowercase();
        lower.contains("timeout")
            || lower.contains("connection reset")
            || lower.contains("connection refused")
            || lower.contains("broken pipe")
            || lower.contains("timed out")
            || lower.contains("eof")
            || lower.contains("network")
            || lower.contains("dns")
            || lower.contains("socket")
            || lower.contains("unreachable")
            || lower.contains("reset by peer")
            || lower.contains("temporary")
            || lower.contains("try again")
    }

    /// Return whether `attempts` completed attempts have exhausted the limit.
    /// Unlimited policies are never exhausted.
    pub fn is_exhausted(&self, attempts: u32) -> bool {
        self.max_retries != 0 && attempts >= self.max_retries
    }

    pub fn total_estimated_wait_sec(&self) -> f64 {
        if self.max_retries == 0 {
            return f64::INFINITY;
        }

        let mut total = 0.0f64;
        for a in 1..self.max_retries {
            total += (self.base_wait_ms as f64) * self.backoff_factor.powi(a as i32 - 1);
        }
        total / 1000.0
    }

    pub fn stats(&self) -> RetryPolicyStats {
        RetryPolicyStats {
            max_retries: self.max_retries,
            retryable_codes_count: self.retryable_http_codes.len(),
            estimated_max_total_wait_sec: self.total_estimated_wait_sec(),
        }
    }
}

pub struct RetryPolicyStats {
    pub max_retries: u32,
    pub retryable_codes_count: usize,
    pub estimated_max_total_wait_sec: f64,
}

#[derive(Debug, Clone)]
pub struct AttemptRecord {
    pub attempt_number: u32,
    pub started_at: std::time::Instant,
    pub error: Option<String>,
    pub duration: Duration,
}

impl AttemptRecord {
    pub fn new(attempt_number: u32) -> Self {
        Self {
            attempt_number,
            started_at: std::time::Instant::now(),
            error: None,
            duration: Duration::ZERO,
        }
    }

    pub fn finish(mut self, error: Option<String>) -> Self {
        self.duration = self.started_at.elapsed();
        self.error = error;
        self
    }
}

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

    #[test]
    fn test_default_policy_values() {
        let p = RetryPolicy::default();
        assert_eq!(p.max_retries, 5);
        assert_eq!(p.base_wait_ms, 1000);
        assert_eq!(p.max_wait_ms, 30000);
        assert!((p.backoff_factor - 2.0).abs() < 0.001);
        assert!(p.retryable_http_codes.contains(&408));
        assert!(p.retryable_http_codes.contains(&503));
        assert_eq!(p.retryable_http_codes.len(), 6);
    }

    #[test]
    fn test_compute_wait_exponential() {
        let p = RetryPolicy::default();
        assert_eq!(p.compute_wait(0), None);
        assert_eq!(p.compute_wait(1), Some(Duration::from_millis(1000)));
        assert_eq!(p.compute_wait(2), Some(Duration::from_millis(2000)));
        assert_eq!(p.compute_wait(3), Some(Duration::from_millis(4000)));
        assert_eq!(p.compute_wait(4), Some(Duration::from_millis(8000)));
    }

    #[test]
    fn test_compute_wait_capped_at_max() {
        let p = RetryPolicy::default();
        let w5 = p.compute_wait(5).unwrap().as_millis();
        assert!(
            w5 <= p.max_wait_ms as u128,
            "wait={} should be capped at {}",
            w5,
            p.max_wait_ms
        );
    }

    #[test]
    fn test_compute_wait_zero_attempts() {
        let p = RetryPolicy::default();
        assert_eq!(p.compute_wait(0), None, "attempt 0 should not wait");
    }

    #[test]
    fn test_wait_duration_preserves_millisecond_precision_and_backoff_factor() {
        let mut p = RetryPolicy::new(3, 5);
        p.backoff_factor = 3.0;

        assert_eq!(p.wait_duration(0), Duration::from_millis(5));
        assert_eq!(p.wait_duration(1), Duration::from_millis(15));
        assert_eq!(p.wait_duration(2), Duration::from_millis(45));
        assert_eq!(p.compute_wait(1), Some(Duration::from_millis(5)));
        assert_eq!(p.compute_wait(2), Some(Duration::from_millis(15)));
    }

    #[test]
    fn test_should_retry_http_statuses_follow_server_retry_boundary() {
        let p = RetryPolicy::default();
        assert!(
            p.should_retry_http(408),
            "Request Timeout should be retryable"
        );
        assert!(
            p.should_retry_http(429),
            "Too Many Requests should be retryable"
        );
        assert!(
            !p.should_retry_http(500),
            "Internal Server Error should be terminal"
        );
        assert!(
            p.should_retry_http(503),
            "Service Unavailable should be retryable"
        );

        let no_wait = RetryPolicy::new(3, 0);
        assert!(!no_wait.should_retry_http(502));
        assert!(!no_wait.should_retry_http(503));
        assert!(no_wait.should_retry_http(504));
    }

    #[test]
    fn test_should_retry_http_404_false() {
        let p = RetryPolicy::default();
        assert!(
            !p.should_retry_http(404),
            "Not Found should NOT be retryable"
        );
        assert!(
            !p.should_retry_http(403),
            "Forbidden should NOT be retryable"
        );
        assert!(
            !p.should_retry_http(400),
            "Bad Request should NOT be retryable"
        );
    }

    #[test]
    fn test_should_retry_network_error_true() {
        let p = RetryPolicy::default();
        assert!(p.should_retry_error("connection reset by peer"));
        assert!(p.should_retry_error("operation timed out"));
        assert!(p.should_retry_error("DNS resolution failed"));
        assert!(p.should_retry_error("broken pipe"));
        assert!(p.should_retry_error("network is unreachable"));
    }

    #[test]
    fn test_should_retry_non_network_false() {
        let p = RetryPolicy::default();
        assert!(!p.should_retry_error("file not found"));
        assert!(!p.should_retry_error("permission denied"));
        assert!(!p.should_retry_error("invalid URL"));
        assert!(!p.should_retry_error("HTTP 404 Not Found"));
    }

    #[test]
    fn test_is_exhausted_false_under_limit() {
        let p = RetryPolicy::with_max_retries(RetryPolicy::default(), 3);
        assert!(!p.is_exhausted(0));
        assert!(!p.is_exhausted(1));
        assert!(!p.is_exhausted(2));
        assert!(p.is_exhausted(3));
    }

    #[test]
    fn test_is_exhausted_true_at_limit() {
        let p = RetryPolicy::with_max_retries(RetryPolicy::default(), 3);
        assert!(
            p.is_exhausted(3),
            "three completed attempts should exhaust max=3"
        );
    }

    #[test]
    fn test_zero_max_tries_is_unlimited() {
        let p = RetryPolicy::new(0, 0);
        assert!(!p.is_exhausted(u32::MAX));
        assert!(p.can_retry_after(u32::MAX));
        assert!(p.should_retry(
            u32::MAX,
            &Aria2Error::Recoverable(crate::error::RecoverableError::Timeout)
        ));
        assert!(p.total_estimated_wait_sec().is_infinite());
    }

    #[test]
    fn test_should_retry_only_allows_transient_errors_and_configured_statuses() {
        let p = RetryPolicy::default();

        assert!(p.should_retry(0, &Aria2Error::Recoverable(RecoverableError::Timeout)));
        assert!(p.should_retry(
            0,
            &Aria2Error::Recoverable(RecoverableError::TemporaryNetworkFailure {
                message: "connection reset".to_string(),
            })
        ));

        for code in [408, 429, 502, 503, 504] {
            assert!(
                p.should_retry(
                    0,
                    &Aria2Error::Recoverable(RecoverableError::ServerError { code })
                ),
                "HTTP status {code} should be retryable"
            );
        }

        assert!(!p.should_retry(
            0,
            &Aria2Error::Recoverable(RecoverableError::ServerError { code: 500 })
        ));

        let no_wait = RetryPolicy::new(3, 0);
        for code in [502, 503] {
            assert!(!no_wait.should_retry(
                0,
                &Aria2Error::Recoverable(RecoverableError::ServerError { code })
            ));
        }
        assert!(no_wait.should_retry(
            0,
            &Aria2Error::Recoverable(RecoverableError::ServerError { code: 504 })
        ));

        for error in [
            Aria2Error::Recoverable(RecoverableError::ServerError { code: 404 }),
            Aria2Error::Recoverable(RecoverableError::ServerError { code: 501 }),
            Aria2Error::Recoverable(RecoverableError::RangeNotSatisfiable {
                range: "bytes=10-20".to_string(),
            }),
            Aria2Error::Recoverable(RecoverableError::CannotResume),
            Aria2Error::Recoverable(RecoverableError::HttpProtocolError {
                message: "redirect missing Location".to_string(),
            }),
            Aria2Error::Recoverable(RecoverableError::HttpAuthFailed {
                message: "HTTP 401".to_string(),
            }),
            Aria2Error::Recoverable(RecoverableError::HttpTooManyRedirects { count: 20 }),
            Aria2Error::Recoverable(RecoverableError::ResourceNotFound),
            Aria2Error::Recoverable(RecoverableError::MaxFileNotFound),
        ] {
            assert!(
                !p.should_retry(0, &error),
                "error should not be retried: {error}"
            );
        }
    }

    #[test]
    fn test_should_retry_honors_custom_http_status_allowlist() {
        let mut p = RetryPolicy::new(3, 0);
        p.retryable_http_codes.clear();
        p.retryable_http_codes.insert(418);

        assert!(p.should_retry(
            0,
            &Aria2Error::Recoverable(RecoverableError::ServerError { code: 418 })
        ));
        assert!(!p.should_retry(
            0,
            &Aria2Error::Recoverable(RecoverableError::ServerError { code: 503 })
        ));
    }

    #[test]
    fn test_max_tries_is_total_attempts() {
        let p = RetryPolicy::new(1, 0);
        let error = Aria2Error::Recoverable(crate::error::RecoverableError::Timeout);
        assert!(!p.should_retry(0, &error));

        let p = RetryPolicy::new(3, 0);
        assert!(p.should_retry(0, &error));
        assert!(p.should_retry(1, &error));
        assert!(!p.should_retry(2, &error));
    }

    #[test]
    fn test_stats_reasonable_values() {
        let p = RetryPolicy::default();
        let s = p.stats();
        assert_eq!(s.max_retries, 5);
        assert_eq!(s.retryable_codes_count, 6);
        assert!(s.estimated_max_total_wait_sec > 0.0);
        assert!(s.estimated_max_total_wait_sec < 30.0 + 10.0);
    }

    #[test]
    fn test_custom_policy_override() {
        let p = RetryPolicy::new(5, 2000).with_max_retries(10);
        assert_eq!(p.max_retries, 10);
        assert_eq!(p.base_wait_ms, 2000);

        let w = p.compute_wait(1).unwrap();
        assert_eq!(w, Duration::from_millis(2000));

        assert!(!p.is_exhausted(9));
        assert!(p.is_exhausted(11));
    }

    #[test]
    fn test_attempt_record_lifecycle() {
        let rec = AttemptRecord::new(2);
        assert_eq!(rec.attempt_number, 2);
        assert!(rec.error.is_none());
        assert_eq!(rec.duration, Duration::ZERO);

        let finished = rec.finish(Some("timeout".to_string()));
        assert_eq!(finished.error.as_deref().unwrap(), "timeout");
        // Just verify duration field exists (u128 is always >= 0)
        let _ = finished.duration;
    }
}