✨ 添加后端内部轮询
This commit is contained in:
@@ -17,6 +17,7 @@ pub struct DownloadManager {
|
||||
aria2_port: Arc<Mutex<u16>>,
|
||||
aria2_pid: Arc<Mutex<Option<u32>>>,
|
||||
installing: Arc<AtomicBool>,
|
||||
last_get_downloads: Arc<Mutex<Option<std::time::Instant>>>,
|
||||
}
|
||||
|
||||
impl DownloadManager {
|
||||
@@ -28,11 +29,40 @@ impl DownloadManager {
|
||||
aria2_port: Arc::new(Mutex::new(5144)),
|
||||
aria2_pid: Arc::new(Mutex::new(None)),
|
||||
installing: Arc::new(AtomicBool::new(false)), // 初始化为 false
|
||||
last_get_downloads: Arc::new(Mutex::new(None)),
|
||||
}
|
||||
}
|
||||
|
||||
// 获取所有下载任务
|
||||
pub async fn get_downloads(&self) -> Result<Vec<DownloadTaskResponse>, String> {
|
||||
// 检查是否需要更新最后调用时间,并在作用域内立即释放锁
|
||||
let should_skip_update = {
|
||||
if let Ok(last_call) = self.last_get_downloads.lock() {
|
||||
if let Some(last_time) = *last_call {
|
||||
last_time.elapsed().as_secs_f32() < 1.0
|
||||
} else {
|
||||
false
|
||||
}
|
||||
} else {
|
||||
false
|
||||
}
|
||||
};
|
||||
|
||||
if should_skip_update {
|
||||
return self.get_downloads_internal().await;
|
||||
}
|
||||
|
||||
// 更新最后调用时间,并在作用域内立即释放锁
|
||||
{
|
||||
if let Ok(mut last_call) = self.last_get_downloads.lock() {
|
||||
*last_call = Some(std::time::Instant::now());
|
||||
}
|
||||
}
|
||||
|
||||
self.get_downloads_internal().await
|
||||
}
|
||||
|
||||
// 获取所有下载任务
|
||||
async fn get_downloads_internal(&self) -> Result<Vec<DownloadTaskResponse>, String> {
|
||||
// 获取队列中的任务信息并立即克隆所需数据,然后释放锁
|
||||
let tasks_clone: Vec<DownloadTask>;
|
||||
let aria2_started;
|
||||
@@ -502,4 +532,13 @@ impl DownloadManager {
|
||||
Err(error_msg)
|
||||
}
|
||||
}
|
||||
|
||||
// 检查是否有下载任务
|
||||
pub fn has_downloads(&self) -> bool {
|
||||
if let Ok(downloads) = self.download_queue.lock() {
|
||||
!downloads.is_empty()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user