From 2dbd8468f31b330e6f84b007e2369c60c6d1dc05 Mon Sep 17 00:00:00 2001 From: arloor Date: Mon, 19 Jan 2026 10:26:41 +0800 Subject: [PATCH] fix(http): add is_ready method and enhance connection checks (#2062) * fix(http): add is_ready method and enhance connection checks * fix(http): enhance connection caching by ensuring readiness before storing --- .../src/local/http/http_client.rs | 40 ++++++++++++++++--- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/crates/shadowsocks-service/src/local/http/http_client.rs b/crates/shadowsocks-service/src/local/http/http_client.rs index bad67192..9045c05e 100644 --- a/crates/shadowsocks-service/src/local/http/http_client.rs +++ b/crates/shadowsocks-service/src/local/http/http_client.rs @@ -249,6 +249,9 @@ where if c.is_closed() { continue; } + if !c.is_ready() { + continue; + } return Some(c); } } @@ -271,12 +274,23 @@ where "HTTP connection keep-alive for host: {}, response: {:?}", host, response ); - self.cache_conn - .lock() - .await - .entry(host) - .or_insert_with(VecDeque::new) - .push_back((c, Instant::now())); + let cache_conn = self.cache_conn.clone(); + tokio::spawn(async move { + match c.ready().await { + Ok(_) => { + trace!("HTTP connection for host: {host} is ready and will be cached"); + cache_conn + .lock() + .await + .entry(host) + .or_insert_with(VecDeque::new) + .push_back((c, Instant::now())); + } + Err(e) => { + trace!("HTTP connection for host: {host} failed to become ready: {}", e); + } + }; + }); } Ok(response) @@ -448,4 +462,18 @@ where Self::Http2(r) => r.is_closed(), } } + + pub fn is_ready(&self) -> bool { + match self { + Self::Http1(r) => r.is_ready(), + Self::Http2(r) => r.is_ready(), + } + } + + pub async fn ready(&mut self) -> Result<(), hyper::Error> { + match self { + HttpConnection::Http1(r) => r.ready().await, + HttpConnection::Http2(r) => r.ready().await, + } + } }