use std::collections::HashSet;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
use tokio_util::sync::CancellationToken;
use tracing::{debug, info, warn};
use super::download_event_hooks::{DownloadEvent, DownloadEventHooks};
use super::engine_command::{
EngineCommand, EngineCommandReceiver, EngineCommandTryRecvError, TaskResult,
};
use super::task_spawner::{CommandDependencies, spawn_download_task};
use crate::dns::dns_cache::DnsCache;
use crate::error::{Aria2Error, RecoverableError};
use crate::filesystem::file_allocation_man::FileAllocationMan;
use crate::network::ConnectionContext;
use crate::rate_limiter::RateLimiter;
use crate::request::request_group::{DownloadResultCode, DownloadStatus, GroupId, HaltReason};
use crate::request::request_group_man::RequestGroupMan;
use crate::selector::server_stat_man::ServerStatMan;
use crate::session::auto_save_coordinator::AutoSaveCoordinator;
use crate::util::rwlock_ext::RwLockRecover;
const MAX_STOPPED_RESULTS: usize = 1000;
const SHUTDOWN_WAIT: Duration = Duration::from_secs(5);
const FORCE_SHUTDOWN_WAIT: Duration = Duration::from_secs(1);
fn should_mark_failed_connection(error: &Aria2Error) -> bool {
matches!(
error,
Aria2Error::Network(_)
| Aria2Error::Recoverable(
RecoverableError::TemporaryNetworkFailure { .. } | RecoverableError::Timeout
)
)
}
async fn mark_failed_connection(
dns_cache: &tokio::sync::Mutex<DnsCache>,
error: &Aria2Error,
context: &ConnectionContext,
use_async_dns: bool,
) {
if !use_async_dns || !should_mark_failed_connection(error) {
return;
}
let mut dns = dns_cache.lock().await;
dns.mark_bad_context(context);
if !dns.has_good_address(&context.endpoint) {
dns.remove_cached(context.endpoint.hostname(), context.endpoint.port());
}
}
pub struct EngineLoopContext {
pub group_man: Arc<RequestGroupMan>,
pub dns_cache: Arc<tokio::sync::Mutex<DnsCache>>,
pub auto_save: Option<Arc<tokio::sync::Mutex<AutoSaveCoordinator>>>,
pub auto_save_dirty_signal: Option<Arc<std::sync::atomic::AtomicBool>>,
pub event_hooks: Arc<DownloadEventHooks>,
pub file_alloc_man: Arc<tokio::sync::RwLock<FileAllocationMan>>,
pub keep_alive: bool,
pub server_stat_man: Arc<ServerStatMan>,
pub server_stat_max_age: Option<Duration>,
pub server_stat_save_path: Option<PathBuf>,
pub server_stat_save_interval: Option<Duration>,
pub server_stat_next_save: Option<Instant>,
pub global_limiter: Option<RateLimiter>,
#[cfg(feature = "bittorrent")]
pub public_tracker_catalog:
Arc<aria2_protocol::bittorrent::tracker::public_list::PublicTrackerList>,
#[cfg(feature = "bittorrent")]
pub bt_registry: Arc<std::sync::RwLock<crate::engine::bt_registry::BtRegistry>>,
#[cfg(feature = "bittorrent")]
pub bt_listener: Arc<crate::engine::bt_peer_listener::BtPeerListenerManager>,
#[cfg(feature = "bittorrent")]
pub lpd_manager: Arc<crate::engine::lpd_manager::LpdManager>,
}
type CommandGeneration = u64;
struct RunningDownload {
_handle: JoinHandle<()>,
shutdown: Option<CancellationToken>,
generation: CommandGeneration,
last_activity: Instant,
timeout: Option<Duration>,
}
fn mark_session_dirty(ctx: &EngineLoopContext) {
if let Some(signal) = &ctx.auto_save_dirty_signal {
signal.store(true, std::sync::atomic::Ordering::Release);
}
}
pub async fn run_engine_loop(
ctx: EngineLoopContext,
cmd_rx: mpsc::UnboundedReceiver<EngineCommand>,
shutdown_rx: tokio::sync::oneshot::Receiver<()>,
) {
run_engine_loop_with_receiver(
ctx,
EngineCommandReceiver::from_unbounded(cmd_rx),
shutdown_rx,
)
.await;
}
pub(crate) async fn run_engine_loop_with_receiver(
mut ctx: EngineLoopContext,
mut cmd_rx: EngineCommandReceiver,
mut shutdown_rx: tokio::sync::oneshot::Receiver<()>,
) {
info!("Engine loop started (event-driven dispatch)");
let mut running_downloads: Vec<(GroupId, RunningDownload)> = Vec::new();
let mut completed_generations: HashSet<CommandGeneration> = HashSet::new();
let mut next_generation: CommandGeneration = 1;
let mut halt_requested = false;
let mut force_halt_requested = false;
let mut shutdown_received = false;
let mut command_closed = false;
let mut completion_closed = false;
let mut first_pass = true;
let mut schedule_on_next_pass = false;
let (completion_tx, mut completion_rx) =
mpsc::unbounded_channel::<(GroupId, CommandGeneration, TaskResult)>();
loop {
let commands_processed = process_engine_commands(
&mut ctx,
&mut cmd_rx,
&mut running_downloads,
&mut halt_requested,
&mut force_halt_requested,
&completion_tx,
)
.await;
if first_pass {
promote_reserved_groups(
&ctx,
&mut running_downloads,
halt_requested,
force_halt_requested,
&mut next_generation,
&completion_tx,
);
}
let completions_processed = process_task_completions(
&ctx,
&mut completion_rx,
&mut running_downloads,
&mut completed_generations,
)
.await;
let demoted_gids = { ctx.group_man.remove_stopped_groups(Some(&ctx.event_hooks)) };
if !demoted_gids.is_empty() {
debug!("Demoted {} groups to stopped", demoted_gids.len());
mark_session_dirty(&ctx);
run_event_cleanup(&ctx).await;
}
let needs_follow_up_promotion = (!first_pass && commands_processed)
|| completions_processed
|| !demoted_gids.is_empty()
|| schedule_on_next_pass;
if needs_follow_up_promotion {
promote_reserved_groups(
&ctx,
&mut running_downloads,
halt_requested,
force_halt_requested,
&mut next_generation,
&completion_tx,
);
}
first_pass = false;
schedule_on_next_pass = false;
let all_done = ctx.group_man.download_finished() && running_downloads.is_empty();
let graceful_done = halt_requested && running_downloads.is_empty();
let force_done =
force_halt_requested && running_downloads.is_empty() && completion_rx.is_empty();
if force_done || graceful_done || (all_done && !ctx.keep_alive) {
if force_halt_requested {
info!("Force halt completed, shutting down engine");
} else if graceful_done {
info!("Graceful halt completed, engine shutting down");
} else {
info!("All downloads completed, engine shutting down");
}
break;
}
let maintenance_wait =
wait_for_deadline(next_maintenance_deadline(&ctx, &running_downloads).await);
tokio::pin!(maintenance_wait);
tokio::select! {
command = cmd_rx.recv(), if !command_closed => {
match command {
Ok(command) => {
let mut prefetched = PrefetchedEngineCommand {
first: Some(command),
receiver: &mut cmd_rx,
};
schedule_on_next_pass |= process_engine_commands(
&mut ctx,
&mut prefetched,
&mut running_downloads,
&mut halt_requested,
&mut force_halt_requested,
&completion_tx,
)
.await;
}
Err(EngineCommandTryRecvError::Closed) => command_closed = true,
Err(EngineCommandTryRecvError::Empty) => unreachable!(
"async engine command receive cannot return empty"
),
}
}
completion = completion_rx.recv(), if !completion_closed => {
match completion {
Some(completion) => {
let mut prefetched = PrefetchedCompletion {
first: Some(completion),
receiver: &mut completion_rx,
};
schedule_on_next_pass |= process_task_completions(
&ctx,
&mut prefetched,
&mut running_downloads,
&mut completed_generations,
)
.await;
}
None => completion_closed = true,
}
}
_ = &mut maintenance_wait => {
run_deadline_maintenance(&mut ctx, &mut running_downloads).await;
}
Ok(_) = &mut shutdown_rx, if !shutdown_received => {
shutdown_received = true;
info!("Shutdown signal received");
ctx.group_man
.halt_all(crate::request::request_group::HaltReason::ShutdownSignal);
halt_requested = true;
}
}
}
on_end_of_run(&ctx, &mut running_downloads).await;
info!("Engine loop exited");
}
mod commands;
mod completions;
mod maintenance;
use commands::{PrefetchedEngineCommand, process_engine_commands, promote_reserved_groups};
use completions::{PrefetchedCompletion, process_task_completions};
use maintenance::{
cancel_running_file_allocations, next_maintenance_deadline, on_end_of_run,
request_shutdown_and_wait, run_deadline_maintenance, run_event_cleanup, wait_for_deadline,
};
#[cfg(test)]
#[path = "../engine_loop_tests/mod.rs"]
mod tests;