From 031df8da4d0113111266785a7009e22cd12b56a8 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sat, 18 Nov 2023 16:32:28 +0000 Subject: [PATCH] Let `spawn_blocking` handle returning its own value This means one fewer thing to await on, and spin up inside a request. It _might_ be faster, who knows, but it can't hurt. --- src/main.rs | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4eadbd6..3e50b9e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ use std::error::Error; compile_error!("feature \"reqwest-native-tls\" or \"reqwest-rustls\" must be set for proxy to have TLS support"); #[cfg(any(feature = "webp", feature = "avif"))] -use tokio::{sync::oneshot, task::spawn_blocking}; +use tokio::task::spawn_blocking; #[cfg(feature = "mimalloc")] #[global_allocator] @@ -231,8 +231,7 @@ async fn index(req: HttpRequest) -> Result> { && (content_type == "image/webp" || content_type == "image/jpeg" && avif) { let resp_bytes = resp.bytes().await.unwrap(); - let (tx, rx) = oneshot::channel::<(Vec, &'static str)>(); - spawn_blocking(|| { + let (body, content_type) = spawn_blocking(|| { use ravif::{Encoder, Img}; use rgb::FromSlice; @@ -252,12 +251,11 @@ async fn index(req: HttpRequest) -> Result> { .encode_rgb(buffer); if let Ok(res) = res { - tx.send((res.avif_file.to_vec(), "image/avif")).unwrap(); + (res.avif_file.to_vec(), "image/avif") } else { - tx.send((resp_bytes.into(), "image/jpeg")).unwrap(); + (resp_bytes.into(), "image/jpeg") } - }); - let (body, content_type) = rx.await.unwrap(); + }).await.unwrap(); response.content_type(content_type); return Ok(response.body(body)); } @@ -265,8 +263,7 @@ async fn index(req: HttpRequest) -> Result> { #[cfg(feature = "webp")] if !disallow_image_transcoding && content_type == "image/jpeg" { let resp_bytes = resp.bytes().await.unwrap(); - let (tx, rx) = oneshot::channel::<(Vec, &'static str)>(); - spawn_blocking(|| { + let (body, content_type) = spawn_blocking(|| { use libwebp_sys::{WebPEncodeRGB, WebPFree}; let image = image::load_from_memory(&resp_bytes).unwrap(); @@ -294,13 +291,12 @@ async fn index(req: HttpRequest) -> Result> { }; if bytes.len() < resp_bytes.len() { - tx.send((bytes, "image/webp")).unwrap(); - return; + (bytes, "image/webp") + } else { + (resp_bytes.into(), "image/jpeg") } - tx.send((resp_bytes.into(), "image/jpeg")).unwrap(); - }); - let (body, content_type) = rx.await.unwrap(); + }).await.unwrap(); response.content_type(content_type); return Ok(response.body(body)); }