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
//! Promotion logic: reserved → active group transition.
//!
//! Mirrors C++ `RequestGroupMan::fillRequestGroupFromReserver()`.
//! When active slots are available, the engine promotes groups from the
//! reserved queue to the active DashMap and spawns their download commands.
//!
//! # C++ Equivalence
//!
//! | Rust | C++ |
//! |---|---|
//! | `fill_from_reserver()` | `fillRequestGroupFromReserver()` |
//! | `configure_request_group()` | `configureRequestGroup()` |
//! | `drop_piece_storage()` | `dropPieceStorage()` |
use std::sync::Arc;
use tracing::{debug, info, warn};
use crate::request::request_group::DownloadStatus;
use crate::util::rwlock_ext::RwLockRecover;
impl super::RequestGroupMan {
/// Promote groups from the reserved queue to the active DashMap
/// until `max_concurrent` is reached or the reserved queue is empty.
/// A limit of zero means unlimited, matching aria2's option semantics.
///
/// Returns the list of groups that were promoted (so the engine can
/// create and spawn their download commands).
///
/// # C++ Flow
///
/// Mirrors `RequestGroupMan::fillRequestGroupFromReserver()`:
/// 1. Remove paused/dependency-blocked groups → pending list
/// 2. `dropPieceStorage()` — release piece storage from paused state
/// 3. `configureRequestGroup()` — set URI selector (feedback/inorder/adaptive)
/// 4. Set state to Active, increment numActive
/// 5. `createInitialCommand()` — create download commands (with error handling)
/// 6. Fire `on-download-start` hook
/// 7. Re-insert pending groups at front of reserved queue
pub fn fill_from_reserver(&self) -> Vec<Arc<std::sync::RwLock<super::RequestGroup>>> {
let _lifecycle = self.lifecycle_guard();
let max = self.max_concurrent();
let current_active = self.active_count();
let queued_at_start = self.reserved.len();
if (max != 0 && current_active >= max) || queued_at_start == 0 {
return Vec::new();
}
let mut promoted = Vec::new();
let mut pending = Vec::new(); // Paused/dependency-blocked groups
let mut inspected = 0;
// Inspect each group that was queued at the start at most once. A
// paused or dependency-blocked group must not consume a usable slot
// and prevent a later runnable group from being promoted.
while inspected < queued_at_start
&& !self.reserved.is_empty()
&& (max == 0 || self.active_count() < max)
{
inspected += 1;
let group = match self.reserved.pop_front() {
Some(g) => g,
None => break,
};
let gid = group.recover().gid();
// Skip if pause requested — collect in pending.
// C++: `if((*i)->isPauseRequested()) continue;`
// Unlike the old Rust code which broke on the first paused group,
// C++ continues iterating (skipping paused groups). We match C++
// by collecting paused groups into `pending` and continuing.
if group.recover().is_pause_requested() {
debug!(
gid = gid.value(),
"Skipping paused group, adding to pending"
);
pending.push(group);
continue;
}
// Check dependency resolution (C++ `isDependencyResolved()`).
// If not resolved, collect in pending — all subsequent groups
// would also need to wait.
if !group.recover().is_dependency_resolved() {
let dep_desc = group
.recover()
.dependency
.recover()
.as_ref()
.map(|d| d.description())
.unwrap_or_default();
debug!(gid = gid.value(), "Dependency not resolved: {}", dep_desc);
pending.push(group);
continue;
}
// ── Drop piece storage before promotion ────────────────────
// C++: `groupToAdd->dropPieceStorage()` — paused downloads
// hold piece storage references; releasing them prevents stale
// state when the download restarts.
group.recover().drop_piece_storage();
// URI selector configuration is projected into the concrete
// selector when the HTTP multi-mirror pipeline is assembled.
// RequestGroupMan owns task scheduling and does not need a
// selector instance.
// Transition: set status Active, move to active DashMap.
// Keep the status check and transition under one write lock. A
// pause can arrive after the pause-flag check above; a paused
// group must remain reserved until an explicit unpause makes it
// Waiting again.
let paused = {
let mut g = group.recover_mut();
match g.status() {
DownloadStatus::Waiting => {
g.clear_command_failure();
g.start().ok(); // Sets status to Active
g.clear_pause();
false
}
DownloadStatus::Paused => true,
other => {
warn!(
gid = gid.value(),
?other,
"Unexpected status in reserved queue, skipping"
);
false
}
}
};
if paused {
pending.push(group);
continue;
}
// Insert into active DashMap.
let gid_val = gid;
self.active.insert(gid_val, Arc::clone(&group));
info!(
gid = gid_val.value(),
"Promoted group from reserved to active"
);
promoted.push(group);
}
// ── Re-insert pending groups at front of reserved queue ─────────
// C++: `reservedGroups_.insert(reservedGroups_.begin(), ...)`
// Pending groups (paused / dependency-blocked) go back to the front
// so the next scheduling pass can re-evaluate them.
for group in pending.into_iter().rev() {
self.reserved.push_front(group);
}
promoted
}
/// Current number of active groups that consume the concurrency budget.
/// Seed-only BitTorrent groups remain in the active map for seeding but are
/// excluded, matching C++ `isSeedOnlyEnabled()` handling.
pub fn active_count(&self) -> usize {
self.active
.iter()
// A paused or terminal group remains in the active map until its
// last command reports completion. It still owns a concurrency
// slot during that drain window, so the scheduling count must be
// based on map ownership rather than the published status.
.filter(|entry| !entry.recover().is_seed_only())
.count()
}
/// Check whether the number of active downloads exceeds `max_concurrent`.
pub fn exceeds_max_concurrent(&self) -> bool {
let max = self.max_concurrent();
max > 0 && self.active_count() > max
}
/// Reduce the number of active downloads to the `max_concurrent` limit
/// by pausing the excess groups.
///
/// Mirrors C++ `RequestGroupMan::reduceActiveDownloadsToLimit()`.
///
/// In C++ aria2-next, this method iterates from the end (most recently
/// added groups first) and for each excess group:
/// 1. Sets `haltRequested` (graceful halt)
/// 2. Sets `pauseRequested` (will pause after halt)
/// 3. Sets `restartRequested` (will re-queue when limit increases)
/// This ensures paused downloads automatically resume when the concurrent
/// limit is raised again.
pub fn reduce_to_limit(&self) -> usize {
let max = self.max_concurrent();
if max == 0 {
return 0;
}
let excess = self.active_count().saturating_sub(max);
if excess == 0 {
return 0;
}
let mut paused = 0;
// Collect active groups in order, then iterate in reverse so that
// most recently added groups get paused first — matching C++ which
// iterates from `requestGroups_.end()`.
let entries: Vec<_> = self.active.iter().collect();
for entry in entries.into_iter().rev() {
if paused >= excess {
break;
}
let mut g = entry.recover_mut();
// Skip groups that are already seeding, halted, or paused
// (mirrors C++ `isSeedOnlyEnabled() || isHaltRequested() || isPauseRequested()`)
if !matches!(g.status(), DownloadStatus::Active) || g.is_seed_only() {
continue;
}
// In C++, the group gets: haltRequested + pauseRequested + restartRequested
// The restart flag ensures it will be re-queued when limit increases.
// Our `pause()` sets the pause flag; `request_restart()` sets the
// restart flag so the engine re-queues it on next promotion cycle.
if g.pause().is_ok() {
g.request_restart();
paused += 1;
}
}
if paused > 0 {
info!(
paused,
"Paused excess active downloads to respect max_concurrent limit"
);
}
paused
}
}