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
//! Command counter and lifecycle control flag accessors.
//!
//! These methods provide lock-free access to the command counter
//! (`num_commands`) and control flags (`DownloadControlFlags`), which
//! are checked on every iteration of the download hot path.
use std::sync::atomic::Ordering;
use crate::util::rwlock_ext::RwLockRecover;
impl super::RequestGroup {
// ── Command Counter (lifecycle tracking) ────────────────────────────
/// Increment the in-flight command counter (before spawning a task).
/// Mirrors C++ `AbstractCommand` constructor incrementing `numCommand_`.
pub fn inc_commands(&self) {
self.num_commands.fetch_add(1, Ordering::SeqCst);
}
/// Decrement the in-flight command counter (when a task completes).
/// Mirrors C++ `AbstractCommand` destructor decrementing `numCommand_`.
/// Returns the previous value, or zero when the counter is already empty.
pub fn dec_commands(&self) -> u32 {
self.num_commands
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |current| {
current.checked_sub(1)
})
.unwrap_or(0)
}
/// Get the current number of in-flight commands (lock-free).
/// When this is 0, the group has no running work and can be demoted.
pub fn num_commands(&self) -> u32 {
self.num_commands.load(Ordering::SeqCst)
}
// ── Lifecycle Control Flag Accessors ────────────────────────────────
/// Non-blocking check whether a graceful halt has been requested.
///
/// The download loop should call this on each iteration; if `true`,
/// finish writing the current chunk and then stop.
pub fn is_halt_requested(&self) -> bool {
self.control_flags.is_halt_requested()
}
/// Non-blocking check whether a forced halt has been requested.
///
/// If `true`, the download loop should abort immediately, even mid-write.
pub fn is_force_halt_requested(&self) -> bool {
self.control_flags.is_force_halt_requested()
}
/// Non-blocking check whether a pause has been requested.
pub fn is_pause_requested(&self) -> bool {
self.control_flags.is_pause_requested()
}
/// Non-blocking check whether a forced pause has been requested.
pub fn is_force_pause_requested(&self) -> bool {
self.control_flags.is_force_pause_requested()
}
/// Non-blocking check whether a restart has been requested.
pub fn is_restart_requested(&self) -> bool {
self.control_flags.is_restart_requested()
}
/// Request a restart (stop current download and re-queue).
pub fn request_restart(&self) {
self.control_flags.request_restart();
tracing::info!(gid = self.gid.value(), "Restart requested");
}
/// Get the current halt reason.
pub fn get_halt_reason(&self) -> super::halt_reason::HaltReason {
*self.halt_reason.recover()
}
/// Record an error code and message for this download.
pub fn set_last_error(
&self,
code: super::result_code::DownloadResultCode,
message: impl Into<String>,
) {
*self.last_error_code.recover_mut() = code;
*self.last_error_message.recover_mut() = message.into();
}
/// Get the last recorded error code.
pub fn get_last_error_code(&self) -> super::result_code::DownloadResultCode {
*self.last_error_code.recover()
}
/// Get the last recorded error message.
pub fn get_last_error_message(&self) -> String {
self.last_error_message.recover().clone()
}
/// Clear command-failure state before starting a new command generation.
pub fn clear_command_failure(&self) {
self.command_failure
.store(false, std::sync::atomic::Ordering::Release);
}
/// Return the number of resume failures recorded for this group.
pub fn resume_failure_count(&self) -> u32 {
self.resume_failure_count
.load(std::sync::atomic::Ordering::Acquire)
}
/// Record one `CANNOT_RESUME` URI attempt and return the new count.
pub fn increase_resume_failure_count(&self) -> u32 {
self.resume_failure_count
.fetch_add(1, std::sync::atomic::Ordering::AcqRel)
.saturating_add(1)
}
}