use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use notify::{RecursiveMode, Watcher as _};
use tokio::sync::mpsc;
use crate::error::{Error, Result};
pub const DEBOUNCE: Duration = Duration::from_millis(300);
pub const MAX_DEBOUNCE: Duration = Duration::from_secs(1);
#[derive(Debug, Default)]
struct Pending {
roots: HashMap<PathBuf, Window>,
}
#[derive(Debug)]
struct Window {
first: Instant,
last: Instant,
}
impl Pending {
fn note(&mut self, root: &Path, kind: ¬ify::EventKind, now: Instant) {
if matches!(kind, notify::EventKind::Access(_)) {
return;
}
self.roots
.entry(root.to_owned())
.and_modify(|window| window.last = now)
.or_insert(Window {
first: now,
last: now,
});
}
fn take_ready(&mut self, now: Instant) -> Vec<PathBuf> {
let ready: Vec<PathBuf> = self
.roots
.iter()
.filter(|(_, window)| {
now.duration_since(window.last) >= DEBOUNCE
|| now.duration_since(window.first) >= MAX_DEBOUNCE
})
.map(|(root, _)| root.clone())
.collect();
for root in &ready {
self.roots.remove(root);
}
ready
}
}
pub struct Watcher {
inner: Mutex<notify::RecommendedWatcher>,
roots: Arc<Mutex<Vec<PathBuf>>>,
}
impl Watcher {
pub fn start(roots: Vec<PathBuf>, tx: mpsc::Sender<PathBuf>) -> Result<Watcher> {
let known = Arc::new(Mutex::new(roots.clone()));
let pending: Arc<Mutex<Pending>> = Arc::new(Mutex::new(Pending::default()));
{
let pending = Arc::clone(&pending);
tokio::spawn(async move {
let mut ticker = tokio::time::interval(DEBOUNCE / 3);
loop {
ticker.tick().await;
let ready = pending.lock().expect("pending").take_ready(Instant::now());
for root in ready {
if tx.send(root).await.is_err() {
return;
}
}
}
});
}
let handler_roots = Arc::clone(&known);
let handler_pending = Arc::clone(&pending);
let mut watcher =
notify::recommended_watcher(move |event: notify::Result<notify::Event>| {
let Ok(event) = event else { return };
let roots = handler_roots.lock().expect("roots").clone();
for path in event.paths {
if let Some(root) = roots.iter().find(|r| path.starts_with(r)) {
handler_pending.lock().expect("pending").note(
root,
&event.kind,
Instant::now(),
);
}
}
})
.map_err(|e| Error::Sync(e.to_string()))?;
for root in &roots {
if let Err(err) = watcher.watch(root, RecursiveMode::Recursive) {
tracing::warn!(root = %root.display(), "could not watch: {err}");
}
}
Ok(Watcher {
inner: Mutex::new(watcher),
roots: known,
})
}
pub fn watch(&self, root: &Path) -> Result<()> {
self.roots.lock().expect("roots").push(root.to_owned());
self.inner
.lock()
.expect("watcher")
.watch(root, RecursiveMode::Recursive)
.map_err(|e| Error::Sync(e.to_string()))
}
pub fn unwatch(&self, root: &Path) {
self.roots.lock().expect("roots").retain(|r| r != root);
let _ = self.inner.lock().expect("watcher").unwatch(root);
}
}
#[cfg(test)]
mod tests {
use super::*;
async fn recv(rx: &mut tokio::sync::mpsc::Receiver<PathBuf>) -> Option<PathBuf> {
tokio::time::timeout(std::time::Duration::from_secs(5), rx.recv())
.await
.ok()
.flatten()
}
fn access() -> notify::EventKind {
notify::EventKind::Access(notify::event::AccessKind::Open(
notify::event::AccessMode::Any,
))
}
fn modify() -> notify::EventKind {
notify::EventKind::Modify(notify::event::ModifyKind::Data(
notify::event::DataChange::Any,
))
}
#[test]
fn an_access_event_does_not_make_a_root_due() {
let root = Path::new("/ws");
let t0 = Instant::now();
let mut pending = Pending::default();
pending.note(root, &access(), t0);
assert!(
pending.take_ready(t0 + DEBOUNCE * 10).is_empty(),
"a read must not be reported as an edit"
);
}
#[test]
fn a_steady_stream_of_changes_is_still_reported() {
let root = Path::new("/ws");
let t0 = Instant::now();
let mut pending = Pending::default();
let step = DEBOUNCE / 2;
let mut reported_at = None;
for n in 0..20 {
let now = t0 + step * n;
pending.note(root, &modify(), now);
if !pending.take_ready(now).is_empty() {
reported_at = Some(now - t0);
break;
}
}
let reported_at = reported_at.expect("the root was never reported");
assert!(
reported_at <= MAX_DEBOUNCE + step,
"reported only after {reported_at:?}"
);
}
#[cfg(target_os = "linux")]
#[tokio::test(flavor = "multi_thread")]
async fn reading_a_file_is_not_reported() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().join("ws");
std::fs::create_dir_all(&root).unwrap();
std::fs::write(root.join("note.md"), "content").unwrap();
let (tx, mut rx) = tokio::sync::mpsc::channel(16);
let _watcher = Watcher::start(vec![root.clone()], tx).unwrap();
tokio::time::sleep(DEBOUNCE * 2).await;
while rx.try_recv().is_ok() {}
let _ = std::fs::read_to_string(root.join("note.md")).unwrap();
let reported = tokio::time::timeout(DEBOUNCE * 3, rx.recv()).await;
assert!(reported.is_err(), "a read was reported: {reported:?}");
}
#[tokio::test(flavor = "multi_thread")]
async fn a_file_created_outside_the_server_is_reported() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().join("ws");
std::fs::create_dir_all(&root).unwrap();
let (tx, mut rx) = tokio::sync::mpsc::channel(16);
let _watcher = Watcher::start(vec![root.clone()], tx).unwrap();
std::fs::write(root.join("new.md"), "content").unwrap();
assert_eq!(recv(&mut rx).await.as_deref(), Some(root.as_path()));
}
#[tokio::test(flavor = "multi_thread")]
async fn a_burst_of_writes_produces_one_report() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().join("ws");
std::fs::create_dir_all(&root).unwrap();
let (tx, mut rx) = tokio::sync::mpsc::channel(64);
let _watcher = Watcher::start(vec![root.clone()], tx).unwrap();
for n in 0..20 {
std::fs::write(root.join(format!("f{n}.md")), "x").unwrap();
}
assert!(recv(&mut rx).await.is_some());
let extra = tokio::time::timeout(DEBOUNCE * 3, rx.recv()).await;
assert!(
extra.is_err(),
"the burst should have coalesced, got {extra:?}"
);
}
#[tokio::test(flavor = "multi_thread")]
async fn events_inside_the_marker_directory_are_reported_too() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().join("ws");
std::fs::create_dir_all(root.join(".test-app")).unwrap();
let (tx, mut rx) = tokio::sync::mpsc::channel(16);
let _watcher = Watcher::start(vec![root.clone()], tx).unwrap();
std::fs::write(root.join(".test-app").join("config.toml"), "x = 1").unwrap();
assert!(
recv(&mut rx).await.is_some(),
"the marker directory holds sync-id and config, which are synced"
);
}
#[tokio::test(flavor = "multi_thread")]
async fn a_watched_root_that_disappears_does_not_kill_the_watcher() {
let tmp = tempfile::tempdir().unwrap();
let going = tmp.path().join("going");
let staying = tmp.path().join("staying");
std::fs::create_dir_all(&going).unwrap();
std::fs::create_dir_all(&staying).unwrap();
let (tx, mut rx) = tokio::sync::mpsc::channel(16);
let _watcher = Watcher::start(vec![going.clone(), staying.clone()], tx).unwrap();
std::fs::remove_dir_all(&going).unwrap();
tokio::time::sleep(DEBOUNCE * 2).await;
while rx.try_recv().is_ok() {}
std::fs::write(staying.join("still-here.md"), "x").unwrap();
assert_eq!(
recv(&mut rx).await.as_deref(),
Some(staying.as_path()),
"one lost root must not stop the others being watched"
);
}
#[tokio::test(flavor = "multi_thread")]
async fn a_root_added_later_is_watched() {
let tmp = tempfile::tempdir().unwrap();
let first = tmp.path().join("first");
let second = tmp.path().join("second");
std::fs::create_dir_all(&first).unwrap();
std::fs::create_dir_all(&second).unwrap();
let (tx, mut rx) = tokio::sync::mpsc::channel(16);
let watcher = Watcher::start(vec![first], tx).unwrap();
watcher.watch(&second).unwrap();
std::fs::write(second.join("a.md"), "x").unwrap();
assert_eq!(recv(&mut rx).await.as_deref(), Some(second.as_path()));
}
}