From 636e530bb4c48a7d9def04e1aa27d6746e0176a3 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 9 Feb 2022 16:21:22 +0900 Subject: [PATCH] Fix duplicate short arg, tweak to timer fn --- src/main.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index f7bcec7..81c34f1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,7 @@ const DEFAULT_PID_FILENAME: &str = "rust_network_rate_pid"; struct Opt { net_dev: String, #[structopt( - short = "s", + short = "c", long = "disable-scaling", help = "Disables byte scaling into interval files" )] @@ -284,15 +284,21 @@ fn timer_execute(func: F, sleep_seconds: u64) -> Result<(), String> where F: std::ops::Fn() -> Result<(), String>, { - let mut instant = Instant::now() - Duration::from_secs(sleep_seconds); - let diff_duration = Duration::from_secs(sleep_seconds * 2); - let mut duration: Duration; + let mut instant = Instant::now(); + let sleep_duration = Duration::from_secs(sleep_seconds); + let half_sleep_duration = sleep_duration / 2; + let double_sleep_duration = sleep_duration * 2; loop { func()?; let newer_instant = Instant::now(); - duration = diff_duration - (newer_instant - instant); + let elapsed = newer_instant - instant; + // println!("{:?}", elapsed); instant = newer_instant; - sleep(duration); + if elapsed < half_sleep_duration { + sleep(sleep_duration); + } else { + sleep(double_sleep_duration - elapsed); + } } }