aria2-core 0.3.8

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
//! Admission and execution for HTTP Range requests.
//!
//! This module deliberately does not model download tasks or transport
//! connections. The shared `reqwest::Client` owns transport connection reuse;
//! this module owns the per-download request budget and per-authority adaptive
//! budget.

use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};

use dashmap::DashMap;
use tokio::sync::mpsc;

use crate::engine::download_cookie::CookieHelper;
use crate::engine::http_segment_downloader::{HttpSegmentDownloader, SegmentProgress, WriteChunk};
use crate::error::Result;
use crate::http::{AuthResolveOptions, HttpRequestPolicy};

const MAX_SERVER_CONCURRENCY: usize = 16;

/// One HTTP byte-range request admitted for execution.
pub struct HttpSegmentRequest {
    pub mirror_index: usize,
    pub segment_index: u32,
    /// Authority bucket used for per-server request admission.
    pub authority_key: String,
    pub url: String,
    pub offset: u64,
    pub length: u64,
    pub cookie_header: Option<String>,
    pub(crate) progress: Arc<SegmentProgress>,
    pub write_tx: mpsc::Sender<WriteChunk>,
    pub expected_entity_length: u64,
}

/// Result returned after one Range request finishes.
pub struct HttpSegmentRequestResult {
    /// Internal identity used to reclaim the task handle when this completion
    /// event is consumed. The result channel is the completion signal, so the
    /// scheduler never needs to probe every handle for readiness.
    pub(crate) task_id: u64,
    pub mirror_index: usize,
    pub segment_index: u32,
    pub authority_key: String,
    pub result: Result<u64>,
    pub peer_addr: Option<std::net::SocketAddr>,
    // The lease intentionally remains attached to the result. A completed
    // request still counts as in-flight until the scheduler consumes it.
    _lease: AdmissionLease,
}

struct AuthorityState {
    hard_limit: usize,
    target: AtomicUsize,
    in_flight: AtomicUsize,
}

struct ExecutorState {
    authorities: DashMap<Box<str>, Arc<AuthorityState>>,
    total_in_flight: AtomicUsize,
}

struct AdmissionLease {
    state: Arc<ExecutorState>,
    authority: Arc<AuthorityState>,
}

impl Drop for AdmissionLease {
    fn drop(&mut self) {
        self.authority.in_flight.fetch_sub(1, Ordering::AcqRel);
        self.state.total_in_flight.fetch_sub(1, Ordering::AcqRel);
    }
}

struct RunningTask {
    id: u64,
    segment_index: u32,
    handle: tokio::task::JoinHandle<()>,
}

/// Dynamic executor for admitted HTTP Range requests.
///
/// `total_limit` is the per-download `split` budget. Each authority also has
/// an independent target controlled by `HttpAdaptiveConcurrency`. There is no
/// fixed worker count: each admitted request owns one Tokio task until its
/// HTTP response completes.
pub struct HttpSegmentRequestExecutor {
    result_rx: mpsc::Receiver<HttpSegmentRequestResult>,
    result_tx: mpsc::Sender<HttpSegmentRequestResult>,
    client: reqwest::Client,
    request_policy: HttpRequestPolicy,
    cookie_helper: CookieHelper,
    auth_options: AuthResolveOptions,
    netrc_path: Option<String>,
    state: Arc<ExecutorState>,
    total_limit: usize,
    tasks: Vec<RunningTask>,
    next_task_id: u64,
}

