REST API blocking async: Solution A, block_in_place

This commit is contained in:
junderw 2023-10-01 19:12:31 -07:00
parent 09a11b1c7a
commit ff4e4530e7
No known key found for this signature in database
GPG Key ID: B256185D3A971908

View File

@ -570,16 +570,18 @@ async fn run_server(config: Arc<Config>, query: Arc<Query>, 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());