move image decoding to blocking task

This commit is contained in:
Erica Z 2024-11-19 19:15:42 +01:00
parent 79f0e3d978
commit 5032b9d3a2

View file

@ -294,13 +294,19 @@ impl Client {
};
return error;
}
let image = ImageReader::new(std::io::BufReader::new(std::io::Cursor::new(
// 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)?;
.map_err(Error::ImageDecodeError)
})
.await
.expect("could not receive image from blocking tokio task")?;
let width = image.width();
let height = image.height();