From e9cd5d3dca48d1dc5824c1a8909de3f811b4fd04 Mon Sep 17 00:00:00 2001 From: Ani Betts Date: Wed, 15 May 2024 16:53:56 +0200 Subject: [PATCH] Rewrite the timeouts Change the strategy of how timeouts work, from a simple timer, to something a bit more nuanced --- crates/gitbutler-watcher/src/file_monitor.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/crates/gitbutler-watcher/src/file_monitor.rs b/crates/gitbutler-watcher/src/file_monitor.rs index 40d7081a0..4cb839171 100644 --- a/crates/gitbutler-watcher/src/file_monitor.rs +++ b/crates/gitbutler-watcher/src/file_monitor.rs @@ -11,11 +11,20 @@ use notify::{RecommendedWatcher, Watcher}; use tokio::task; use tracing::Level; -/// The timeout for debouncing file change events. -/// This is used to prevent multiple events from being sent for a single file change. +/// We will collect notifications for up to this amount of time at a very +/// maximum before releasing them. This duration will be hit if e.g. a build +/// is constantly running and producing a lot of file changes, we will process +/// them even if the build is still running. const DEBOUNCE_TIMEOUT: Duration = Duration::from_secs(60); + +// The internal rate at which the debouncer will update its state. const TICK_RATE: Duration = Duration::from_millis(250); -const FLUSH_AFTER_EMPTY: u32 = 3; + +// The number of TICK_RATE intervals required of "dead air" (i.e. no new events +// arriving) before we will automatically flush pending events. This means that +// after the disk is quiet for TICK_RATE * FLUSH_AFTER_EMPTY, we will process +// the pending events, even if DEBOUNCE_TIMEOUT hasn't expired yet +const FLUSH_AFTER_EMPTY: u32 = 6; /// This error is required only because `anyhow::Error` isn't implementing `std::error::Error`, and [`spawn()`] /// needs to wrap it into a `backoff::Error` which also has to implement the `Error` trait.