impl HttpSegmentRequestExecutor {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        client: &reqwest::Client,
        request_policy: HttpRequestPolicy,
        cookie_helper: CookieHelper,
        auth_options: AuthResolveOptions,
        netrc_path: Option<String>,
        total_limit: usize,
        authority_keys: &[String],
        server_hard_limit: usize,
    ) -> Self {
        let total_limit = total_limit.max(1);
        let (result_tx, result_rx) = mpsc::channel(total_limit);
        let state = Arc::new(ExecutorState::new(authority_keys, server_hard_limit));

        Self {
            result_rx,
            result_tx,
            client: client.clone(),
            request_policy,
            cookie_helper,
            auth_options,
            netrc_path,
            state,
            total_limit,
            tasks: Vec::new(),
            next_task_id: 1,
        }
    }

    /// Admit and start a request if both total and authority targets have room.
    pub fn try_submit(&mut self, request: HttpSegmentRequest) -> Option<u64> {
        let authority = self.state.authority(&request.authority_key)?;
        let lease = self.state.try_acquire(&authority, self.total_limit)?;

        let client = self.client.clone();
        let request_policy = self.request_policy.clone();
        let cookie_helper = self.cookie_helper.clone();
        let auth_options = self.auth_options.clone();
        let netrc_path = self.netrc_path.clone();
        let result_tx = self.result_tx.clone();
        let authority_key = request.authority_key.clone();
        let task_id = self.next_task_id;
        self.next_task_id = self.next_task_id.wrapping_add(1).max(1);

        let handle = tokio::spawn(async move {
            let downloader = HttpSegmentDownloader::new_with_policy(&client, request_policy)
                .with_cookie_helper(cookie_helper)
                .with_auth_options(auth_options, netrc_path);
            downloader.clear_last_peer_addr();
            let result = downloader
                .download_range_streaming_with_progress(
                    &request.url,
                    request.offset,
                    request.length,
                    request.cookie_header.as_deref(),
                    &[],
                    Some(&request.progress),
                    &request.write_tx,
                    request.expected_entity_length,
                )
                .await;
            let request_result = HttpSegmentRequestResult {
                task_id,
                mirror_index: request.mirror_index,
                segment_index: request.segment_index,
                authority_key,
                result,
                peer_addr: downloader.last_peer_addr(),
                _lease: lease,
            };

            // If the scheduler has gone away, dropping the result also drops
            // the lease and releases both admission counters.
            let _ = result_tx.send(request_result).await;
        });
        self.tasks.push(RunningTask {
            id: task_id,
            segment_index: request.segment_index,
            handle,
        });
        Some(task_id)
    }

    pub fn set_target(&self, authority_key: &str, target: usize) {
        if let Some(authority) = self.state.authority(authority_key) {
            authority
                .target
                .store(target.clamp(1, authority.hard_limit), Ordering::Release);
        }
    }

    pub fn target_for(&self, authority_key: &str) -> Option<usize> {
        self.state
            .authority(authority_key)
            .map(|authority| authority.target.load(Ordering::Acquire))
    }

    /// Includes completed results waiting to be consumed by the scheduler.
    pub fn in_flight(&self) -> usize {
        self.state.total_in_flight.load(Ordering::Acquire)
    }

    pub fn in_flight_for(&self, authority_key: &str) -> usize {
        self.state
            .authority(authority_key)
            .map(|authority| authority.in_flight.load(Ordering::Acquire))
            .unwrap_or(0)
    }

    pub async fn next_result(&mut self) -> Option<HttpSegmentRequestResult> {
        let result = self.result_rx.recv().await?;
        self.reap_task(result.task_id).await;
        Some(result)
    }

    /// Abort one stalled Range request and release its admission lease.
    pub async fn abort_segment(&mut self, segment_index: u32) -> bool {
        let Some(index) = self
            .tasks
            .iter()
            .position(|task| task.segment_index == segment_index)
        else {
            return false;
        };
        let task = self.tasks.swap_remove(index);
        task.handle.abort();
        let _ = task.handle.await;
        true
    }

    /// Reclaim the task handle associated with a completion event.
    ///
    /// A request sends its result immediately before returning, so awaiting
    /// this exact handle is short and deterministic. This keeps task storage
    /// bounded by requests that have not yet emitted a completion event.
    async fn reap_task(&mut self, task_id: u64) {
        let Some(index) = self.tasks.iter().position(|task| task.id == task_id) else {
            return;
        };
        let task = self.tasks.swap_remove(index);
        let _ = task.handle.await;
    }

    /// Wait for all admitted requests after the scheduler has drained results.
    pub async fn shutdown(mut self) {
        while let Some(task) = self.tasks.pop() {
            let _ = task.handle.await;
        }
    }

    /// Abort all admitted requests. Used for cancellation and Range fallback.
    pub async fn cancel(mut self) {
        for task in &self.tasks {
            task.handle.abort();
        }
        while let Some(task) = self.tasks.pop() {
            let _ = task.handle.await;
        }
    }
}

impl Drop for HttpSegmentRequestExecutor {
    fn drop(&mut self) {
        for task in &self.tasks {
            task.handle.abort();
        }
    }
}

impl ExecutorState {
    fn new(authority_keys: &[String], server_hard_limit: usize) -> Self {
        let hard_limit = server_hard_limit.clamp(1, MAX_SERVER_CONCURRENCY);
        let authorities = DashMap::new();
        for key in authority_keys {
            authorities.insert(
                key.clone().into_boxed_str(),
                Arc::new(AuthorityState {
                    hard_limit,
                    target: AtomicUsize::new(hard_limit),
                    in_flight: AtomicUsize::new(0),
                }),
            );
        }
        Self {
            authorities,
            total_in_flight: AtomicUsize::new(0),
        }
    }

    fn authority(&self, authority_key: &str) -> Option<Arc<AuthorityState>> {
        self.authorities
            .get(authority_key)
            .map(|entry| Arc::clone(entry.value()))
    }

