diff --git a/crates/ptth_server/src/errors.rs b/crates/ptth_server/src/errors.rs index bac6985..5ed5fba 100644 --- a/crates/ptth_server/src/errors.rs +++ b/crates/ptth_server/src/errors.rs @@ -79,4 +79,7 @@ pub enum ServerError { #[error ("Step 3 relay response (non-200 OK) was not valid UTF-8")] Step3ErrorResponseNotUtf8 (std::string::FromUtf8Error), + + #[error (transparent)] + Other (#[from] anyhow::Error), } diff --git a/crates/ptth_server/src/lib.rs b/crates/ptth_server/src/lib.rs index bb32415..23a2767 100644 --- a/crates/ptth_server/src/lib.rs +++ b/crates/ptth_server/src/lib.rs @@ -40,6 +40,7 @@ #![allow (clippy::mut_mut)] use std::{ + future::Future, path::PathBuf, sync::Arc, time::Duration, @@ -144,10 +145,15 @@ async fn handle_one_req ( Ok::<(), ServerError> (()) } -async fn handle_requests ( +async fn handle_requests ( state: &Arc , - req_resp: reqwest::Response -) -> Result <(), ServerError> + req_resp: reqwest::Response, + mut spawn_handler: H, +) -> Result <(), ServerError> +where +F: Send + Future >, +F2: Send + 'static + FnOnce (http_serde::RequestParts) -> F, +H: Send + FnMut () -> F2 { //println! ("Step 1"); @@ -165,6 +171,7 @@ async fn handle_requests ( for wrapped_req in wrapped_reqs { let state = Arc::clone (&state); + let handler = spawn_handler (); // These have to detach, so we won't be able to catch the join errors. @@ -172,12 +179,16 @@ async fn handle_requests ( let (req_id, parts) = (wrapped_req.id, wrapped_req.req); debug! ("Handling request {}", req_id); - + /* let response = state.file_server.serve_all ( parts.method, &parts.uri, &parts.headers, ).await?; + */ + + let f = handler (parts); + let response = f.await?; handle_one_req (&state, req_id, response).await }); @@ -373,7 +384,19 @@ async fn run_server_loop ( // Unpack the requests, spawn them into new tasks, then loop back // around. - if handle_requests (&state, req_resp).await.is_err () { + let spawn_handler = || { + let state = Arc::clone (&state); + + |req: http_serde::RequestParts| async move { + Ok (state.file_server.serve_all (req.method, &req.uri, &req.headers).await?) + } + }; + + if handle_requests ( + &state, + req_resp, + spawn_handler, + ).await.is_err () { backoff_delay = err_backoff_delay; continue; }