diff --git a/src/dispatch.rs b/src/dispatch.rs index f3c56fe..d2082ea 100644 --- a/src/dispatch.rs +++ b/src/dispatch.rs @@ -167,10 +167,9 @@ fn send_python_error_sync( path: &str, err: PyErr, scope: Option<&pyo3::Bound<'_, PyAny>>, - state: Option<&std::sync::Arc>>, + snapshot: Option<&HotSnapshot>, ) -> PyResult<()> { - if let (Some(sc), Some(st)) = (scope, state) { - let snap = st.read().hot_snapshot(); + if let (Some(sc), Some(snap)) = (scope, snapshot) { for (exc_type, handler, is_async) in snap.exception_handlers.iter().rev() { if let Ok(exc_obj) = err.clone_ref(py).into_bound_py_any(py) { if exc_obj.is_instance(exc_type.bind(py)).unwrap_or(false) { @@ -220,7 +219,6 @@ fn run_trivial_sync_route( entry: &RouteEntry, params: &matchit::Params<'_, '_>, scope: &pyo3::Bound<'_, PyAny>, - state: &std::sync::Arc>, snapshot: &HotSnapshot, ) -> PyResult<()> { let _ = protocol.setattr( @@ -240,7 +238,7 @@ fn run_trivial_sync_route( path, e, Some(scope), - Some(state), + Some(snapshot), ); } } @@ -260,7 +258,7 @@ fn run_trivial_sync_route( path, e, Some(scope), - Some(state), + Some(snapshot), ); } } @@ -274,7 +272,7 @@ fn run_trivial_sync_route( path, e, Some(scope), - Some(state), + Some(snapshot), ); } } @@ -282,17 +280,33 @@ fn run_trivial_sync_route( let mapped = match map_handler_return(py, &out) { Ok(m) => m, Err(e) => { - return send_python_error_sync(py, protocol, method, path, e, Some(scope), Some(state)); + return send_python_error_sync( + py, + protocol, + method, + path, + e, + Some(scope), + Some(snapshot), + ); } }; let mapped = match merge_config_response_headers(py, snapshot, scope, mapped) { Ok(m) => m, Err(e) => { - return send_python_error_sync(py, protocol, method, path, e, Some(scope), Some(state)); + return send_python_error_sync( + py, + protocol, + method, + path, + e, + Some(scope), + Some(snapshot), + ); } }; if let Err(e) = send_handler_map_inline(py, protocol, is_head, mapped) { - return send_python_error_sync(py, protocol, method, path, e, Some(scope), Some(state)); + return send_python_error_sync(py, protocol, method, path, e, Some(scope), Some(snapshot)); } Ok(()) } @@ -304,10 +318,9 @@ async fn send_python_error( path: &str, err: PyErr, scope: Option<&Py>, - state: Option<&std::sync::Arc>>, + snapshot: Option<&HotSnapshot>, ) -> PyResult { - if let (Some(sc), Some(st)) = (scope, state) { - let snap = st.read().hot_snapshot(); + if let (Some(sc), Some(snap)) = (scope, snapshot) { let coro_or_res = Python::with_gil(|py| -> PyResult, bool)>> { for (exc_type, handler, is_async) in snap.exception_handlers.iter().rev() { if let Ok(exc_obj) = err.clone_ref(py).into_bound_py_any(py) { @@ -388,6 +401,7 @@ fn ensure_compiled_snapshot(state: &Arc>) -> Arc( py: Python<'py>, state: &Arc>, + snapshot: &HotSnapshot, scope: &Bound<'py, PyAny>, protocol: &Bound<'py, PyAny>, ) -> PyResult> { @@ -404,7 +418,6 @@ pub fn try_rsgi_sync_short_circuit<'py>( let path_obj = scope.getattr("path")?; let path = path_obj.extract::<&str>()?; let is_head = method == "HEAD"; - let snapshot = state.read().hot_snapshot(); let protocol_py: Py = protocol.clone().unbind(); // Native CORS preflight handling (issue #218) @@ -509,8 +522,7 @@ pub fn try_rsgi_sync_short_circuit<'py>( entry, ¶ms, scope, - state, - &snapshot, + snapshot, )?; return Ok(Some(py.None())); } @@ -536,12 +548,13 @@ pub fn try_rsgi_sync_short_circuit<'py>( pub async fn run_rsgi( state: Arc>, + snapshot: HotSnapshot, scope: Py, protocol: Py, ) -> PyResult { - // Single GIL block: read proto, scope fields, and shared cfg in one acquire/release. + // Single GIL block: read proto and scope fields in one acquire/release. // Each `Python::with_gil` costs ~100-300ns; coalescing saves ~1µs/req on the http path. - type RsgiPrelim = (String, String, String, bool, HotSnapshot); + type RsgiPrelim = (String, String, String, bool); let (proto, prelim): (String, Option) = Python::with_gil(|py| -> PyResult<_> { let s = scope.bind(py); let proto: String = s.getattr("proto")?.extract()?; @@ -558,16 +571,12 @@ pub async fn run_rsgi( .and_then(|x| x.extract()) .unwrap_or_default(); let is_head = method == "HEAD"; - // One `state.read()` for the entire request: every subsequent dispatch step uses - // [`HotSnapshot`] without re-acquiring the `RwLock`. The snapshot's - // ``Option>`` fields are cloned *here*, while the GIL is held. - let snapshot = state.read().hot_snapshot(); - Ok((proto, Some((method, path, qs, is_head, snapshot)))) + Ok((proto, Some((method, path, qs, is_head)))) })?; if proto == "websocket" { - return run_rsgi_websocket(state, scope, protocol).await; + return run_rsgi_websocket(state, snapshot, scope, protocol).await; } - let Some((method, path, query_string, is_head, snapshot)) = prelim else { + let Some((method, path, query_string, is_head)) = prelim else { return Ok(Python::with_gil(|py| py.None())); }; if (method == "GET" || method == "HEAD") && path == "/openapi.json" && snapshot.include_openapi @@ -637,8 +646,15 @@ pub async fn run_rsgi( }) { Ok(x) => x, Err(e) => { - return send_python_error(&protocol, &method, &path, e, Some(&scope), Some(&state)) - .await; + return send_python_error( + &protocol, + &method, + &path, + e, + Some(&scope), + Some(&snapshot), + ) + .await; } }; let skip = Python::with_gil(|py| out.bind(py).is_none()); @@ -714,7 +730,7 @@ pub async fn run_rsgi( }) { Ok(()) => Ok(Python::with_gil(|py| py.None())), Err(e) => { - send_python_error(&protocol, &method, &path, e, Some(&scope), Some(&state)) + send_python_error(&protocol, &method, &path, e, Some(&scope), Some(&snapshot)) .await } }; @@ -959,7 +975,7 @@ pub async fn run_rsgi( &path, e, Some(&scope), - Some(&state), + Some(&snapshot), ) .await; } @@ -1028,8 +1044,15 @@ pub async fn run_rsgi( }) { Ok(o) => Some(o), Err(e) => { - return send_python_error(&protocol, &method, &path, e, Some(&scope), Some(&state)) - .await; + return send_python_error( + &protocol, + &method, + &path, + e, + Some(&scope), + Some(&snapshot), + ) + .await; } } } else { @@ -1066,7 +1089,7 @@ pub async fn run_rsgi( &path, e, Some(&scope), - Some(&state), + Some(&snapshot), ) .await; } @@ -1083,7 +1106,7 @@ pub async fn run_rsgi( &path, e, Some(&scope), - Some(&state), + Some(&snapshot), ) .await; } @@ -1097,7 +1120,7 @@ pub async fn run_rsgi( &path, e, Some(&scope), - Some(&state), + Some(&snapshot), ) .await; } @@ -1131,7 +1154,7 @@ pub async fn run_rsgi( &path, e, Some(&scope), - Some(&state), + Some(&snapshot), ) .await; } @@ -1149,7 +1172,7 @@ pub async fn run_rsgi( })?; let resolved = if let Some(query) = db_query_opt { - let db_pool = state.read().db_pool.clone(); + let db_pool = snapshot.db_pool.clone(); match db_pool { Some(pool) => match crate::db::execute_query(&pool, &query).await { Ok(py_rows) => py_rows, @@ -1160,7 +1183,7 @@ pub async fn run_rsgi( &path, e, Some(&scope), - Some(&state), + Some(&snapshot), ) .await; } @@ -1174,7 +1197,7 @@ pub async fn run_rsgi( "DBQuery used but database pool is not configured (call app.setup_database first)", ), Some(&scope), - Some(&state), + Some(&snapshot), ) .await; } @@ -1375,7 +1398,7 @@ pub async fn run_rsgi( .await; } Err(e) => { - return send_python_error(&protocol, &method, &path, e, Some(&scope), Some(&state)) + return send_python_error(&protocol, &method, &path, e, Some(&scope), Some(&snapshot)) .await; } }; @@ -1386,15 +1409,29 @@ pub async fn run_rsgi( }) { Ok(f) => f, Err(e) => { - return send_python_error(&protocol, &method, &path, e, Some(&scope), Some(&state)) - .await; + return send_python_error( + &protocol, + &method, + &path, + e, + Some(&scope), + Some(&snapshot), + ) + .await; } }; match fut.await { Ok(x) => x, Err(e) => { - return send_python_error(&protocol, &method, &path, e, Some(&scope), Some(&state)) - .await; + return send_python_error( + &protocol, + &method, + &path, + e, + Some(&scope), + Some(&snapshot), + ) + .await; } } } else { @@ -1467,7 +1504,9 @@ pub async fn run_rsgi( send_handler_map_inline(py, &protocol, is_head, mapped) }) { Ok(()) => Ok(Python::with_gil(|py| py.None())), - Err(e) => send_python_error(&protocol, &method, &path, e, Some(&scope), Some(&state)).await, + Err(e) => { + send_python_error(&protocol, &method, &path, e, Some(&scope), Some(&snapshot)).await + } } } @@ -1978,12 +2017,12 @@ fn value_to_bytes_and_ct(py: Python<'_>, b: &Bound<'_, PyAny>) -> PyResult<(Vec< /// handler error → close (best-effort) and propagate to logs. async fn run_rsgi_websocket( state: Arc>, + snapshot: HotSnapshot, scope: Py, protocol: Py, ) -> PyResult { let path: String = Python::with_gil(|py| -> PyResult { scope.bind(py).getattr("path")?.extract() })?; - let snapshot = state.read().hot_snapshot(); let compiled = match snapshot.compiled.clone() { Some(c) => c, None => ensure_compiled_snapshot(&state), diff --git a/src/lib.rs b/src/lib.rs index f21a5f0..021ecd3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -202,6 +202,8 @@ fn dependency_wants_request(py: Python<'_>, f: &Bound<'_, PyAny>) -> PyResult>, + in_flight: Arc, + max_concurrency: Arc, } impl App { @@ -353,28 +355,29 @@ impl App { fn new(include_openapi: bool) -> Self { let mut s = AppState::new(); s.include_openapi = include_openapi; + let in_flight = Arc::clone(&s.in_flight); + let max_concurrency = Arc::clone(&s.max_concurrency); Self { state: Arc::new(RwLock::new(s)), + in_flight, + max_concurrency, } } fn set_max_concurrency(&self, limit: usize) -> PyResult<()> { - let st = self.state.read(); - st.max_concurrency + self.max_concurrency .store(limit, std::sync::atomic::Ordering::Relaxed); Ok(()) } fn get_max_concurrency(&self) -> PyResult { - let st = self.state.read(); - Ok(st + Ok(self .max_concurrency .load(std::sync::atomic::Ordering::Relaxed)) } fn get_in_flight(&self) -> PyResult { - let st = self.state.read(); - Ok(st.in_flight.load(std::sync::atomic::Ordering::Relaxed)) + Ok(self.in_flight.load(std::sync::atomic::Ordering::Relaxed)) } /// Notify all active WebSocket connections with a close status code (default: 1001 Going Away). @@ -888,18 +891,16 @@ impl App { scope: &Bound<'py, PyAny>, protocol: &Bound<'py, PyAny>, ) -> PyResult> { - let (in_flight, max_c) = { - let st = this.state.read(); - ( - Arc::clone(&st.in_flight), - st.max_concurrency - .load(std::sync::atomic::Ordering::Relaxed), - ) - }; + let max_c = this + .max_concurrency + .load(std::sync::atomic::Ordering::Relaxed); if max_c > 0 { - let current = in_flight.fetch_add(1, std::sync::atomic::Ordering::Acquire); + let current = this + .in_flight + .fetch_add(1, std::sync::atomic::Ordering::Acquire); if current >= max_c { - in_flight.fetch_sub(1, std::sync::atomic::Ordering::Release); + this.in_flight + .fetch_sub(1, std::sync::atomic::Ordering::Release); let protocol_py: Py = protocol.as_any().clone().unbind(); let headers = vec![ ( @@ -918,11 +919,15 @@ impl App { return Ok(py.None().into_bound(py)); } } else { - in_flight.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + this.in_flight + .fetch_add(1, std::sync::atomic::Ordering::Relaxed); } - let guard = InFlightGuard(in_flight); + let guard = InFlightGuard(Arc::clone(&this.in_flight)); - if let Some(obj) = try_rsgi_sync_short_circuit(py, &this.state, scope, protocol)? { + let snapshot = this.state.read().hot_snapshot(); + + if let Some(obj) = try_rsgi_sync_short_circuit(py, &this.state, &snapshot, scope, protocol)? + { drop(guard); return Ok(obj.into_bound(py)); } @@ -931,7 +936,7 @@ impl App { let protocol_py: Py = protocol.as_any().clone().unbind(); pyo3_async_runtimes::tokio::future_into_py(py, async move { let _guard = guard; - run_rsgi(state, scope_py, protocol_py).await + run_rsgi(state, snapshot, scope_py, protocol_py).await }) } diff --git a/src/state.rs b/src/state.rs index 0f2e9d5..0b6165e 100644 --- a/src/state.rs +++ b/src/state.rs @@ -217,6 +217,7 @@ impl AppState { response_middleware: Arc::clone(&response_middleware), exception_handlers: Arc::clone(&exception_handlers), include_openapi: true, + db_pool: None, }); Self { routes, @@ -263,6 +264,7 @@ impl AppState { response_middleware: Arc::clone(&self.response_middleware), exception_handlers: Arc::clone(&self.exception_handlers), include_openapi: self.include_openapi, + db_pool: self.db_pool.clone(), }); }); } @@ -307,6 +309,7 @@ pub struct FrozenState { pub response_middleware: Arc>>, pub exception_handlers: ExceptionHandlerList, pub include_openapi: bool, + pub db_pool: Option, } pub type HotSnapshot = Arc;