fix(tunnel): writer 退出时立即标记连接关闭, 防止后续请求堵塞

- ws_tx.send 添加 15s 超时, 防止 TCP 背压导致 writer 无限阻塞
- writer 退出时立即 request_close(), 不等 reader 结束即从路由表摘除连接
- ws_tx.close 添加 5s 超时, 防止 close 握手阻塞
This commit is contained in:
fawney19
2026-04-16 02:29:36 +08:00
parent 7597caa3a5
commit 28a489acbe

View File

@@ -43,6 +43,7 @@ pub async fn handle_proxy_connection(
hub.register_proxy(conn.clone()); hub.register_proxy(conn.clone());
let writer_conn_id = conn_id; let writer_conn_id = conn_id;
let writer_conn = conn.clone();
let writer = tokio::spawn(async move { let writer = tokio::spawn(async move {
let mut frames_sent: u64 = 0; let mut frames_sent: u64 = 0;
loop { loop {
@@ -54,14 +55,29 @@ pub async fn handle_proxy_connection(
Message::Binary(b) => b.len(), Message::Binary(b) => b.len(),
_ => 0, _ => 0,
}; };
if let Err(e) = ws_tx.send(msg).await { let send_result = tokio::time::timeout(
warn!( Duration::from_secs(15),
conn_id = writer_conn_id, ws_tx.send(msg),
frames_sent = frames_sent, ).await;
error = %e, match send_result {
"writer ws_tx.send failed" Ok(Ok(())) => {}
); Ok(Err(e)) => {
break; warn!(
conn_id = writer_conn_id,
frames_sent = frames_sent,
error = %e,
"writer ws_tx.send failed"
);
break;
}
Err(_) => {
warn!(
conn_id = writer_conn_id,
frames_sent = frames_sent,
"writer ws_tx.send timed out"
);
break;
}
} }
frames_sent += 1; frames_sent += 1;
if is_binary && msg_len > protocol::HEADER_SIZE { if is_binary && msg_len > protocol::HEADER_SIZE {
@@ -87,7 +103,8 @@ pub async fn handle_proxy_connection(
frames_sent = frames_sent, frames_sent = frames_sent,
"writer task exiting" "writer task exiting"
); );
let _ = ws_tx.close().await; writer_conn.request_close();
let _ = tokio::time::timeout(Duration::from_secs(5), ws_tx.close()).await;
}); });
let ping_conn = conn.clone(); let ping_conn = conn.clone();