Skip to content

Commit d64b1e7

Browse files
committed
Made Timeout of LazyAcceptTcpStream
1 parent b005b1e commit d64b1e7

File tree

2 files changed

+46
-27
lines changed

2 files changed

+46
-27
lines changed

src/http_control/startup.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::app::AppContext;
77
const DEFAULT_PORT: u16 = 8000;
88

99
pub fn start(app: &Arc<AppContext>) {
10-
let http_port = if let Ok(result) = std::env::var("CONTROL_HTTP_PORT") {
10+
let http_port = if let Ok(result) = std::env::var("HTTP_PORT") {
1111
match result.parse() {
1212
Ok(port) => port,
1313
Err(_) => DEFAULT_PORT,

src/http_server/https_server.rs

+45-26
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::time::Duration;
12
use std::{net::SocketAddr, sync::Arc};
23

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

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

42-
let result = lazy_accept_tcp_stream(app.clone(), endpoint_port, tcp_stream).await;
43+
let app = app.clone();
44+
tokio::spawn(async move { handle_connection(app, endpoint_port, tcp_stream, socket_addr) });
45+
}
46+
}
4347

44-
if let Err(err) = &result {
45-
eprintln!("failed to perform tls handshake: {err:#}");
46-
continue;
47-
}
48+
async fn handle_connection(
49+
app: Arc<AppContext>,
50+
endpoint_port: u16,
51+
tcp_stream: TcpStream,
52+
socket_addr: SocketAddr,
53+
) {
54+
let future = lazy_accept_tcp_stream(app.clone(), endpoint_port, tcp_stream);
4855

49-
let (tls_stream, endpoint_info, cn_user_name) = result.unwrap();
50-
51-
if endpoint_info.http_type.is_protocol_http1() {
52-
kick_off_https1(
53-
app.clone(),
54-
socket_addr,
55-
endpoint_info,
56-
tls_stream,
57-
cn_user_name,
58-
endpoint_port,
59-
);
60-
} else {
61-
kick_off_https2(
62-
app.clone(),
63-
socket_addr,
64-
endpoint_info,
65-
tls_stream,
66-
cn_user_name,
67-
endpoint_port,
68-
);
69-
}
56+
let result = tokio::time::timeout(Duration::from_secs(10), future).await;
57+
58+
if result.is_err() {
59+
println!("Timeout waiting for tls handshake from {}", socket_addr);
60+
}
61+
62+
let result = result.unwrap();
63+
64+
if let Err(err) = &result {
65+
eprintln!("failed to perform tls handshake: {err:#}");
66+
return;
67+
}
68+
69+
let (tls_stream, endpoint_info, cn_user_name) = result.unwrap();
70+
71+
if endpoint_info.http_type.is_protocol_http1() {
72+
kick_off_https1(
73+
app,
74+
socket_addr,
75+
endpoint_info,
76+
tls_stream,
77+
cn_user_name,
78+
endpoint_port,
79+
);
80+
} else {
81+
kick_off_https2(
82+
app,
83+
socket_addr,
84+
endpoint_info,
85+
tls_stream,
86+
cn_user_name,
87+
endpoint_port,
88+
);
7089
}
7190
}
7291

0 commit comments

Comments
 (0)