Fix duplicate short arg, tweak to timer fn

This commit is contained in:
Stephen Seo 2022-02-09 16:21:22 +09:00
parent 4136d82aed
commit 636e530bb4

View file

@ -20,7 +20,7 @@ const DEFAULT_PID_FILENAME: &str = "rust_network_rate_pid";
struct Opt { struct Opt {
net_dev: String, net_dev: String,
#[structopt( #[structopt(
short = "s", short = "c",
long = "disable-scaling", long = "disable-scaling",
help = "Disables byte scaling into interval files" help = "Disables byte scaling into interval files"
)] )]
@ -284,15 +284,21 @@ fn timer_execute<F>(func: F, sleep_seconds: u64) -> Result<(), String>
where where
F: std::ops::Fn() -> Result<(), String>, F: std::ops::Fn() -> Result<(), String>,
{ {
let mut instant = Instant::now() - Duration::from_secs(sleep_seconds); let mut instant = Instant::now();
let diff_duration = Duration::from_secs(sleep_seconds * 2); let sleep_duration = Duration::from_secs(sleep_seconds);
let mut duration: Duration; let half_sleep_duration = sleep_duration / 2;
let double_sleep_duration = sleep_duration * 2;
loop { loop {
func()?; func()?;
let newer_instant = Instant::now(); let newer_instant = Instant::now();
duration = diff_duration - (newer_instant - instant); let elapsed = newer_instant - instant;
// println!("{:?}", elapsed);
instant = newer_instant; instant = newer_instant;
sleep(duration); if elapsed < half_sleep_duration {
sleep(sleep_duration);
} else {
sleep(double_sleep_duration - elapsed);
}
} }
} }