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
This commit is contained in:
arloor
2026-01-19 10:26:41 +08:00
committed by GitHub
parent 8e82925c19
commit 2dbd8468f3
@@ -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,
}
}
}