fix(chatsvc): connect the Trouter socket over IPv4 first
- on Daniel's Windows network the IPv6 address of the Trouter host hangs past the 20 s budget, so the calling engine needed minutes to start - each address gets 5 s, IPv4 before IPv6 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -10,7 +10,7 @@ use tokio::net::TcpStream;
10
10
use tokio::sync::{Notify, mpsc, oneshot, watch};
11
11
use tokio::time::{MissedTickBehavior, interval, sleep, sleep_until, timeout};
12
12
use tokio_tungstenite::tungstenite::Message;
13
use tokio_tungstenite::{MaybeTlsStream, WebSocketStream, connect_async};
13
use tokio_tungstenite::{MaybeTlsStream, WebSocketStream, client_async_tls_with_config};
14
14
15
15
use super::RealtimeConfig;
16
16
use super::callback::{CallbackReply, decode_callback};
@@ -25,6 +25,7 @@ const PING_EVERY: Duration = Duration::from_secs(30);
25
25
const SILENCE_LIMIT: Duration = Duration::from_secs(100);
26
26
const REREGISTER_AFTER: Duration = Duration::from_secs(40 * 60);
27
27
const CONNECT_TIMEOUT: Duration = Duration::from_secs(20);
28
const ADDRESS_TIMEOUT: Duration = Duration::from_secs(5);
28
29
const MAX_BACKOFF: Duration = Duration::from_secs(30);
29
30
const ACTIVITY_DEBOUNCE: Duration = Duration::from_secs(1);
30
31
const ACTIVITY_ACK_TIMEOUT: Duration = Duration::from_secs(10);
@@ -353,8 +354,8 @@ impl NativeTrouter {
353
354
};
354
355
session::ensure_crypto_provider();
355
356
let url = socket_url(&self.host, &self.endpoint_id, &uuid::Uuid::new_v4().to_string(), now_ms() as u128);
356
let socket = match timeout(CONNECT_TIMEOUT, connect_async(url.as_str())).await {
357
Ok(Ok((socket, _))) => socket,
357
let socket = match timeout(CONNECT_TIMEOUT, open_socket(&self.host, &url)).await {
358
Ok(Ok(socket)) => socket,
358
359
Ok(Err(error)) => {
359
360
self.forward(&status_payload("disconnected", &format!("connect_failed: {}", short(&error.to_string()))));
360
361
return Err(None);
@@ -602,6 +603,24 @@ impl NativeTrouter {
602
603
}
603
604
}
604
605
606
/// IPv4 first: on some networks the IPv6 address of the Trouter host takes longer than the whole connect budget to fail.
607
async fn open_socket(host: &str, url: &str) -> Result<Socket, tokio_tungstenite::tungstenite::Error> {
608
let mut addresses: Vec<std::net::SocketAddr> = tokio::net::lookup_host((host, 443)).await?.collect();
609
addresses.sort_by_key(|address| address.is_ipv6());
610
let mut last_error = std::io::Error::new(std::io::ErrorKind::NotFound, "no address");
611
for address in addresses {
612
match timeout(ADDRESS_TIMEOUT, TcpStream::connect(address)).await {
613
Ok(Ok(stream)) => {
614
let (socket, _) = client_async_tls_with_config(url, stream, None, None).await?;
615
return Ok(socket);
616
}
617
Ok(Err(error)) => last_error = error,
618
Err(_) => last_error = std::io::Error::new(std::io::ErrorKind::TimedOut, format!("{} timed out", if address.is_ipv6() { "IPv6" } else { "IPv4" })),
619
}
620
}
621
Err(last_error.into())
622
}
623
605
624
async fn send(connection: &mut Connection, text: String) {
606
625
let _ = connection.socket.send(Message::Text(text.into())).await;
607
626
}
@@ -10,7 +10,7 @@ use tokio::net::TcpStream;10
use tokio::sync::{Notify, mpsc, oneshot, watch};10
use tokio::sync::{Notify, mpsc, oneshot, watch};11
use tokio::time::{MissedTickBehavior, interval, sleep, sleep_until, timeout};11
use tokio::time::{MissedTickBehavior, interval, sleep, sleep_until, timeout};12
use tokio_tungstenite::tungstenite::Message;12
use tokio_tungstenite::tungstenite::Message;13
use tokio_tungstenite::{MaybeTlsStream, WebSocketStream, connect_async};13
use tokio_tungstenite::{MaybeTlsStream, WebSocketStream, client_async_tls_with_config};14
14
15
use super::RealtimeConfig;15
use super::RealtimeConfig;16
use super::callback::{CallbackReply, decode_callback};16
use super::callback::{CallbackReply, decode_callback};@@ -25,6 +25,7 @@ const PING_EVERY: Duration = Duration::from_secs(30);25
const SILENCE_LIMIT: Duration = Duration::from_secs(100);25
const SILENCE_LIMIT: Duration = Duration::from_secs(100);26
const REREGISTER_AFTER: Duration = Duration::from_secs(40 * 60);26
const REREGISTER_AFTER: Duration = Duration::from_secs(40 * 60);27
const CONNECT_TIMEOUT: Duration = Duration::from_secs(20);27
const CONNECT_TIMEOUT: Duration = Duration::from_secs(20);28
const ADDRESS_TIMEOUT: Duration = Duration::from_secs(5);28
const MAX_BACKOFF: Duration = Duration::from_secs(30);29
const MAX_BACKOFF: Duration = Duration::from_secs(30);29
const ACTIVITY_DEBOUNCE: Duration = Duration::from_secs(1);30
const ACTIVITY_DEBOUNCE: Duration = Duration::from_secs(1);30
const ACTIVITY_ACK_TIMEOUT: Duration = Duration::from_secs(10);31
const ACTIVITY_ACK_TIMEOUT: Duration = Duration::from_secs(10);@@ -353,8 +354,8 @@ impl NativeTrouter {353
};354
};354
session::ensure_crypto_provider();355
session::ensure_crypto_provider();355
let url = socket_url(&self.host, &self.endpoint_id, &uuid::Uuid::new_v4().to_string(), now_ms() as u128);356
let url = socket_url(&self.host, &self.endpoint_id, &uuid::Uuid::new_v4().to_string(), now_ms() as u128);356
let socket = match timeout(CONNECT_TIMEOUT, connect_async(url.as_str())).await {357
let socket = match timeout(CONNECT_TIMEOUT, open_socket(&self.host, &url)).await {357
Ok(Ok((socket, _))) => socket,358
Ok(Ok(socket)) => socket,358
Ok(Err(error)) => {359
Ok(Err(error)) => {359
self.forward(&status_payload("disconnected", &format!("connect_failed: {}", short(&error.to_string()))));360
self.forward(&status_payload("disconnected", &format!("connect_failed: {}", short(&error.to_string()))));360
return Err(None);361
return Err(None);@@ -602,6 +603,24 @@ impl NativeTrouter {602
}603
}603
}604
}604
605
606
/// IPv4 first: on some networks the IPv6 address of the Trouter host takes longer than the whole connect budget to fail.607
async fn open_socket(host: &str, url: &str) -> Result<Socket, tokio_tungstenite::tungstenite::Error> {608
let mut addresses: Vec<std::net::SocketAddr> = tokio::net::lookup_host((host, 443)).await?.collect();609
addresses.sort_by_key(|address| address.is_ipv6());610
let mut last_error = std::io::Error::new(std::io::ErrorKind::NotFound, "no address");611
for address in addresses {612
match timeout(ADDRESS_TIMEOUT, TcpStream::connect(address)).await {613
Ok(Ok(stream)) => {614
let (socket, _) = client_async_tls_with_config(url, stream, None, None).await?;615
return Ok(socket);616
}617
Ok(Err(error)) => last_error = error,618
Err(_) => last_error = std::io::Error::new(std::io::ErrorKind::TimedOut, format!("{} timed out", if address.is_ipv6() { "IPv6" } else { "IPv4" })),619
}620
}621
Err(last_error.into())622
}623
605
async fn send(connection: &mut Connection, text: String) {624
async fn send(connection: &mut Connection, text: String) {606
let _ = connection.socket.send(Message::Text(text.into())).await;625
let _ = connection.socket.send(Message::Text(text.into())).await;607
}626
}