From c00653829a34d7fa9f1351d3c89eaac32fd45111 Mon Sep 17 00:00:00 2001 From: Erica Z Date: Fri, 15 Nov 2024 21:48:41 +0100 Subject: [PATCH] only apply new semaphore to thumbnails otherwise it blocks every other api query, including the large cover art, since the tokio semaphore is fair --- src/model/song.rs | 6 ++++++ src/subsonic.rs | 10 ---------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/model/song.rs b/src/model/song.rs index 8ec818d..e7f6a7f 100644 --- a/src/model/song.rs +++ b/src/model/song.rs @@ -64,8 +64,12 @@ use crate::subsonic; use adw::{prelude::*, subclass::prelude::*}; use glib::Object; use gtk::{gdk, glib}; +use tokio::sync::Semaphore; use tracing::{event, Level}; +// only fetch 20 thumbnails at a time +static SEM: Semaphore = Semaphore::const_new(20); + glib::wrapper! { pub struct Song(ObjectSubclass); } @@ -95,6 +99,8 @@ impl Song { song.imp() .thumbnail_loading .replace(Some(glib::spawn_future_local(async move { + let _permit = SEM.acquire().await.unwrap(); + let bytes = match api .cover_art(&id, Some(50 * scale_factor)) // see pixel-size in // play_queue_song.blp diff --git a/src/subsonic.rs b/src/subsonic.rs index 2ffeca6..57d2300 100644 --- a/src/subsonic.rs +++ b/src/subsonic.rs @@ -6,8 +6,6 @@ pub use album_list::AlbumListType; use adw::glib; use bytes::Bytes; use rand::Rng; -use std::sync::Arc; -use tokio::sync::Semaphore; use tracing::{event, Level}; fn runtime() -> &'static tokio::runtime::Runtime { @@ -58,7 +56,6 @@ impl From for Error { pub struct Client { client: reqwest::Client, base_url: reqwest::Url, - sem: Arc, } fn random_salt(length: usize) -> String { @@ -109,7 +106,6 @@ impl Client { .user_agent(crate::USER_AGENT) .build()?, base_url, - sem: Arc::new(Semaphore::new(20)), // 20 requests at one time }) } @@ -125,10 +121,7 @@ impl Client { // note that this is why those silly bounds on T are needed, because we're // sending back the result of the query from another thread let future = request.send(); - let sem = Arc::clone(&self.sem); runtime().spawn(async move { - let _permit = sem.acquire().await.unwrap(); - // wrap this logic in a fn so we can use ? async fn perform( response: Result, @@ -202,10 +195,7 @@ impl Client { let (sender, receiver) = async_channel::bounded(1); let future = self.client.get(self.cover_art_url(id, size)).send(); - let sem = Arc::clone(&self.sem); runtime().spawn(async move { - let _permit = sem.acquire().await.unwrap(); - async fn perform( response: Result, ) -> Result {