From 7c6fc489911b3446e22e908b615c6a6b33356894 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Ant=C3=B4nio=20Cardoso?= Date: Tue, 12 Nov 2024 23:05:45 -0300 Subject: [PATCH] src: lib: hub: Send a burst of initial heartbeats if needed --- src/lib/hub/actor.rs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/lib/hub/actor.rs b/src/lib/hub/actor.rs index 5c6a478..1b91df3 100644 --- a/src/lib/hub/actor.rs +++ b/src/lib/hub/actor.rs @@ -7,6 +7,7 @@ use tokio::sync::{broadcast, mpsc, RwLock}; use tracing::*; use crate::{ + cli, drivers::{Driver, DriverInfo}, hub::HubCommand, protocol::Protocol, @@ -157,11 +158,22 @@ impl HubActor { mavlink_version: 0x3, }); + let burst_size = 5; + let mut burst_msgs_counter = 0; + let mut do_burst = cli::send_initial_heartbeats(); + loop { - tokio::time::sleep(tokio::time::Duration::from_secs_f32( - 1f32.div(*frequency.read().await), - )) - .await; + let duration = if do_burst { + if burst_msgs_counter == burst_size { + do_burst = false; + } + + tokio::time::Duration::from_millis(100) + } else { + tokio::time::Duration::from_secs_f32(1f32.div(*frequency.read().await)) + }; + + tokio::time::sleep(duration).await; if bcst_sender.receiver_count().eq(&0) { continue; // Don't try to send if the channel has no subscribers yet @@ -181,6 +193,10 @@ impl HubActor { if let Err(error) = bcst_sender.send(Arc::new(message)) { error!("Failed to send HEARTBEAT message: {error}"); } + + if do_burst && burst_msgs_counter < burst_size { + burst_msgs_counter += 1; + } } }