🚧 it works!

main
_ 2021-05-09 18:32:24 +00:00
parent c663d126aa
commit 08009de043
2 changed files with 31 additions and 5 deletions

View File

@ -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),
}

View File

@ -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 <F, F2, H> (
state: &Arc <State>,
req_resp: reqwest::Response
) -> Result <(), ServerError>
req_resp: reqwest::Response,
mut spawn_handler: H,
) -> Result <(), ServerError>
where
F: Send + Future <Output = anyhow::Result <http_serde::Response>>,
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;
}