use std::time::{Duration, Instant};
use crate::engine::http_tail_reclaim::{self, HttpTailReclaimState};
use crate::util::rwlock_ext::RwLockRecover;
use super::DownloadCommand;
impl DownloadCommand {
pub fn update_tail_reclaim_progress(&mut self) {
let current_completed = self.progress.completed_length();
if current_completed > self.last_tail_reclaim_session_download_length {
self.last_tail_reclaim_session_download_length = current_completed;
self.tail_reclaim_last_progress = Instant::now();
}
}
pub fn fill_tail_reclaim_state(&self) -> Option<HttpTailReclaimState> {
let current_completed = self.progress.completed_length();
let last_reclaim = self.last_tail_reclaim_session_download_length;
let uri = {
let g = self.group.recover();
g.uris().first().cloned().unwrap_or_default()
};
if uri.is_empty() {
return None;
}
let protocol = extract_protocol(&uri);
let g = self.group.recover();
let total_length = g.total_length() as i64;
if total_length <= 0 {
return None;
}
let completed_length = g.completed_length();
let pending_length = total_length.saturating_sub(completed_length as i64);
if pending_length <= 0 {
return None;
}
let p2p_involved = false;
let num_concurrent_command = g.num_commands() as i32;
let num_stream_command = num_concurrent_command;
let has_missing_unused_piece = false;
let no_progress_time = self.tail_reclaim_last_progress.elapsed();
let stall_time = self.startup_idle_time;
Some(HttpTailReclaimState {
protocol,
p2p_involved,
total_length,
pending_length,
has_missing_unused_piece,
num_concurrent_command,
num_stream_command,
current_session_download_length: current_completed,
last_session_download_length: last_reclaim,
no_progress_time,
stall_time,
})
}
pub fn should_reclaim_tail_segment(&self) -> bool {
match self.fill_tail_reclaim_state() {
Some(state) => http_tail_reclaim::should_reclaim_http_tail_segment(&state),
None => false,
}
}
pub fn is_tail_reclaim_check_ready(&self) -> bool {
match self.fill_tail_reclaim_state() {
Some(state) => http_tail_reclaim::is_http_tail_blocked(&state),
None => false,
}
}
pub fn needs_speed_monitoring(&self) -> bool {
self.lowest_speed_limit > 0 || self.is_tail_reclaim_check_ready()
}
pub fn startup_idle_time(&self) -> Duration {
self.startup_idle_time
}
pub fn set_startup_idle_time(&mut self, secs: u64) {
self.startup_idle_time = Duration::from_secs(secs);
}
pub fn lowest_speed_limit(&self) -> u64 {
self.lowest_speed_limit
}
pub fn set_lowest_speed_limit(&mut self, limit: u64) {
self.lowest_speed_limit = limit;
}
}
fn extract_protocol(uri: &str) -> String {
if let Some(colon_pos) = uri.find(':') {
uri[..colon_pos].to_string()
} else {
String::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::engine::download_command::DownloadCommand;
use crate::request::request_group::{DownloadOptions, GroupId};
fn create_test_cmd() -> DownloadCommand {
DownloadCommand::new(
GroupId::new(1),
"http://example.com/file.bin",
&DownloadOptions::default(),
None,
None,
)
.expect("DownloadCommand::new should succeed")
}
#[test]
fn test_extract_protocol_http() {
assert_eq!(extract_protocol("http://example.com/file"), "http");
}
#[test]
fn test_extract_protocol_https() {
assert_eq!(extract_protocol("https://example.com/file"), "https");
}
#[test]
fn test_extract_protocol_ftp() {
assert_eq!(extract_protocol("ftp://example.com/file"), "ftp");
}
#[test]
fn test_extract_protocol_no_scheme() {
assert_eq!(extract_protocol("example.com/file"), "");
}
#[test]
fn test_fill_tail_reclaim_state_returns_none_for_zero_total_length() {
let cmd = create_test_cmd();
assert!(cmd.fill_tail_reclaim_state().is_none());
}
#[test]
fn test_fill_tail_reclaim_state_returns_some_with_total_length() {
let cmd = create_test_cmd();
cmd.group.recover().set_total_length(10_000);
let state = cmd.fill_tail_reclaim_state();
assert!(state.is_some());
let s = state.unwrap();
assert_eq!(s.protocol, "http");
assert!(!s.p2p_involved);
assert_eq!(s.total_length, 10_000);
assert_eq!(s.pending_length, 10_000); }
#[test]
fn test_should_reclaim_returns_false_by_default() {
let cmd = create_test_cmd();
assert!(!cmd.should_reclaim_tail_segment());
}
#[test]
fn test_is_tail_reclaim_check_ready_returns_false_for_pure_http() {
let cmd = create_test_cmd();
assert!(!cmd.is_tail_reclaim_check_ready());
}
#[test]
fn test_needs_speed_monitoring_default() {
let cmd = create_test_cmd();
assert!(!cmd.needs_speed_monitoring());
}
#[test]
fn test_needs_speed_monitoring_with_speed_limit() {
let mut cmd = create_test_cmd();
cmd.set_lowest_speed_limit(1024);
assert!(cmd.needs_speed_monitoring());
}
#[test]
fn test_update_tail_reclaim_progress_tracks_length() {
let mut cmd = create_test_cmd();
assert_eq!(cmd.last_tail_reclaim_session_download_length, 0);
cmd.progress.set_completed_length(5000);
cmd.update_tail_reclaim_progress();
assert_eq!(cmd.last_tail_reclaim_session_download_length, 5000);
cmd.update_tail_reclaim_progress();
assert_eq!(cmd.last_tail_reclaim_session_download_length, 5000);
cmd.progress.set_completed_length(8000);
cmd.update_tail_reclaim_progress();
assert_eq!(cmd.last_tail_reclaim_session_download_length, 8000);
}
#[test]
fn test_set_startup_idle_time() {
let mut cmd = create_test_cmd();
cmd.set_startup_idle_time(20);
assert_eq!(cmd.startup_idle_time(), Duration::from_secs(20));
}
#[test]
fn test_set_lowest_speed_limit() {
let mut cmd = create_test_cmd();
cmd.set_lowest_speed_limit(4096);
assert_eq!(cmd.lowest_speed_limit(), 4096);
}
#[test]
fn test_tail_reclaim_blocked_with_simulated_mixed_commands() {
let cmd = create_test_cmd();
cmd.group.recover().set_total_length(10_000);
let mut state = cmd.fill_tail_reclaim_state().unwrap();
state.num_concurrent_command = 3;
state.num_stream_command = 1;
state.p2p_involved = false;
state.has_missing_unused_piece = false;
state.no_progress_time = Duration::from_secs(30);
state.stall_time = Duration::from_secs(20);
state.current_session_download_length = 100;
state.last_session_download_length = 100;
assert!(http_tail_reclaim::is_http_tail_blocked(&state));
assert!(http_tail_reclaim::should_reclaim_http_tail_segment(&state));
}
#[test]
fn test_tail_reclaim_not_blocked_when_still_progressing() {
let cmd = create_test_cmd();
cmd.group.recover().set_total_length(10_000);
let mut state = cmd.fill_tail_reclaim_state().unwrap();
state.num_concurrent_command = 3;
state.num_stream_command = 1;
state.current_session_download_length = 200;
state.last_session_download_length = 100;
state.no_progress_time = Duration::from_secs(60);
state.stall_time = Duration::from_secs(20);
assert!(http_tail_reclaim::is_http_tail_blocked(&state));
assert!(!http_tail_reclaim::should_reclaim_http_tail_segment(&state));
}
#[test]
fn test_tail_reclaim_not_blocked_when_p2p_involved() {
let cmd = create_test_cmd();
cmd.group.recover().set_total_length(10_000);
let mut state = cmd.fill_tail_reclaim_state().unwrap();
state.num_concurrent_command = 3;
state.num_stream_command = 1;
state.p2p_involved = true;
assert!(!http_tail_reclaim::is_http_tail_blocked(&state));
assert!(!http_tail_reclaim::should_reclaim_http_tail_segment(&state));
}
}