    fn try_acquire(
        self: &Arc<Self>,
        authority: &Arc<AuthorityState>,
        total_limit: usize,
    ) -> Option<AdmissionLease> {
        if !reserve_total(&self.total_in_flight, total_limit) {
            return None;
        }
        if !reserve_authority(authority) {
            self.total_in_flight.fetch_sub(1, Ordering::AcqRel);
            return None;
        }
        Some(AdmissionLease {
            state: Arc::clone(self),
            authority: Arc::clone(authority),
        })
    }
}

fn reserve_total(total: &AtomicUsize, limit: usize) -> bool {
    let mut current = total.load(Ordering::Acquire);
    loop {
        if current >= limit {
            return false;
        }
        match total.compare_exchange_weak(current, current + 1, Ordering::AcqRel, Ordering::Acquire)
        {
            Ok(_) => return true,
            Err(observed) => current = observed,
        }
    }
}

fn reserve_authority(authority: &AuthorityState) -> bool {
    let target = authority.target.load(Ordering::Acquire);
    let mut current = authority.in_flight.load(Ordering::Acquire);
    loop {
        if current >= target {
            return false;
        }
        match authority.in_flight.compare_exchange_weak(
            current,
            current + 1,
            Ordering::AcqRel,
            Ordering::Acquire,
        ) {
            Ok(_) => return true,
            Err(observed) => current = observed,
        }
    }
}

/// Return the authority used for per-server request accounting.
pub fn authority_key(url: &str) -> Option<String> {
    let url = reqwest::Url::parse(url).ok()?;
    let host = url.host_str()?.to_ascii_lowercase();
    let host = if host.contains(':') && !host.starts_with('[') {
        format!("[{host}]")
    } else {
        host
    };
    let port = url.port_or_known_default()?;
    Some(format!(
        "{}://{host}:{port}",
        url.scheme().to_ascii_lowercase()
    ))
}

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

    #[test]
    fn authority_key_includes_scheme_and_ignores_path() {
        assert_eq!(
            authority_key("HTTP://Example.TEST/download/file").as_deref(),
            Some("http://example.test:80")
        );
        assert_eq!(
            authority_key("https://[::1]/file").as_deref(),
            Some("https://[::1]:443")
        );
        assert_ne!(
            authority_key("http://example.test/file"),
            authority_key("https://example.test/file")
        );
    }

    #[test]
    fn leases_keep_completed_requests_in_flight_until_consumed() {
        let state = Arc::new(ExecutorState::new(&["http://example.test:80".into()], 4));
        let authority = state.authority("http://example.test:80").unwrap();
        let lease = state.try_acquire(&authority, 2).unwrap();
        assert_eq!(state.total_in_flight.load(Ordering::Acquire), 1);
        assert_eq!(authority.in_flight.load(Ordering::Acquire), 1);
        drop(lease);
        assert_eq!(state.total_in_flight.load(Ordering::Acquire), 0);
        assert_eq!(authority.in_flight.load(Ordering::Acquire), 0);
    }

    #[test]
    fn total_and_authority_limits_are_independent() {
        let state = Arc::new(ExecutorState::new(
            &["http://one.test:80".into(), "http://two.test:80".into()],
            2,
        ));
        let one = state.authority("http://one.test:80").unwrap();
        let two = state.authority("http://two.test:80").unwrap();
        let first = state.try_acquire(&one, 2).unwrap();
        let second = state.try_acquire(&one, 2).unwrap();
        assert!(state.try_acquire(&two, 2).is_none());
        drop(first);
        drop(second);
        assert!(state.try_acquire(&two, 2).is_some());
    }

    #[tokio::test]
    async fn completion_event_reclaims_only_its_task() {
        crate::http::client_pool::ensure_rustls_provider();
        let (result_tx, result_rx) = mpsc::channel(1);
        let mut executor = HttpSegmentRequestExecutor {
            result_rx,
            result_tx,
            client: reqwest::Client::new(),
            request_policy: HttpRequestPolicy::default(),
            cookie_helper: CookieHelper::new(
                Arc::new(crate::http::cookie_storage::CookieStorage::new()),
                None,
            ),
            auth_options: AuthResolveOptions::default(),
            netrc_path: None,
            state: Arc::new(ExecutorState::new(&[], 1)),
            total_limit: 1,
            tasks: vec![
                RunningTask {
                    id: 1,
                    segment_index: 1,
                    handle: tokio::spawn(async {}),
                },
                RunningTask {
                    id: 2,
                    segment_index: 2,
                    handle: tokio::spawn(async {}),
                },
            ],
            next_task_id: 3,
        };

        executor.reap_task(2).await;
        assert_eq!(executor.tasks.len(), 1);
        assert_eq!(executor.tasks[0].id, 1);

        executor.cancel().await;
    }
}