Skip to content

Commit

Permalink
move flush watching out of the critical path
Browse files Browse the repository at this point in the history
  • Loading branch information
arlyon committed May 5, 2023
1 parent 88d0696 commit 7d44542
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions crates/globwatch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl GlobWatcher {
std::fs::create_dir_all(&flush_dir).ok();
let flush_dir = flush_dir.canonicalize()?;

let mut watcher = notify::recommended_watcher(move |event: Result<Event, Error>| {
let watcher = notify::recommended_watcher(move |event: Result<Event, Error>| {
let span = span!(tracing::Level::TRACE, "watcher");
let _ = span.enter();

Expand All @@ -83,10 +83,25 @@ impl GlobWatcher {
}
})?;

watcher.watch(flush_dir.as_path(), notify::RecursiveMode::Recursive)?;

let watcher = Arc::new(Mutex::new(watcher));

// registering to watch this directory takes a few ms,
// so we just fire and forget a thread to do it in the
// background, to cut our startup time in half.
let flush = watcher.clone();
let path = flush_dir.as_path().to_owned();
tokio::task::spawn_blocking(move || {
if let Err(e) = flush
.lock()
.expect("only fails if poisoned")
.watch(&path, notify::RecursiveMode::Recursive)
{
warn!("failed to watch flush dir: {}", e);
} else {
trace!("watching flush dir: {:?}", path);
}
});

Ok((
Self {
flush_dir,
Expand Down

0 comments on commit 7d44542

Please sign in to comment.