From 6a400dd85041220794ff06e96565382fb19ca248 Mon Sep 17 00:00:00 2001 From: dr-frmr Date: Wed, 8 May 2024 13:24:43 -0600 Subject: [PATCH] fix: kill node<>node eth subs after 2 hours no data --- kinode/src/eth/subscription.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/kinode/src/eth/subscription.rs b/kinode/src/eth/subscription.rs index 79b92d79..d071086a 100644 --- a/kinode/src/eth/subscription.rs +++ b/kinode/src/eth/subscription.rs @@ -300,6 +300,7 @@ async fn build_subscription( } } } + response_channels.remove(&km_id); return Err(EthError::NoRpcForChain); } @@ -338,6 +339,9 @@ async fn maintain_local_subscription( /// handle the subscription updates from a remote provider, /// and also perform keepalive checks on that provider. /// current keepalive is 30s, this can be adjusted as desired +/// +/// if the subscription goes more than 2 hours without an update, +/// the provider will be considered dead and the subscription will be closed. async fn maintain_remote_subscription( our: &str, provider_node: &str, @@ -350,11 +354,16 @@ async fn maintain_remote_subscription( send_to_loop: &MessageSender, ) -> Result<(), EthSubError> { let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(30)); + let mut last_received = tokio::time::Instant::now(); + let two_hours = tokio::time::Duration::from_secs(2 * 3600); + loop { tokio::select! { incoming = rx.recv() => { match incoming { Some(EthSubResult::Ok(upd)) => { + // Update the last received time on any successful sub result + last_received = tokio::time::Instant::now(); kernel_message( &our, rand::random(), @@ -381,7 +390,6 @@ async fn maintain_remote_subscription( id: sub_id, error: "subscription closed unexpectedly".to_string(), }); - } } } @@ -406,6 +414,12 @@ async fn maintain_remote_subscription( }); } } + _ = tokio::time::sleep_until(last_received + two_hours) => { + return Err(EthSubError { + id: sub_id, + error: "No updates received for 2 hours, subscription considered dead.".to_string(), + }); + } } } }