Skip to content

Commit

Permalink
Revert "Always run callbacks as blocking in WSGI"
Browse files Browse the repository at this point in the history
This reverts commit 9f85301 as it seems to affect performance negatively
  • Loading branch information
gi0baro committed Nov 15, 2023
1 parent 2faec04 commit 5d49dfd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
9 changes: 8 additions & 1 deletion src/wsgi/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ fn run_callback(callback: PyObject, scope: Scope) -> PyResult<(i32, Vec<(String,
})
}

pub(crate) fn call_http(cb: CallbackWrapper, scope: Scope) -> JoinHandle<PyResult<(i32, Vec<(String, String)>, Body)>> {
pub(crate) fn call_rtb_http(cb: CallbackWrapper, scope: Scope) -> PyResult<(i32, Vec<(String, String)>, Body)> {
run_callback(cb.callback, scope)
}

pub(crate) fn call_rtt_http(
cb: CallbackWrapper,
scope: Scope,
) -> JoinHandle<PyResult<(i32, Vec<(String, String)>, Body)>> {
tokio::task::spawn_blocking(move || run_callback(cb.callback, scope))
}
27 changes: 23 additions & 4 deletions src/wsgi/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use hyper::{
};
use std::net::SocketAddr;

use super::{callbacks::call_http, types::WSGIScope as Scope};
use super::{
callbacks::{call_rtb_http, call_rtt_http},
types::WSGIScope as Scope,
};
use crate::{
callbacks::CallbackWrapper,
http::{response_500, HV_SERVER},
Expand All @@ -26,16 +29,15 @@ fn build_response(status: i32, pyheaders: Vec<(String, String)>, body: Body) ->
res
}

pub(crate) async fn handle(
pub(crate) async fn handle_rtt(
_rt: RuntimeRef,
callback: CallbackWrapper,
server_addr: SocketAddr,
client_addr: SocketAddr,
req: Request<Body>,
scheme: &str,
) -> Response<Body> {
let scope = Scope::new(scheme, server_addr, client_addr, req).await;
if let Ok(res) = call_http(callback, scope).await {
if let Ok(res) = call_rtt_http(callback, Scope::new(scheme, server_addr, client_addr, req).await).await {
if let Ok((status, headers, body)) = res {
return build_response(status, headers, body);
}
Expand All @@ -45,3 +47,20 @@ pub(crate) async fn handle(
}
response_500()
}

pub(crate) async fn handle_rtb(
_rt: RuntimeRef,
callback: CallbackWrapper,
server_addr: SocketAddr,
client_addr: SocketAddr,
req: Request<Body>,
scheme: &str,
) -> Response<Body> {
match call_rtb_http(callback, Scope::new(scheme, server_addr, client_addr, req).await) {
Ok((status, headers, body)) => build_response(status, headers, body),
_ => {
log::warn!("Application callable raised an exception");
response_500()
}
}
}
10 changes: 5 additions & 5 deletions src/wsgi/serve.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use pyo3::prelude::*;

use super::http::handle;
use super::http::{handle_rtb, handle_rtt};
use crate::workers::{serve_rth, serve_rth_ssl, serve_wth, serve_wth_ssl, WorkerConfig};

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

impl WSGIWorker {
serve_rth!(_serve_rth, handle);
serve_wth!(_serve_wth, handle);
serve_rth_ssl!(_serve_rth_ssl, handle);
serve_wth_ssl!(_serve_wth_ssl, handle);
serve_rth!(_serve_rth, handle_rtb);
serve_wth!(_serve_wth, handle_rtt);
serve_rth_ssl!(_serve_rth_ssl, handle_rtb);
serve_wth_ssl!(_serve_wth_ssl, handle_rtt);
}

#[pymethods]
Expand Down

0 comments on commit 5d49dfd

Please sign in to comment.