From df750799b0e5881a3e479079a2350c28adb4ff7f Mon Sep 17 00:00:00 2001 From: Thang Pham Date: Wed, 2 Aug 2023 22:43:17 -0400 Subject: [PATCH 1/3] increase `CACHE_DURATION` to `3h` --- spotify_player/src/state/data.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spotify_player/src/state/data.rs b/spotify_player/src/state/data.rs index 2bf96999..111f62c2 100644 --- a/spotify_player/src/state/data.rs +++ b/spotify_player/src/state/data.rs @@ -6,9 +6,9 @@ use super::model::*; pub type DataReadGuard<'a> = parking_lot::RwLockReadGuard<'a, AppData>; -// cache duration, which is default to be 1h +// cache duration, which is default to be 3h pub static CACHE_DURATION: Lazy = - Lazy::new(|| std::time::Duration::from_secs(60 * 60)); + Lazy::new(|| std::time::Duration::from_secs(60 * 60 * 3)); #[derive(Default)] /// the application's data From 5b5cf89e20bd5f274b03737e93154d65c3cd5134 Mon Sep 17 00:00:00 2001 From: Thang Pham Date: Wed, 2 Aug 2023 22:44:25 -0400 Subject: [PATCH 2/3] re-request context data if not found in cache --- spotify_player/src/client/handlers.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/spotify_player/src/client/handlers.rs b/spotify_player/src/client/handlers.rs index e89007df..069f58b5 100644 --- a/spotify_player/src/client/handlers.rs +++ b/spotify_player/src/client/handlers.rs @@ -175,17 +175,18 @@ pub async fn start_player_event_watchers( *page_state = None; } } + } - // request new context's data if not found in memory - if let Some(id) = id { - if !state.data.read().caches.context.contains_key(&id.uri()) { - client_pub - .send(ClientRequest::GetContext(id.clone())) - .unwrap_or_default(); - } + // request new context's data if not found in memory + if let Some(id) = id { + if !state.data.read().caches.context.contains_key(&id.uri()) { + client_pub + .send(ClientRequest::GetContext(id.clone())) + .unwrap_or_default(); } } } + #[cfg(feature = "lyric-finder")] PageState::Lyric { track, From 099863ffd95ac7faff469c942c07be772ea633c5 Mon Sep 17 00:00:00 2001 From: Thang Pham Date: Wed, 2 Aug 2023 22:58:23 -0400 Subject: [PATCH 3/3] add timer to avoid making multiple requests --- spotify_player/src/client/handlers.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/spotify_player/src/client/handlers.rs b/spotify_player/src/client/handlers.rs index 069f58b5..76878465 100644 --- a/spotify_player/src/client/handlers.rs +++ b/spotify_player/src/client/handlers.rs @@ -141,6 +141,10 @@ pub async fn start_player_event_watchers( // An event is categorized as "high-frequency" when // - we want to handle it "immediately" to prevent users from experiencing a noticable delay let refresh_duration = std::time::Duration::from_millis(200); // frequency = 5Hz + + // This timer is used to avoid making multiple `GetContext` requests in a short period of time. + let mut last_request_timer = std::time::Instant::now(); + loop { tokio::time::sleep(refresh_duration).await; @@ -179,7 +183,10 @@ pub async fn start_player_event_watchers( // request new context's data if not found in memory if let Some(id) = id { - if !state.data.read().caches.context.contains_key(&id.uri()) { + if !state.data.read().caches.context.contains_key(&id.uri()) + && last_request_timer.elapsed() >= std::time::Duration::from_secs(1) + { + last_request_timer = std::time::Instant::now(); client_pub .send(ClientRequest::GetContext(id.clone())) .unwrap_or_default();