add some timeouts

This commit is contained in:
Jun Kurihara 2022-07-12 01:12:47 +09:00
commit 9c93b1cc31
No known key found for this signature in database
GPG key ID: 48ADFD173ED22B03
2 changed files with 23 additions and 7 deletions

View file

@ -91,13 +91,22 @@ where
req_forwarded.log(&client_addr, Some("(Request to Backend)")); req_forwarded.log(&client_addr, Some("(Request to Backend)"));
// Forward request to // Forward request to
let mut res_backend = match self.forwarder.request(req_forwarded).await { let mut res_backend = {
Ok(res) => res, match tokio::time::timeout(self.globals.timeout, self.forwarder.request(req_forwarded)).await
Err(e) => { {
error!("Failed to get response from backend: {}", e); Err(_) => {
return http_error(StatusCode::BAD_REQUEST); return http_error(StatusCode::GATEWAY_TIMEOUT);
}
Ok(x) => match x {
Ok(res) => res,
Err(e) => {
error!("Failed to get response from backend: {}", e);
return http_error(StatusCode::BAD_REQUEST);
}
},
} }
}; };
res_backend.log_debug( res_backend.log_debug(
&backend.server_name, &backend.server_name,
&client_addr, &client_addr,

View file

@ -17,8 +17,10 @@ where
return; return;
} }
let fut = self.clone().handle_connection_h3(conn); let fut = self.clone().handle_connection_h3(conn);
let timeout_sec = self.globals.timeout;
self.globals.runtime_handle.spawn(async move { self.globals.runtime_handle.spawn(async move {
if let Err(e) = fut.await { if let Err(e) = tokio::time::timeout(timeout_sec + Duration::from_secs(1), fut).await {
// TODO: ここのtimeoutはどの値を使うべき
warn!("QUIC or HTTP/3 connection failed: {}", e) warn!("QUIC or HTTP/3 connection failed: {}", e)
} }
clients_count.decrement(); clients_count.decrement();
@ -79,7 +81,12 @@ where
let self_inner = self.clone(); let self_inner = self.clone();
self.globals.runtime_handle.spawn(async move { self.globals.runtime_handle.spawn(async move {
if let Err(e) = self_inner.handle_stream_h3(req, stream, client_addr).await { if let Err(e) = tokio::time::timeout(
self_inner.globals.timeout + Duration::from_secs(1), // timeout per stream
self_inner.handle_stream_h3(req, stream, client_addr),
)
.await
{
error!("HTTP/3 request failed: {}", e); error!("HTTP/3 request failed: {}", e);
} }
// // TODO: Work around for timeout // // TODO: Work around for timeout