From ff4e4530e764cc8de33769206d255b7940fcc17e Mon Sep 17 00:00:00 2001 From: junderw Date: Sun, 1 Oct 2023 19:12:31 -0700 Subject: [PATCH] REST API blocking async: Solution A, block_in_place --- src/rest.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/rest.rs b/src/rest.rs index 5656739..1fbb656 100644 --- a/src/rest.rs +++ b/src/rest.rs @@ -570,16 +570,18 @@ async fn run_server(config: Arc, query: Arc, rx: oneshot::Receive let uri = req.uri().clone(); let body = hyper::body::to_bytes(req.into_body()).await?; - let mut resp = handle_request(method, uri, body, &query, &config) - .unwrap_or_else(|err| { - warn!("{:?}", err); - Response::builder() - .status(err.0) - .header("Content-Type", "text/plain") - .header("X-Powered-By", &**VERSION_STRING) - .body(Body::from(err.1)) - .unwrap() - }); + let mut resp = tokio::task::block_in_place(|| { + handle_request(method, uri, body, &query, &config) + }) + .unwrap_or_else(|err| { + warn!("{:?}", err); + Response::builder() + .status(err.0) + .header("Content-Type", "text/plain") + .header("X-Powered-By", &**VERSION_STRING) + .body(Body::from(err.1)) + .unwrap() + }); if let Some(ref origins) = config.cors { resp.headers_mut() .insert("Access-Control-Allow-Origin", origins.parse().unwrap());