From c0576d23a0e46b2fdc69f2ef028fb96459679756 Mon Sep 17 00:00:00 2001 From: Doug Anderson444 Date: Wed, 29 Nov 2023 20:59:20 -0400 Subject: [PATCH 1/7] set timeout to slightly greater than Ping interval The example was timing out because the idle connection time was 5 seconds and the ping interval is 15s --- examples/browser-webrtc/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/browser-webrtc/src/lib.rs b/examples/browser-webrtc/src/lib.rs index 2112919c6de..e200a8436be 100644 --- a/examples/browser-webrtc/src/lib.rs +++ b/examples/browser-webrtc/src/lib.rs @@ -24,7 +24,7 @@ pub async fn run(libp2p_endpoint: String) -> Result<(), JsError> { webrtc_websys::Transport::new(webrtc_websys::Config::new(&key)) })? .with_behaviour(|_| ping::Behaviour::new(ping::Config::new()))? - .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(5))) + .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(20))) .build(); let addr = libp2p_endpoint.parse::()?; From 2c94a3f4d206a132a76c7867dc2fc882bb1f636d Mon Sep 17 00:00:00 2001 From: Doug Anderson444 Date: Wed, 29 Nov 2023 21:00:04 -0400 Subject: [PATCH 2/7] add install note for `wasm-pack` to README it's the small things that help out new users --- examples/browser-webrtc/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/browser-webrtc/README.md b/examples/browser-webrtc/README.md index d44cf879905..eec2c9c0494 100644 --- a/examples/browser-webrtc/README.md +++ b/examples/browser-webrtc/README.md @@ -5,6 +5,8 @@ It uses [wasm-pack](https://rustwasm.github.io/docs/wasm-pack/) to build the pro ## Running the example +Ensure you have `wasm-pack` [installed](https://rustwasm.github.io/wasm-pack/). + 1. Build the client library: ```shell wasm-pack build --target web --out-dir static From 6c8ffb9dc19dcd05c654f8965e91eda7006bd273 Mon Sep 17 00:00:00 2001 From: Doug Anderson444 Date: Wed, 29 Nov 2023 22:34:22 -0400 Subject: [PATCH 3/7] set idle timeout to u64::MAX Since Ping does not `KeepAlive`, we set the idle timeout to a very large number of seconds --- examples/browser-webrtc/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/browser-webrtc/src/lib.rs b/examples/browser-webrtc/src/lib.rs index e200a8436be..bcdf32e52db 100644 --- a/examples/browser-webrtc/src/lib.rs +++ b/examples/browser-webrtc/src/lib.rs @@ -24,7 +24,8 @@ pub async fn run(libp2p_endpoint: String) -> Result<(), JsError> { webrtc_websys::Transport::new(webrtc_websys::Config::new(&key)) })? .with_behaviour(|_| ping::Behaviour::new(ping::Config::new()))? - .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(20))) + // Ping does not KeepAlive, so we set the idle connection timeout to the maximum u64 value + .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(u64::MAX))) .build(); let addr = libp2p_endpoint.parse::()?; From ea9d6fd815079550e66f34d5f911069a559a70b3 Mon Sep 17 00:00:00 2001 From: Doug Anderson444 Date: Thu, 30 Nov 2023 10:12:42 -0400 Subject: [PATCH 4/7] extend the idle timeout on the server --- examples/browser-webrtc/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/browser-webrtc/src/main.rs b/examples/browser-webrtc/src/main.rs index 098b70f3054..3fbc8cfec0e 100644 --- a/examples/browser-webrtc/src/main.rs +++ b/examples/browser-webrtc/src/main.rs @@ -38,7 +38,7 @@ async fn main() -> anyhow::Result<()> { .with_behaviour(|_| ping::Behaviour::default())? .with_swarm_config(|cfg| { cfg.with_idle_connection_timeout( - Duration::from_secs(30), // Allows us to observe the pings. + Duration::from_secs(u64::MAX), // Allows us to observe the pings. ) }) .build(); From 2111a07aa8d8a5574b57f2f21ea18beebbbec3f1 Mon Sep 17 00:00:00 2001 From: Doug Anderson444 Date: Thu, 30 Nov 2023 10:22:34 -0400 Subject: [PATCH 5/7] set wasm32 target idle value to maximum For some reason, the largest value that works in wasm32 target (in Chrome) is 32_212_254u64. Everything larger than this breaks in the browser. Cause unknown at this point. --- examples/browser-webrtc/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/browser-webrtc/src/lib.rs b/examples/browser-webrtc/src/lib.rs index bcdf32e52db..cb9abe18d36 100644 --- a/examples/browser-webrtc/src/lib.rs +++ b/examples/browser-webrtc/src/lib.rs @@ -24,8 +24,9 @@ pub async fn run(libp2p_endpoint: String) -> Result<(), JsError> { webrtc_websys::Transport::new(webrtc_websys::Config::new(&key)) })? .with_behaviour(|_| ping::Behaviour::new(ping::Config::new()))? - // Ping does not KeepAlive, so we set the idle connection timeout to the maximum u64 value - .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(u64::MAX))) + // Ping does not KeepAlive, so we set the idle connection timeout to 32_212_254u64, + // which is the largest value that works with the wasm32 target. + .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(32_212_254u64))) .build(); let addr = libp2p_endpoint.parse::()?; From e27bd7a6303820ea089abb316a348849d2b7b819 Mon Sep 17 00:00:00 2001 From: Doug Anderson444 Date: Fri, 1 Dec 2023 09:51:26 -0400 Subject: [PATCH 6/7] add verbose 30s beginning, duration, and end Added more clarity about how long the Ping is intended to last, and made it a bit more obvious that the ConnectionClosed event is expected in the case of `Ping` after the idle timeout expires --- examples/browser-webrtc/src/lib.rs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/examples/browser-webrtc/src/lib.rs b/examples/browser-webrtc/src/lib.rs index cb9abe18d36..396400bc078 100644 --- a/examples/browser-webrtc/src/lib.rs +++ b/examples/browser-webrtc/src/lib.rs @@ -15,8 +15,13 @@ use web_sys::{Document, HtmlElement}; pub async fn run(libp2p_endpoint: String) -> Result<(), JsError> { tracing_wasm::set_as_global_default(); + let ping_duration = Duration::from_secs(30); + let body = Body::from_current_window()?; - body.append_p("Let's ping the WebRTC Server!")?; + body.append_p(&format!( + "Let's ping the rust-libp2p server over WebRTC for {:?}:", + ping_duration + ))?; let mut swarm = libp2p::SwarmBuilder::with_new_identity() .with_wasm_bindgen() @@ -24,9 +29,7 @@ pub async fn run(libp2p_endpoint: String) -> Result<(), JsError> { webrtc_websys::Transport::new(webrtc_websys::Config::new(&key)) })? .with_behaviour(|_| ping::Behaviour::new(ping::Config::new()))? - // Ping does not KeepAlive, so we set the idle connection timeout to 32_212_254u64, - // which is the largest value that works with the wasm32 target. - .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(32_212_254u64))) + .with_swarm_config(|c| c.with_idle_connection_timeout(ping_duration)) .build(); let addr = libp2p_endpoint.parse::()?; @@ -48,6 +51,19 @@ pub async fn run(libp2p_endpoint: String) -> Result<(), JsError> { tracing::info!("Ping successful: RTT: {rtt:?}, from {peer}"); body.append_p(&format!("RTT: {rtt:?} at {}", Date::new_0().to_string()))?; } + SwarmEvent::ConnectionClosed { + cause: Some(cause), .. + } => { + tracing::info!("Swarm event: {:?}", cause); + + if let libp2p::swarm::ConnectionError::KeepAliveTimeout = cause { + body.append_p("All done with pinging! ")?; + + break; + } else { + body.append_p(&format!("Connection closed due to: {:?}", cause))?; + } + } evt => tracing::info!("Swarm event: {:?}", evt), } } From 89747b9fd0d18dee72fceb62c66fd7a73521709b Mon Sep 17 00:00:00 2001 From: Doug Anderson444 Date: Tue, 5 Dec 2023 13:52:46 -0400 Subject: [PATCH 7/7] rm the `else` after break; as per @thomaseizinger 's comment --- examples/browser-webrtc/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/browser-webrtc/src/lib.rs b/examples/browser-webrtc/src/lib.rs index 396400bc078..9499ccbd158 100644 --- a/examples/browser-webrtc/src/lib.rs +++ b/examples/browser-webrtc/src/lib.rs @@ -60,9 +60,8 @@ pub async fn run(libp2p_endpoint: String) -> Result<(), JsError> { body.append_p("All done with pinging! ")?; break; - } else { - body.append_p(&format!("Connection closed due to: {:?}", cause))?; } + body.append_p(&format!("Connection closed due to: {:?}", cause))?; } evt => tracing::info!("Swarm event: {:?}", evt), }