diff --git a/fluxa-streaming-engine/src/torrent_engine.rs b/fluxa-streaming-engine/src/torrent_engine.rs index aa8a506..eda44ab 100644 --- a/fluxa-streaming-engine/src/torrent_engine.rs +++ b/fluxa-streaming-engine/src/torrent_engine.rs @@ -61,6 +61,7 @@ struct EngineState { output_dir: PathBuf, preload_size: Arc>, known_links: Arc>>, + prioritized_files: Arc>>, stream_progress: Arc>>, access_token: Arc, // Serializes the check-then-add sequence in ensure_torrent so two @@ -190,6 +191,7 @@ pub fn start_torrent_server( output_dir: thread_cache_dir, preload_size: Arc::new(Mutex::new(10 * 1024 * 1024)), known_links: Arc::new(Mutex::new(HashMap::new())), + prioritized_files: Arc::new(Mutex::new(HashMap::new())), stream_progress: Arc::new(Mutex::new(HashMap::new())), access_token: Arc::new(thread_access_token), add_lock: Arc::new(AsyncMutex::new(())), @@ -801,6 +803,20 @@ fn largest_file_id(details: &TorrentDetailsResponse) -> Option { } async fn prioritize_stream_file(state: &EngineState, torrent_id: usize, file_id: usize) { + let should_update = state + .prioritized_files + .lock() + .map(|mut files| match files.get(&torrent_id) { + Some(current) if *current == file_id => false, + _ => { + files.insert(torrent_id, file_id); + true + } + }) + .unwrap_or(true); + if !should_update { + return; + } let only_files = HashSet::from([file_id]); let _ = state .api