fix(watcher): use try_send to prevent blocking notify callback

Replace tx.send() with tx.try_send() in file watcher notify callback
to avoid blocking the notify thread when the bounded channel (cap 100)
is full. Events are silently dropped instead of blocking, preventing
shutdown delays and event loss during consumer stalls.

Closes #7
This commit is contained in:
dailz
2026-06-04 14:33:40 +08:00
parent 1350f659fa
commit d40d70c600

View File

@@ -62,7 +62,7 @@ impl FileWatcher {
match event.kind {
EventKind::Modify(_) | EventKind::Create(_) | EventKind::Any => {}
EventKind::Remove(_) => {
let _ = tx.send(FileEvent::Removed);
let _ = tx.try_send(FileEvent::Removed);
return;
}
_ => return,
@@ -83,19 +83,19 @@ impl FileWatcher {
});
if current_inode != 0 && st.last_inode != 0 && current_inode != st.last_inode {
let _ = tx.send(FileEvent::Rotated {
let _ = tx.try_send(FileEvent::Rotated {
new_inode: current_inode,
});
st.last_inode = current_inode;
st.last_size = current_size;
} else if current_size > st.last_size {
let _ = tx.send(FileEvent::Appended {
let _ = tx.try_send(FileEvent::Appended {
new_size: current_size,
});
st.last_size = current_size;
st.last_inode = current_inode;
} else if current_size < st.last_size {
let _ = tx.send(FileEvent::Truncated {
let _ = tx.try_send(FileEvent::Truncated {
new_size: current_size,
});
st.last_size = current_size;