From e51392fcdeae86b08595a3aa9f0b0522fa684215 Mon Sep 17 00:00:00 2001 From: Chip Senkbeil Date: Mon, 6 Jun 2022 18:47:59 -0500 Subject: [PATCH] Simplify watch event passing by dropping events rather than blocking the hot code path --- distant-core/src/constants.rs | 3 -- distant-core/src/server/distant/handler.rs | 36 ++++++++-------------- 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/distant-core/src/constants.rs b/distant-core/src/constants.rs index e67ecd6..8aa3683 100644 --- a/distant-core/src/constants.rs +++ b/distant-core/src/constants.rs @@ -10,9 +10,6 @@ pub const CLIENT_WATCHER_CAPACITY: usize = 100; /// Capacity associated with the server's file watcher to pass events outbound pub const SERVER_WATCHER_CAPACITY: usize = 10000; -/// Duration in milliseconds to sleep when reaching maximum watcher events in queue -pub const SERVER_WATCHER_PAUSE_MILLIS: u64 = 100; - /// Represents the maximum size (in bytes) that data will be read from pipes /// per individual `read` call /// diff --git a/distant-core/src/server/distant/handler.rs b/distant-core/src/server/distant/handler.rs index ccf94e4..b2bb836 100644 --- a/distant-core/src/server/distant/handler.rs +++ b/distant-core/src/server/distant/handler.rs @@ -1,5 +1,5 @@ use crate::{ - constants::{SERVER_WATCHER_CAPACITY, SERVER_WATCHER_PAUSE_MILLIS}, + constants::SERVER_WATCHER_CAPACITY, data::{ self, Change, ChangeKind, ChangeKindSet, DirEntry, FileType, Metadata, PtySize, Request, RequestData, Response, ResponseData, RunningProcess, SystemInfo, @@ -19,7 +19,7 @@ use std::{ path::{Path, PathBuf}, pin::Pin, sync::Arc, - time::{Duration, SystemTime}, + time::SystemTime, }; use tokio::{ io::{self, AsyncWriteExt}, @@ -406,28 +406,16 @@ where // with a large volume of watch requests let (tx, mut rx) = mpsc::channel(SERVER_WATCHER_CAPACITY); - let mut watcher = notify::recommended_watcher(move |res| { - let mut res = res; - - // Attempt to send our result, breaking out of the loop - // if we succeed or it is impossible, otherwise trying - // again after a brief sleep - loop { - match tx.try_send(res) { - Ok(_) => break, - Err(TrySendError::Full(x)) => { - warn!( - "Reached watcher capacity of {}! Trying again after {}ms", - SERVER_WATCHER_CAPACITY, SERVER_WATCHER_PAUSE_MILLIS - ); - res = x; - std::thread::sleep(Duration::from_millis(SERVER_WATCHER_PAUSE_MILLIS)); - } - Err(TrySendError::Closed(_)) => { - warn!("Skipping watch event because watcher channel closed"); - break; - } - } + let mut watcher = notify::recommended_watcher(move |res| match tx.try_send(res) { + Ok(_) => {} + Err(TrySendError::Full(_)) => { + warn!( + "Reached watcher capacity of {}! Dropping watcher event!", + SERVER_WATCHER_CAPACITY, + ); + } + Err(TrySendError::Closed(_)) => { + warn!("Skipping watch event because watcher channel closed"); } })?;