From 9f97a2cae3ffdf95b6a762ecce7ff0a47142b902 Mon Sep 17 00:00:00 2001 From: Erica Z Date: Fri, 1 Nov 2024 09:58:03 +0100 Subject: [PATCH] cordon off the tokio runtime to the subsonic client --- src/main.rs | 9 +-------- src/subsonic.rs | 28 +++++++++++++++++++++++----- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index ca2c46d..d0c38a8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,13 +30,6 @@ pub mod mpv; #[link(name = "soup-3.0")] extern "C" {} -pub fn runtime() -> &'static tokio::runtime::Runtime { - static RUNTIME: std::sync::OnceLock = std::sync::OnceLock::new(); - RUNTIME.get_or_init(|| { - tokio::runtime::Runtime::new().expect("could not initialize the tokio runtime") - }) -} - fn main() -> glib::ExitCode { gio::resources_register_include!("audrey.gresource").expect("could not register resources"); @@ -52,7 +45,7 @@ fn main() -> glib::ExitCode { let app = Application::new(); // smol test for the subsonic client - runtime().spawn(async { + glib::spawn_future_local(async { let keyring = oo7::Keyring::new().await.unwrap(); let attributes = vec![("xdg:schema", APP_ID)]; let items = keyring.search_items(&attributes).await.unwrap(); diff --git a/src/subsonic.rs b/src/subsonic.rs index ce0e17b..00bc6ff 100644 --- a/src/subsonic.rs +++ b/src/subsonic.rs @@ -1,5 +1,12 @@ mod schema; +fn runtime() -> &'static tokio::runtime::Runtime { + static RUNTIME: std::sync::OnceLock = std::sync::OnceLock::new(); + RUNTIME.get_or_init(|| { + tokio::runtime::Runtime::new().expect("could not initialize the tokio runtime") + }) +} + impl schema::SubsonicResponse { fn fixup(self) -> Result { match self { @@ -100,11 +107,22 @@ impl Client { .unwrap_or_else(|_| unsafe { std::hint::unreachable_unchecked() }) .extend(path); - self.client - .get(url) - .query(query) - .send() - .await? + // FIXME: is an entire channel per request overkill? maybe pool them? + let (sender, receiver) = async_channel::bounded(1); + + let future = self.client.get(url).query(query).send(); + runtime().spawn(async move { + let response = future.await; + sender + .send(response) + .await + .expect("could not send subsonic response back to the main loop"); + }); + + receiver + .recv() + .await + .expect("could not receive subsonic response from tokio")? .error_for_status()? .json::>() .await?