perf: throttle watch updates (#2311)

* perf: throttle watch updates

* Fix file not exist error in the UI

* Use separate timeout for each file
This commit is contained in:
Joe Lim authored and GitHub committed 2025-12-19 19:08:55 +11:00
1 parent c3dbf0ab01
commit f0f7cfbbed
2 files changed
+60 -53

No files matched your search

+4 -1
View File
@@ -175,7 +175,10 @@ func readdir(path string) ([]*file, error) {
files := make([]*file, 0, len(names)) files := make([]*file, 0, len(names))
for _, fname := range names { for _, fname := range names {
files = append(files, newFile(filepath.Join(path, fname))) file := newFile(filepath.Join(path, fname))
if !os.IsNotExist(file.err) {
files = append(files, file)
}
} }
return files, err return files, err
+56 -52
View File
@@ -10,29 +10,27 @@ import (
) )
type watch struct { type watch struct {
watcher *fsnotify.Watcher watcher *fsnotify.Watcher
events <-chan fsnotify.Event events <-chan fsnotify.Event
quit chan struct{} quit chan struct{}
loads map[string]bool pending map[watchUpdate]bool
loadTimer *time.Timer timeout chan watchUpdate
updates map[string]bool dirChan chan<- *dir
updateTimer *time.Timer fileChan chan<- *file
dirChan chan<- *dir delChan chan<- string
fileChan chan<- *file
delChan chan<- string
} }
func newWatch(dirChan chan<- *dir, fileChan chan<- *file, delChan chan<- string) *watch { func newWatch(dirChan chan<- *dir, fileChan chan<- *file, delChan chan<- string) *watch {
return &watch{ watch := &watch{
quit: make(chan struct{}), quit: make(chan struct{}),
loads: make(map[string]bool), pending: make(map[watchUpdate]bool),
loadTimer: time.NewTimer(0), timeout: make(chan watchUpdate, 1024),
updates: make(map[string]bool), dirChan: dirChan,
updateTimer: time.NewTimer(0), fileChan: fileChan,
dirChan: dirChan, delChan: delChan,
fileChan: fileChan,
delChan: delChan,
} }
return watch
} }
func (watch *watch) start() { func (watch *watch) start() {
@@ -83,8 +81,8 @@ func (watch *watch) loop() {
case ev := <-watch.events: case ev := <-watch.events:
if ev.Has(fsnotify.Create) { if ev.Has(fsnotify.Create) {
for _, path := range watch.getSameDirs(filepath.Dir(ev.Name)) { for _, path := range watch.getSameDirs(filepath.Dir(ev.Name)) {
watch.addLoad(path) watch.addUpdate(watchUpdate{"dir", path})
watch.addUpdate(path) watch.addUpdate(watchUpdate{"file", path})
} }
} }
@@ -92,8 +90,8 @@ func (watch *watch) loop() {
dir, file := filepath.Split(ev.Name) dir, file := filepath.Split(ev.Name)
for _, path := range watch.getSameDirs(dir) { for _, path := range watch.getSameDirs(dir) {
watch.delChan <- filepath.Join(path, file) watch.delChan <- filepath.Join(path, file)
watch.addLoad(path) watch.addUpdate(watchUpdate{"dir", path})
watch.addUpdate(path) watch.addUpdate(watchUpdate{"file", path})
} }
} }
@@ -108,47 +106,53 @@ func (watch *watch) loop() {
dir, file := filepath.Split(ev.Name) dir, file := filepath.Split(ev.Name)
for _, path := range watch.getSameDirs(dir) { for _, path := range watch.getSameDirs(dir) {
watch.addUpdate(filepath.Join(path, file)) watch.addUpdate(watchUpdate{"file", filepath.Join(path, file)})
} }
} }
case <-watch.loadTimer.C: case update := <-watch.timeout:
for path := range watch.loads { if watch.pending[update] {
if _, err := os.Lstat(path); err != nil { watch.processUpdate(update)
continue time.AfterFunc(100*time.Millisecond, func() { watch.timeout <- update })
} watch.pending[update] = false
dir := newDir(path) } else {
dir.sort() delete(watch.pending, update)
watch.dirChan <- dir
} }
clear(watch.loads)
case <-watch.updateTimer.C:
for path := range watch.updates {
if _, err := os.Lstat(path); err != nil {
continue
}
watch.fileChan <- newFile(path)
}
clear(watch.updates)
case <-watch.quit: case <-watch.quit:
return return
} }
} }
} }
func (watch *watch) addLoad(path string) { type watchUpdate struct {
if len(watch.loads) == 0 { kind string
watch.loadTimer.Stop() path string
watch.loadTimer.Reset(10 * time.Millisecond)
}
watch.loads[path] = true
} }
func (watch *watch) addUpdate(path string) { func (watch *watch) addUpdate(update watchUpdate) {
if len(watch.updates) == 0 { // process an update immediately if is the first time, otherwise store it
watch.updateTimer.Stop() // and process only after a timeout to reduce the number of actual loads
watch.updateTimer.Reset(10 * time.Millisecond) if _, ok := watch.pending[update]; !ok {
watch.processUpdate(update)
time.AfterFunc(100*time.Millisecond, func() { watch.timeout <- update })
watch.pending[update] = false
} else {
watch.pending[update] = true
}
}
func (watch *watch) processUpdate(update watchUpdate) {
switch update.kind {
case "dir":
if _, err := os.Lstat(update.path); err == nil {
dir := newDir(update.path)
dir.sort()
watch.dirChan <- dir
}
case "file":
if _, err := os.Lstat(update.path); err == nil {
watch.fileChan <- newFile(update.path)
}
} }
watch.updates[path] = true
} }
// Hacky workaround since fsnotify reports changes for only one path if a // Hacky workaround since fsnotify reports changes for only one path if a