From 6c3081d656ca0f2b0ffaea22b24e2105346c0316 Mon Sep 17 00:00:00 2001 From: Astro Date: Sun, 14 Apr 2024 21:57:15 +0200 Subject: [PATCH] tokio-xmpp: let happy_eyeballs connect to records in parallel --- tokio-xmpp/src/starttls/happy_eyeballs.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/tokio-xmpp/src/starttls/happy_eyeballs.rs b/tokio-xmpp/src/starttls/happy_eyeballs.rs index 2c41897..1c4de86 100644 --- a/tokio-xmpp/src/starttls/happy_eyeballs.rs +++ b/tokio-xmpp/src/starttls/happy_eyeballs.rs @@ -1,4 +1,5 @@ use super::error::{ConnectorError, Error}; +use futures::{future::select_ok, FutureExt}; use hickory_resolver::{ config::LookupIpStrategy, name_server::TokioConnectionProvider, IntoName, TokioAsyncResolver, }; @@ -24,13 +25,15 @@ pub async fn connect_to_host(domain: &str, port: u16) -> Result return Ok(stream), - Err(_) => {} - } - } - Err(crate::Error::Disconnected.into()) + // Happy Eyeballs: connect to all records in parallel, return the + // first to succeed + select_ok( + ips.into_iter() + .map(|ip| TcpStream::connect(SocketAddr::new(ip, port)).boxed()), + ) + .await + .map(|(result, _)| result) + .map_err(|_| crate::Error::Disconnected.into()) } pub async fn connect_with_srv(