Skip to content

Commit eb813d9

Browse files
authored
Add 5 second timeout when connecting to the home relay (#83)
This will let us print out a ticket even if we can't get online
1 parent 4002f3e commit eb813d9

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/main.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ use std::{
77
io,
88
net::{SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs},
99
str::FromStr,
10+
time::Duration,
1011
};
1112
use tokio::{
1213
io::{AsyncRead, AsyncWrite, AsyncWriteExt},
1314
select,
15+
time::timeout,
1416
};
1517
use tokio_util::sync::CancellationToken;
1618

@@ -20,6 +22,8 @@ use {
2022
tokio::net::{UnixListener, UnixStream},
2123
};
2224

25+
const ONLINE_TIMEOUT: Duration = Duration::from_secs(5);
26+
2327
/// Create a dumb pipe between two machines, using an iroh endpoint.
2428
///
2529
/// One side listens, the other side connects. Both sides are identified by a
@@ -352,7 +356,9 @@ async fn listen_stdio(args: ListenArgs) -> Result<()> {
352356
let secret_key = get_or_create_secret()?;
353357
let endpoint = create_endpoint(secret_key, &args.common, vec![args.common.alpn()?]).await?;
354358
// wait for the endpoint to figure out its home relay and addresses before making a ticket
355-
endpoint.online().await;
359+
if (timeout(ONLINE_TIMEOUT, endpoint.online()).await).is_err() {
360+
eprintln!("Warning: Failed to connect to the home relay");
361+
}
356362
let addr = endpoint.addr();
357363
let short = create_short_ticket(&addr);
358364
let ticket = EndpointTicket::new(addr);
@@ -458,7 +464,9 @@ async fn connect_tcp(args: ConnectTcpArgs) -> Result<()> {
458464
tracing::info!("tcp listening on {:?}", addrs);
459465

460466
// Wait for our own endpoint to be ready before trying to connect.
461-
endpoint.online().await;
467+
if (timeout(ONLINE_TIMEOUT, endpoint.online()).await).is_err() {
468+
eprintln!("Warning: Failed to connect to the home relay");
469+
}
462470

463471
let tcp_listener = match tokio::net::TcpListener::bind(addrs.as_slice()).await {
464472
Ok(tcp_listener) => tcp_listener,
@@ -534,7 +542,9 @@ async fn listen_tcp(args: ListenTcpArgs) -> Result<()> {
534542
let secret_key = get_or_create_secret()?;
535543
let endpoint = create_endpoint(secret_key, &args.common, vec![args.common.alpn()?]).await?;
536544
// wait for the endpoint to figure out its address before making a ticket
537-
endpoint.online().await;
545+
if (timeout(ONLINE_TIMEOUT, endpoint.online()).await).is_err() {
546+
eprintln!("Warning: Failed to connect to the home relay");
547+
}
538548
let addr = endpoint.addr();
539549
let short = create_short_ticket(&addr);
540550
let ticket = EndpointTicket::new(addr);
@@ -630,7 +640,9 @@ async fn listen_unix(args: ListenUnixArgs) -> Result<()> {
630640
let secret_key = get_or_create_secret()?;
631641
let endpoint = create_endpoint(secret_key, &args.common, vec![args.common.alpn()?]).await?;
632642
// wait for the endpoint to figure out its address before making a ticket
633-
endpoint.online().await;
643+
if (timeout(ONLINE_TIMEOUT, endpoint.online()).await).is_err() {
644+
eprintln!("Warning: Failed to connect to the home relay");
645+
}
634646
let addr = endpoint.addr();
635647
let short = create_short_ticket(&addr);
636648
let ticket = EndpointTicket::new(addr);
@@ -750,7 +762,9 @@ async fn connect_unix(args: ConnectUnixArgs) -> Result<()> {
750762
tracing::info!("unix listening on {:?}", socket_path);
751763

752764
// Wait for our own endpoint to be ready before trying to connect.
753-
endpoint.online().await;
765+
if (timeout(ONLINE_TIMEOUT, endpoint.online()).await).is_err() {
766+
eprintln!("Warning: Failed to connect to the home relay");
767+
}
754768

755769
// Remove existing socket file if it exists
756770
if let Err(e) = tokio::fs::remove_file(&socket_path).await {

0 commit comments

Comments
 (0)