Skip to content

Commit

Permalink
src: lib: hub: Send a burst of initial heartbeats if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
joaoantoniocardoso authored and patrickelectric committed Nov 13, 2024
1 parent dd5df92 commit 7c6fc48
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/lib/hub/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use tokio::sync::{broadcast, mpsc, RwLock};
use tracing::*;

use crate::{
cli,
drivers::{Driver, DriverInfo},
hub::HubCommand,
protocol::Protocol,
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
}
}

Expand Down

0 comments on commit 7c6fc48

Please sign in to comment.