only apply new semaphore to thumbnails

otherwise it blocks every other api query, including the large cover
art, since the tokio semaphore is fair
This commit is contained in:
Erica Z 2024-11-15 21:48:41 +01:00
parent 118e82d432
commit c00653829a
2 changed files with 6 additions and 10 deletions

View file

@ -64,8 +64,12 @@ use crate::subsonic;
use adw::{prelude::*, subclass::prelude::*}; use adw::{prelude::*, subclass::prelude::*};
use glib::Object; use glib::Object;
use gtk::{gdk, glib}; use gtk::{gdk, glib};
use tokio::sync::Semaphore;
use tracing::{event, Level}; use tracing::{event, Level};
// only fetch 20 thumbnails at a time
static SEM: Semaphore = Semaphore::const_new(20);
glib::wrapper! { glib::wrapper! {
pub struct Song(ObjectSubclass<imp::Song>); pub struct Song(ObjectSubclass<imp::Song>);
} }
@ -95,6 +99,8 @@ impl Song {
song.imp() song.imp()
.thumbnail_loading .thumbnail_loading
.replace(Some(glib::spawn_future_local(async move { .replace(Some(glib::spawn_future_local(async move {
let _permit = SEM.acquire().await.unwrap();
let bytes = match api let bytes = match api
.cover_art(&id, Some(50 * scale_factor)) // see pixel-size in .cover_art(&id, Some(50 * scale_factor)) // see pixel-size in
// play_queue_song.blp // play_queue_song.blp

View file

@ -6,8 +6,6 @@ pub use album_list::AlbumListType;
use adw::glib; use adw::glib;
use bytes::Bytes; use bytes::Bytes;
use rand::Rng; use rand::Rng;
use std::sync::Arc;
use tokio::sync::Semaphore;
use tracing::{event, Level}; use tracing::{event, Level};
fn runtime() -> &'static tokio::runtime::Runtime { fn runtime() -> &'static tokio::runtime::Runtime {
@ -58,7 +56,6 @@ impl From<reqwest::Error> for Error {
pub struct Client { pub struct Client {
client: reqwest::Client, client: reqwest::Client,
base_url: reqwest::Url, base_url: reqwest::Url,
sem: Arc<Semaphore>,
} }
fn random_salt(length: usize) -> String { fn random_salt(length: usize) -> String {
@ -109,7 +106,6 @@ impl Client {
.user_agent(crate::USER_AGENT) .user_agent(crate::USER_AGENT)
.build()?, .build()?,
base_url, 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 // 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 // sending back the result of the query from another thread
let future = request.send(); let future = request.send();
let sem = Arc::clone(&self.sem);
runtime().spawn(async move { runtime().spawn(async move {
let _permit = sem.acquire().await.unwrap();
// wrap this logic in a fn so we can use ? // wrap this logic in a fn so we can use ?
async fn perform<T: serde::de::DeserializeOwned + Send + 'static>( async fn perform<T: serde::de::DeserializeOwned + Send + 'static>(
response: Result<reqwest::Response, reqwest::Error>, response: Result<reqwest::Response, reqwest::Error>,
@ -202,10 +195,7 @@ impl Client {
let (sender, receiver) = async_channel::bounded(1); let (sender, receiver) = async_channel::bounded(1);
let future = self.client.get(self.cover_art_url(id, size)).send(); let future = self.client.get(self.cover_art_url(id, size)).send();
let sem = Arc::clone(&self.sem);
runtime().spawn(async move { runtime().spawn(async move {
let _permit = sem.acquire().await.unwrap();
async fn perform( async fn perform(
response: Result<reqwest::Response, reqwest::Error>, response: Result<reqwest::Response, reqwest::Error>,
) -> Result<Bytes, Error> { ) -> Result<Bytes, Error> {