Skip to content

Commit

Permalink
fix: use sleep instead of async_std::stream::interval
Browse files Browse the repository at this point in the history
Otherwise we may be constantly notifying
if we cannot notify all tokens within the given time interval.
  • Loading branch information
link2xt committed Apr 17, 2024
1 parent 794fbf9 commit 1365364
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/notifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use a2::{
NotificationOptions, Priority,
};
use anyhow::Result;
use async_std::prelude::*;
use log::*;

use crate::metrics::Metrics;
Expand All @@ -21,16 +20,16 @@ pub async fn start(state: State, interval: std::time::Duration) -> Result<()> {
humantime::format_duration(interval)
);

// first wakeup on startup
wakeup(db, metrics, production_client, sandbox_client, topic).await;

// create interval
let mut interval = async_std::stream::interval(interval);
while interval.next().await.is_some() {
loop {
let wakeup_start = std::time::Instant::now();
wakeup(db, metrics, production_client, sandbox_client, topic).await;
let elapsed = wakeup_start.elapsed();
info!(
"Waking up all devices took {}",
humantime::format_duration(elapsed)
);
async_std::task::sleep(interval.saturating_sub(elapsed)).await;
}

Ok(())
}

async fn wakeup(
Expand Down

0 comments on commit 1365364

Please sign in to comment.