Skip to content

Commit 5d49dfd

Browse files
committed
Revert "Always run callbacks as blocking in WSGI"
This reverts commit 9f85301 as it seems to affect performance negatively
1 parent 2faec04 commit 5d49dfd

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

src/wsgi/callbacks.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ fn run_callback(callback: PyObject, scope: Scope) -> PyResult<(i32, Vec<(String,
2929
})
3030
}
3131

32-
pub(crate) fn call_http(cb: CallbackWrapper, scope: Scope) -> JoinHandle<PyResult<(i32, Vec<(String, String)>, Body)>> {
32+
pub(crate) fn call_rtb_http(cb: CallbackWrapper, scope: Scope) -> PyResult<(i32, Vec<(String, String)>, Body)> {
33+
run_callback(cb.callback, scope)
34+
}
35+
36+
pub(crate) fn call_rtt_http(
37+
cb: CallbackWrapper,
38+
scope: Scope,
39+
) -> JoinHandle<PyResult<(i32, Vec<(String, String)>, Body)>> {
3340
tokio::task::spawn_blocking(move || run_callback(cb.callback, scope))
3441
}

src/wsgi/http.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use hyper::{
44
};
55
use std::net::SocketAddr;
66

7-
use super::{callbacks::call_http, types::WSGIScope as Scope};
7+
use super::{
8+
callbacks::{call_rtb_http, call_rtt_http},
9+
types::WSGIScope as Scope,
10+
};
811
use crate::{
912
callbacks::CallbackWrapper,
1013
http::{response_500, HV_SERVER},
@@ -26,16 +29,15 @@ fn build_response(status: i32, pyheaders: Vec<(String, String)>, body: Body) ->
2629
res
2730
}
2831

29-
pub(crate) async fn handle(
32+
pub(crate) async fn handle_rtt(
3033
_rt: RuntimeRef,
3134
callback: CallbackWrapper,
3235
server_addr: SocketAddr,
3336
client_addr: SocketAddr,
3437
req: Request<Body>,
3538
scheme: &str,
3639
) -> Response<Body> {
37-
let scope = Scope::new(scheme, server_addr, client_addr, req).await;
38-
if let Ok(res) = call_http(callback, scope).await {
40+
if let Ok(res) = call_rtt_http(callback, Scope::new(scheme, server_addr, client_addr, req).await).await {
3941
if let Ok((status, headers, body)) = res {
4042
return build_response(status, headers, body);
4143
}
@@ -45,3 +47,20 @@ pub(crate) async fn handle(
4547
}
4648
response_500()
4749
}
50+
51+
pub(crate) async fn handle_rtb(
52+
_rt: RuntimeRef,
53+
callback: CallbackWrapper,
54+
server_addr: SocketAddr,
55+
client_addr: SocketAddr,
56+
req: Request<Body>,
57+
scheme: &str,
58+
) -> Response<Body> {
59+
match call_rtb_http(callback, Scope::new(scheme, server_addr, client_addr, req).await) {
60+
Ok((status, headers, body)) => build_response(status, headers, body),
61+
_ => {
62+
log::warn!("Application callable raised an exception");
63+
response_500()
64+
}
65+
}
66+
}

src/wsgi/serve.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use pyo3::prelude::*;
22

3-
use super::http::handle;
3+
use super::http::{handle_rtb, handle_rtt};
44
use crate::workers::{serve_rth, serve_rth_ssl, serve_wth, serve_wth_ssl, WorkerConfig};
55

66
#[pyclass(module = "granian._granian")]
@@ -9,10 +9,10 @@ pub struct WSGIWorker {
99
}
1010

1111
impl WSGIWorker {
12-
serve_rth!(_serve_rth, handle);
13-
serve_wth!(_serve_wth, handle);
14-
serve_rth_ssl!(_serve_rth_ssl, handle);
15-
serve_wth_ssl!(_serve_wth_ssl, handle);
12+
serve_rth!(_serve_rth, handle_rtb);
13+
serve_wth!(_serve_wth, handle_rtt);
14+
serve_rth_ssl!(_serve_rth_ssl, handle_rtb);
15+
serve_wth_ssl!(_serve_wth_ssl, handle_rtt);
1616
}
1717

1818
#[pymethods]

0 commit comments

Comments
 (0)