Skip to content

Commit 2dbd846

Browse files
authored
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
1 parent 8e82925 commit 2dbd846

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

crates/shadowsocks-service/src/local/http/http_client.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ where
249249
if c.is_closed() {
250250
continue;
251251
}
252+
if !c.is_ready() {
253+
continue;
254+
}
252255
return Some(c);
253256
}
254257
}
@@ -271,12 +274,23 @@ where
271274
"HTTP connection keep-alive for host: {}, response: {:?}",
272275
host, response
273276
);
274-
self.cache_conn
275-
.lock()
276-
.await
277-
.entry(host)
278-
.or_insert_with(VecDeque::new)
279-
.push_back((c, Instant::now()));
277+
let cache_conn = self.cache_conn.clone();
278+
tokio::spawn(async move {
279+
match c.ready().await {
280+
Ok(_) => {
281+
trace!("HTTP connection for host: {host} is ready and will be cached");
282+
cache_conn
283+
.lock()
284+
.await
285+
.entry(host)
286+
.or_insert_with(VecDeque::new)
287+
.push_back((c, Instant::now()));
288+
}
289+
Err(e) => {
290+
trace!("HTTP connection for host: {host} failed to become ready: {}", e);
291+
}
292+
};
293+
});
280294
}
281295

282296
Ok(response)
@@ -448,4 +462,18 @@ where
448462
Self::Http2(r) => r.is_closed(),
449463
}
450464
}
465+
466+
pub fn is_ready(&self) -> bool {
467+
match self {
468+
Self::Http1(r) => r.is_ready(),
469+
Self::Http2(r) => r.is_ready(),
470+
}
471+
}
472+
473+
pub async fn ready(&mut self) -> Result<(), hyper::Error> {
474+
match self {
475+
HttpConnection::Http1(r) => r.ready().await,
476+
HttpConnection::Http2(r) => r.ready().await,
477+
}
478+
}
451479
}

0 commit comments

Comments
 (0)