mirror of
https://github.com/TeamPiped/piped-proxy.git
synced 2024-08-14 23:50:45 +00:00
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.
This commit is contained in:
parent
9cda613cd2
commit
031df8da4d
1 changed files with 10 additions and 14 deletions
24
src/main.rs
24
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");
|
compile_error!("feature \"reqwest-native-tls\" or \"reqwest-rustls\" must be set for proxy to have TLS support");
|
||||||
|
|
||||||
#[cfg(any(feature = "webp", feature = "avif"))]
|
#[cfg(any(feature = "webp", feature = "avif"))]
|
||||||
use tokio::{sync::oneshot, task::spawn_blocking};
|
use tokio::task::spawn_blocking;
|
||||||
|
|
||||||
#[cfg(feature = "mimalloc")]
|
#[cfg(feature = "mimalloc")]
|
||||||
#[global_allocator]
|
#[global_allocator]
|
||||||
|
@ -231,8 +231,7 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
|
||||||
&& (content_type == "image/webp" || content_type == "image/jpeg" && avif)
|
&& (content_type == "image/webp" || content_type == "image/jpeg" && avif)
|
||||||
{
|
{
|
||||||
let resp_bytes = resp.bytes().await.unwrap();
|
let resp_bytes = resp.bytes().await.unwrap();
|
||||||
let (tx, rx) = oneshot::channel::<(Vec<u8>, &'static str)>();
|
let (body, content_type) = spawn_blocking(|| {
|
||||||
spawn_blocking(|| {
|
|
||||||
use ravif::{Encoder, Img};
|
use ravif::{Encoder, Img};
|
||||||
use rgb::FromSlice;
|
use rgb::FromSlice;
|
||||||
|
|
||||||
|
@ -252,12 +251,11 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
|
||||||
.encode_rgb(buffer);
|
.encode_rgb(buffer);
|
||||||
|
|
||||||
if let Ok(res) = res {
|
if let Ok(res) = res {
|
||||||
tx.send((res.avif_file.to_vec(), "image/avif")).unwrap();
|
(res.avif_file.to_vec(), "image/avif")
|
||||||
} else {
|
} else {
|
||||||
tx.send((resp_bytes.into(), "image/jpeg")).unwrap();
|
(resp_bytes.into(), "image/jpeg")
|
||||||
}
|
}
|
||||||
});
|
}).await.unwrap();
|
||||||
let (body, content_type) = rx.await.unwrap();
|
|
||||||
response.content_type(content_type);
|
response.content_type(content_type);
|
||||||
return Ok(response.body(body));
|
return Ok(response.body(body));
|
||||||
}
|
}
|
||||||
|
@ -265,8 +263,7 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
|
||||||
#[cfg(feature = "webp")]
|
#[cfg(feature = "webp")]
|
||||||
if !disallow_image_transcoding && content_type == "image/jpeg" {
|
if !disallow_image_transcoding && content_type == "image/jpeg" {
|
||||||
let resp_bytes = resp.bytes().await.unwrap();
|
let resp_bytes = resp.bytes().await.unwrap();
|
||||||
let (tx, rx) = oneshot::channel::<(Vec<u8>, &'static str)>();
|
let (body, content_type) = spawn_blocking(|| {
|
||||||
spawn_blocking(|| {
|
|
||||||
use libwebp_sys::{WebPEncodeRGB, WebPFree};
|
use libwebp_sys::{WebPEncodeRGB, WebPFree};
|
||||||
|
|
||||||
let image = image::load_from_memory(&resp_bytes).unwrap();
|
let image = image::load_from_memory(&resp_bytes).unwrap();
|
||||||
|
@ -294,13 +291,12 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
|
||||||
};
|
};
|
||||||
|
|
||||||
if bytes.len() < resp_bytes.len() {
|
if bytes.len() < resp_bytes.len() {
|
||||||
tx.send((bytes, "image/webp")).unwrap();
|
(bytes, "image/webp")
|
||||||
return;
|
} else {
|
||||||
|
(resp_bytes.into(), "image/jpeg")
|
||||||
}
|
}
|
||||||
|
|
||||||
tx.send((resp_bytes.into(), "image/jpeg")).unwrap();
|
}).await.unwrap();
|
||||||
});
|
|
||||||
let (body, content_type) = rx.await.unwrap();
|
|
||||||
response.content_type(content_type);
|
response.content_type(content_type);
|
||||||
return Ok(response.body(body));
|
return Ok(response.body(body));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue