From 5032b9d3a24a5859ce92a128ec6dddfc2bc3ccc6 Mon Sep 17 00:00:00 2001 From: Erica Z Date: Tue, 19 Nov 2024 19:15:42 +0100 Subject: [PATCH] move image decoding to blocking task --- src/subsonic.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/subsonic.rs b/src/subsonic.rs index 988e18d..3f51337 100644 --- a/src/subsonic.rs +++ b/src/subsonic.rs @@ -294,13 +294,19 @@ impl Client { }; return error; } - let image = ImageReader::new(std::io::BufReader::new(std::io::Cursor::new( - byteresponse.bytes, - ))) - .with_guessed_format() - .map_err(|e| Error::ImageDecodeError(image::ImageError::IoError(e)))? - .decode() - .map_err(Error::ImageDecodeError)?; + // decoding the image can take up a good chunk of cpu time, so spawn a blocking task + let image = runtime() + .spawn_blocking(move || { + ImageReader::new(std::io::BufReader::new(std::io::Cursor::new( + byteresponse.bytes, + ))) + .with_guessed_format() + .map_err(|e| Error::ImageDecodeError(image::ImageError::IoError(e)))? + .decode() + .map_err(Error::ImageDecodeError) + }) + .await + .expect("could not receive image from blocking tokio task")?; let width = image.width(); let height = image.height();