Skip to content

Commit

Permalink
Made Timeout of LazyAcceptTcpStream
Browse files Browse the repository at this point in the history
  • Loading branch information
amigin committed Apr 26, 2024
1 parent b005b1e commit d64b1e7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/http_control/startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::app::AppContext;
const DEFAULT_PORT: u16 = 8000;

pub fn start(app: &Arc<AppContext>) {
let http_port = if let Ok(result) = std::env::var("CONTROL_HTTP_PORT") {
let http_port = if let Ok(result) = std::env::var("HTTP_PORT") {
match result.parse() {
Ok(port) => port,
Err(_) => DEFAULT_PORT,
Expand Down
71 changes: 45 additions & 26 deletions src/http_server/https_server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::time::Duration;
use std::{net::SocketAddr, sync::Arc};

use hyper_util::rt::TokioIo;
Expand Down Expand Up @@ -39,34 +40,52 @@ async fn start_https_server_loop(addr: SocketAddr, app: Arc<AppContext>) {

println!("Accepted connection from {}", socket_addr);

let result = lazy_accept_tcp_stream(app.clone(), endpoint_port, tcp_stream).await;
let app = app.clone();
tokio::spawn(async move { handle_connection(app, endpoint_port, tcp_stream, socket_addr) });
}
}

if let Err(err) = &result {
eprintln!("failed to perform tls handshake: {err:#}");
continue;
}
async fn handle_connection(
app: Arc<AppContext>,
endpoint_port: u16,
tcp_stream: TcpStream,
socket_addr: SocketAddr,
) {
let future = lazy_accept_tcp_stream(app.clone(), endpoint_port, tcp_stream);

let (tls_stream, endpoint_info, cn_user_name) = result.unwrap();

if endpoint_info.http_type.is_protocol_http1() {
kick_off_https1(
app.clone(),
socket_addr,
endpoint_info,
tls_stream,
cn_user_name,
endpoint_port,
);
} else {
kick_off_https2(
app.clone(),
socket_addr,
endpoint_info,
tls_stream,
cn_user_name,
endpoint_port,
);
}
let result = tokio::time::timeout(Duration::from_secs(10), future).await;

if result.is_err() {
println!("Timeout waiting for tls handshake from {}", socket_addr);
}

let result = result.unwrap();

if let Err(err) = &result {
eprintln!("failed to perform tls handshake: {err:#}");
return;
}

let (tls_stream, endpoint_info, cn_user_name) = result.unwrap();

if endpoint_info.http_type.is_protocol_http1() {
kick_off_https1(
app,
socket_addr,
endpoint_info,
tls_stream,
cn_user_name,
endpoint_port,
);
} else {
kick_off_https2(
app,
socket_addr,
endpoint_info,
tls_stream,
cn_user_name,
endpoint_port,
);
}
}

Expand Down

0 comments on commit d64b1e7

Please sign in to comment.