feat(downloads): limit concurrent downloads to 3
This commit is contained in:
1 parent
6361f2e3df
commit
540844de9f
3 files changed
+27
-7
No files matched your search
@@ -50,25 +50,43 @@ pub async fn download_track(
|
||||
db.insert_track(&track_info, &file_path_str)?;
|
||||
}
|
||||
|
||||
// Emit started event
|
||||
let _ = app_handle.emit("download:started", serde_json::json!({
|
||||
"trackId": track_id
|
||||
}));
|
||||
|
||||
// Clone what we need for the spawn
|
||||
let client = state.client.clone();
|
||||
let downloader = cache_state.downloader.clone();
|
||||
let db = cache_state.db.clone();
|
||||
let app = app_handle.clone();
|
||||
let semaphore = cache_state.download_semaphore.clone();
|
||||
|
||||
// Spawn download task
|
||||
tokio::spawn(async move {
|
||||
let _permit = match semaphore.acquire_owned().await {
|
||||
Ok(permit) => permit,
|
||||
Err(err) => {
|
||||
log::error!("Failed to acquire download slot for track {}: {}", track_id, err);
|
||||
let db_guard = db.lock().await;
|
||||
let _ = db_guard.update_status(
|
||||
track_id,
|
||||
DownloadStatus::Failed,
|
||||
Some("Failed to start download"),
|
||||
);
|
||||
let _ = app.emit("download:failed", serde_json::json!({
|
||||
"trackId": track_id,
|
||||
"error": "Failed to acquire download slot"
|
||||
}));
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// Update status to downloading
|
||||
{
|
||||
let db_guard = db.lock().await;
|
||||
let _ = db_guard.update_status(track_id, DownloadStatus::Downloading, None);
|
||||
}
|
||||
|
||||
let _ = app.emit("download:started", serde_json::json!({
|
||||
"trackId": track_id
|
||||
}));
|
||||
|
||||
// Get stream URL
|
||||
let stream_url = {
|
||||
let client_guard = client.lock().await;
|
||||
|
||||
@@ -12,7 +12,7 @@ pub mod downloader;
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::sync::{Mutex, Semaphore};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub use db::DownloadCacheDb;
|
||||
@@ -115,6 +115,7 @@ pub struct DownloadCacheState {
|
||||
pub cache_dir: PathBuf,
|
||||
/// Cache limit in bytes (None = unlimited)
|
||||
pub limit_bytes: Arc<Mutex<Option<u64>>>,
|
||||
pub download_semaphore: Arc<Semaphore>,
|
||||
}
|
||||
|
||||
impl DownloadCacheState {
|
||||
@@ -144,6 +145,7 @@ impl DownloadCacheState {
|
||||
downloader: Arc::new(Downloader::new()),
|
||||
cache_dir: cache_dir.clone(),
|
||||
limit_bytes: Arc::new(Mutex::new(default_limit)),
|
||||
download_semaphore: Arc::new(Semaphore::new(3)),
|
||||
};
|
||||
|
||||
log::info!("Download cache initialized at: {:?}", cache_dir);
|
||||
|
||||
@@ -88,7 +88,7 @@ export async function startDownloadEventListeners(): Promise<void> {
|
||||
try {
|
||||
const unlistenStarted = await listen<{ trackId: number }>('download:started', (event) => {
|
||||
console.log('Download started:', event.payload.trackId);
|
||||
setDownloadState(event.payload.trackId, { status: 'queued', progress: 0 });
|
||||
setDownloadState(event.payload.trackId, { status: 'downloading', progress: 0 });
|
||||
});
|
||||
|
||||
const unlistenProgress = await listen<{
|
||||
|
||||
Reference in new issue
Block a user