hyperium / h2

HTTP 2.0 client & server implementation for Rust.
MIT License
1.34k stars 266 forks source link

Request processing always times out, #694

Closed silence-coding closed 1 year ago

silence-coding commented 1 year ago

If the peer IP address is suddenly unreachable after the H2 client creates a connection and processes requests for a period of time, the request sent to the target domain name times out due to connection reuse. Is there any method to detect the IP address unreachability and create a new connection? In the Hyper Connector, each time a connection is created, the domain name is resolved again to obtain the IP address.

Keepalive has been configured.

            .http2_keep_alive_timeout(Duration::from_secs(10))
            .http2_keep_alive_while_idle(true)
            .http2_keep_alive_interval(Duration::from_secs(5));
impl<A: ToSocketAddrs + Send + Sync> TcpConnector<A> {
    async fn resolve(&self) -> io::Result<tokio::net::TcpStream> {
        let mut stream = Err(std::io::ErrorKind::AddrNotAvailable.into());
        let addr = { *self.addr_cache.read().unwrap() };
        if let Some(addr) = addr {
            stream = tokio::net::TcpStream::connect(addr).await;
        }
        if stream.is_err() {
            for addr in tokio::net::lookup_host(&self.addr).await? {
                match tokio::net::TcpStream::connect(addr).await {
                    Ok(s) => {
                        *self.addr_cache.write().unwrap() = Some(addr);
                        return Ok(s);
                    },
                    Err(e) => stream = Err(e),
                }
            }
        }
        stream
    }
}
silence-coding commented 1 year ago

This is my code problem because the TcpStream::connect method keeps waiting