tokio-xmpp: let happy_eyeballs connect to records in parallel
This commit is contained in:
parent
598ffdbecf
commit
6c3081d656
1 changed files with 10 additions and 7 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
use super::error::{ConnectorError, Error};
|
use super::error::{ConnectorError, Error};
|
||||||
|
use futures::{future::select_ok, FutureExt};
|
||||||
use hickory_resolver::{
|
use hickory_resolver::{
|
||||||
config::LookupIpStrategy, name_server::TokioConnectionProvider, IntoName, TokioAsyncResolver,
|
config::LookupIpStrategy, name_server::TokioConnectionProvider, IntoName, TokioAsyncResolver,
|
||||||
};
|
};
|
||||||
|
|
@ -24,13 +25,15 @@ pub async fn connect_to_host(domain: &str, port: u16) -> Result<TcpStream, Error
|
||||||
.lookup_ip(ascii_domain)
|
.lookup_ip(ascii_domain)
|
||||||
.await
|
.await
|
||||||
.map_err(ConnectorError::Resolve)?;
|
.map_err(ConnectorError::Resolve)?;
|
||||||
for ip in ips.iter() {
|
// Happy Eyeballs: connect to all records in parallel, return the
|
||||||
match TcpStream::connect(&SocketAddr::new(ip, port)).await {
|
// first to succeed
|
||||||
Ok(stream) => return Ok(stream),
|
select_ok(
|
||||||
Err(_) => {}
|
ips.into_iter()
|
||||||
}
|
.map(|ip| TcpStream::connect(SocketAddr::new(ip, port)).boxed()),
|
||||||
}
|
)
|
||||||
Err(crate::Error::Disconnected.into())
|
.await
|
||||||
|
.map(|(result, _)| result)
|
||||||
|
.map_err(|_| crate::Error::Disconnected.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn connect_with_srv(
|
pub async fn connect_with_srv(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue