From 07dcc5bef1ce2ff4347c6622240310c2f63e4967 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 11 Nov 2025 13:33:10 +0000 Subject: [PATCH] Make is_valid_pk() async to use tokio::time::sleep Changed is_valid_pk() from sync to async to properly wait for file creation without blocking. This is a breaking change but we're in active development. Changes: - is_valid_pk() signature: fn -> async fn - Replaced std::thread::sleep with tokio::time::sleep - Updated all 6 call sites in pmoplaylist to add .await: - WriteHandle::push() - WriteHandle::push_set() - ReadHandle::pop() - ReadHandle::peek() - ReadHandle::remaining() - ReadHandle::get_all() Benefits: - Non-blocking wait for file creation during ingestion - More idiomatic async Rust code - Better integration with tokio runtime --- pmocache/src/cache_trait.rs | 4 ++-- pmoplaylist/src/handle/read.rs | 8 ++++---- pmoplaylist/src/handle/write.rs | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pmocache/src/cache_trait.rs b/pmocache/src/cache_trait.rs index 179efe6c..0874414b 100644 --- a/pmocache/src/cache_trait.rs +++ b/pmocache/src/cache_trait.rs @@ -144,7 +144,7 @@ pub trait FileCache: Send + Sync { /// /// Ceci permet le progressive caching: les fichiers en cours de download sont acceptés /// dès que le prebuffer est atteint, sans attendre le marker de completion. - fn is_valid_pk(&self, pk: &str) -> bool { + async fn is_valid_pk(&self, pk: &str) -> bool { if self.get_database().get(pk, false).is_err() { tracing::debug!("is_valid_pk({}): DB entry not found", pk); return false; @@ -158,7 +158,7 @@ pub trait FileCache: Send + Sync { let mut attempts = 0; while !file_path.exists() && attempts < 100 { - std::thread::sleep(std::time::Duration::from_millis(10)); + tokio::time::sleep(tokio::time::Duration::from_millis(10)).await; attempts += 1; } diff --git a/pmoplaylist/src/handle/read.rs b/pmoplaylist/src/handle/read.rs index ffd762a6..1fa832fc 100644 --- a/pmoplaylist/src/handle/read.rs +++ b/pmoplaylist/src/handle/read.rs @@ -51,7 +51,7 @@ impl ReadHandle { // Vérifier validité dans le cache let cache = crate::manager::audio_cache()?; - if cache.is_valid_pk(&cache_pk) { + if cache.is_valid_pk(&cache_pk).await { // Valide, avancer le curseur et retourner self.cursor.fetch_add(1, Ordering::SeqCst); return Ok(Some(PlaylistTrack::new(cache_pk))); @@ -92,7 +92,7 @@ impl ReadHandle { Some(record) => { // Vérifier validité let cache = crate::manager::audio_cache()?; - if cache.is_valid_pk(&record.cache_pk) { + if cache.is_valid_pk(&record.cache_pk).await { Ok(Some(PlaylistTrack::new(record.cache_pk.clone()))) } else { Ok(None) @@ -125,7 +125,7 @@ impl ReadHandle { for i in pos..core.len() { if let Some(record) = core.get(i) { - if cache.is_valid_pk(&record.cache_pk) { + if cache.is_valid_pk(&record.cache_pk).await { count += 1; } } @@ -200,7 +200,7 @@ impl ReadHandle { }; // Vérifier validité - if !cache.is_valid_pk(&record.cache_pk) { + if !cache.is_valid_pk(&record.cache_pk).await { continue; } diff --git a/pmoplaylist/src/handle/write.rs b/pmoplaylist/src/handle/write.rs index 1469c905..48feb2f5 100644 --- a/pmoplaylist/src/handle/write.rs +++ b/pmoplaylist/src/handle/write.rs @@ -30,7 +30,7 @@ impl WriteHandle { // Vérifier que le pk existe dans le cache let cache = crate::manager::audio_cache()?; - if !cache.is_valid_pk(&cache_pk) { + if !cache.is_valid_pk(&cache_pk).await { return Err(crate::Error::CacheEntryNotFound(cache_pk)); } @@ -59,7 +59,7 @@ impl WriteHandle { // Vérifier tous les pks d'abord let cache = crate::manager::audio_cache()?; for pk in &cache_pks { - if !cache.is_valid_pk(pk) { + if !cache.is_valid_pk(pk).await { return Err(crate::Error::CacheEntryNotFound(pk.clone())); } }