Skip to content

Commit

Permalink
refactor: use check_sub for sleep calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjackwills committed Nov 15, 2024
1 parent 68a6551 commit fe3696e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/docker_data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,10 @@ impl DockerData {
let mut now = std::time::Instant::now();
tokio::spawn(async move {
loop {
let to_sleep = update_duration.saturating_sub(now.elapsed());
tokio::time::sleep(to_sleep).await;
docker_tx.send(DockerMessage::Update).await.ok();
if let Some(to_sleep) = update_duration.checked_sub(now.elapsed()) {
tokio::time::sleep(to_sleep).await;
}
now = std::time::Instant::now();
}
});
Expand Down
12 changes: 8 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,20 @@ async fn main() {
Ui::create(app_data, gui_state, input_tx, is_running).await;
} else {
info!("in debug mode\n");
let mut now = std::time::Instant::now();
// Debug mode for testing, less pointless now, will display some basic information
while is_running.load(Ordering::SeqCst) {
let err = app_data.lock().get_error();
if let Some(err) = err {
error!("{}", err);
process::exit(1);
}
tokio::time::sleep(std::time::Duration::from_millis(u64::from(
args.docker_interval,
)))
.await;
if let Some(Ok(to_sleep)) = u128::from(args.docker_interval)
.checked_sub(now.elapsed().as_millis())
.map(u64::try_from)
{
tokio::time::sleep(std::time::Duration::from_millis(to_sleep)).await;
}
let containers = app_data
.lock()
.get_container_items()
Expand All @@ -148,6 +151,7 @@ async fn main() {
}
println!();
}
now = std::time::Instant::now();
}
}
}
Expand Down

0 comments on commit fe3696e

Please # to comment.