Skip to content

Commit

Permalink
Fix disappearing idle time after timeout
Browse files Browse the repository at this point in the history
The active time was reported within idle time for idle_timeout. This caused disappearing the idle event.
The temporary solution sometimes sacrificies idle_timeout of active time.
  • Loading branch information
2e3s committed Jun 24, 2024
1 parent 3e478e9 commit 123a0e4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
1 change: 0 additions & 1 deletion .github/workflows/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ jobs:
components: clippy
- uses: Swatinem/rust-cache@v2
- run: cargo clippy --locked --all-targets --all-features --workspace -- -D warnings
- run: cargo clippy --locked --all-targets --workspace -- -D warnings
clippy:
runs-on: ubuntu-latest
env:
Expand Down
43 changes: 39 additions & 4 deletions watchers/src/watchers/idle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ pub struct State {
is_idle: bool,
is_changed: bool,
idle_timeout: Duration,

idle_start: Option<DateTime<Utc>>,
idle_end: Option<DateTime<Utc>>,
}

impl State {
Expand All @@ -18,6 +21,8 @@ impl State {
is_idle: false,
is_changed: false,
idle_timeout,
idle_start: None,
idle_end: None,
}
}

Expand All @@ -30,10 +35,14 @@ impl State {
pub fn mark_not_idle(&mut self) {
self.last_input_time = Utc::now();
self.set_idle(false, self.last_input_time);

self.idle_end = self.changed_time.into();
}

pub fn mark_idle(&mut self) {
self.set_idle(true, Utc::now());

self.idle_start = self.changed_time.into();
}

// The logic is rewritten from the original Python code:
Expand Down Expand Up @@ -67,6 +76,18 @@ impl State {
let now = Utc::now();
if !self.is_idle {
self.last_input_time = max(now - self.idle_timeout, self.changed_time);
if let (Some(idle_start), Some(idle_end)) = (self.idle_start, self.idle_end) {
if !self.is_changed
&& idle_start <= self.last_input_time
&& self.last_input_time <= idle_end
{
warn!("Active time may not be accounted for.");

// TODO: send the correct timings.
// After idle_end there is some active time for idle_timeout which may be accounted as idle time if it becomes idle soon.
return Ok(());
}
}
}

self.send_ping(now, client).await
Expand All @@ -79,7 +100,11 @@ impl State {
) -> anyhow::Result<()> {
if self.is_changed {
let result = if self.is_idle {
debug!("Reporting as changed to idle");
debug!(
"Reporting as changed to idle for {} seconds since {}",
(now - self.last_input_time).num_seconds(),
self.last_input_time.format("%Y-%m-%d %H:%M:%S"),
);
client
.ping(false, self.last_input_time, Duration::zero())
.await?;
Expand All @@ -90,7 +115,10 @@ impl State {
.ping(true, self.last_input_time, now - self.last_input_time)
.await
} else {
debug!("Reporting as no longer idle");
debug!(
"Reporting as no longer idle at {}",
self.last_input_time.format("%Y-%m-%d %H:%M:%S")
);

client
.ping(true, self.last_input_time, Duration::zero())
Expand All @@ -107,12 +135,19 @@ impl State {
self.is_changed = false;
result
} else if self.is_idle {
trace!("Reporting as idle");
trace!(
"Reporting as idle for {} seconds since {}",
(now - self.last_input_time).num_seconds(),
self.last_input_time.format("%Y-%m-%d %H:%M:%S"),
);
client
.ping(true, self.last_input_time, now - self.last_input_time)
.await
} else {
trace!("Reporting as not idle");
trace!(
"Reporting as not idle at {}",
self.last_input_time.format("%Y-%m-%d %H:%M:%S")
);
client
.ping(false, self.last_input_time, Duration::zero())
.await
Expand Down

0 comments on commit 123a0e4

Please sign in to comment.