Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Bound asset watcher batch latency during continuous events https://github.com/siyuan-note/siyuan/issues/20972

Daniel79c9919986a9
diff --git a/kernel/model/assets_watcher.go b/kernel/model/assets_watcher.go
index bc28cad84b..3ea3ee02ec 100644
--- a/kernel/model/assets_watcher.go
+++ b/kernel/model/assets_watcher.go
@@ -72,26 +72,47 @@ func (batch assetWatchBatch) addFSNotifyEvent(event fsnotify.Event) {
func watchAssetEvents(watcher *fsnotify.Watcher) {
defer logging.Recover()
+ processAssetWatchEvents(watcher.Events, watcher.Errors, assetWatchBatch.flush)
+}
+
+func processAssetWatchEvents(events <-chan fsnotify.Event, errors <-chan error, flush func(assetWatchBatch)) {
timer := time.NewTimer(100 * time.Millisecond)
timer.Stop()
defer timer.Stop()
+ deadline := time.NewTimer(time.Second)
+ deadline.Stop()
+ defer deadline.Stop()
batch := assetWatchBatch{}
+ flushBatch := func() {
+ timer.Stop()
+ deadline.Stop()
+ flush(batch)
+ batch = assetWatchBatch{}
+ }
for {
select {
- case event, ok := <-watcher.Events:
+ case event, ok := <-events:
if !ok {
return
}
+ empty := len(batch) == 0
batch.addFSNotifyEvent(event)
- timer.Reset(100 * time.Millisecond)
- case err, ok := <-watcher.Errors:
+ if len(batch) > 0 {
+ if empty {
+ // Bound batch latency even when another path never becomes idle.
+ deadline.Reset(time.Second)
+ }
+ timer.Reset(100 * time.Millisecond)
+ }
+ case err, ok := <-errors:
if !ok {
return
}
logging.LogErrorf("watch assets failed: %s", err)
case <-timer.C:
- batch.flush()
- batch = assetWatchBatch{}
+ flushBatch()
+ case <-deadline.C:
+ flushBatch()
}
}
}
diff --git a/kernel/model/assets_watcher_test.go b/kernel/model/assets_watcher_test.go
index a6dd6365a5..b7e1cb42ee 100644
--- a/kernel/model/assets_watcher_test.go
+++ b/kernel/model/assets_watcher_test.go
@@ -4,6 +4,8 @@ package model
import (
"testing"
+ "testing/synctest"
+ "time"
"github.com/fsnotify/fsnotify"
)
@@ -31,3 +33,54 @@ func TestAssetWatcherEventBatch(t *testing.T) {
t.Fatalf("later events discarded an earlier path or sync trigger: %v", batch)
}
}
+
+func TestAssetWatcherFlushesContinuousEvents(t *testing.T) {
+ synctest.Test(t, func(t *testing.T) {
+ events := make(chan fsnotify.Event)
+ errors := make(chan error)
+ var batches []assetWatchBatch
+ go processAssetWatchEvents(events, errors, func(batch assetWatchBatch) { batches = append(batches, batch) })
+ events <- fsnotify.Event{Name: "saved.pdf", Op: fsnotify.Write}
+ for i := 0; i < 61; i++ {
+ time.Sleep(50 * time.Millisecond)
+ events <- fsnotify.Event{Name: ".continuous.tmp", Op: fsnotify.Write}
+ synctest.Wait()
+ if i == 20 && (len(batches) == 0 || !batches[0]["saved.pdf"]) {
+ t.Fatal("continuous temporary events postponed a saved asset beyond one second")
+ }
+ }
+ if len(batches) < 3 {
+ t.Fatalf("continuous events did not flush periodically: %d", len(batches))
+ }
+ time.Sleep(101 * time.Millisecond)
+ synctest.Wait()
+ if len(batches) != 4 {
+ t.Fatalf("final idle batch was not flushed: %d", len(batches))
+ }
+ close(events)
+ synctest.Wait()
+ })
+}
+
+func TestAssetWatcherDebouncesShortBurst(t *testing.T) {
+ synctest.Test(t, func(t *testing.T) {
+ events := make(chan fsnotify.Event)
+ var batches []assetWatchBatch
+ go processAssetWatchEvents(events, nil, func(batch assetWatchBatch) { batches = append(batches, batch) })
+ events <- fsnotify.Event{Name: "first", Op: fsnotify.Write}
+ time.Sleep(70 * time.Millisecond)
+ events <- fsnotify.Event{Name: "second", Op: fsnotify.Create}
+ time.Sleep(70 * time.Millisecond)
+ synctest.Wait()
+ if len(batches) != 0 {
+ t.Fatal("short burst was flushed before it became idle")
+ }
+ time.Sleep(31 * time.Millisecond)
+ synctest.Wait()
+ if len(batches) != 1 || !batches[0]["first"] || !batches[0]["second"] {
+ t.Fatalf("short burst was not coalesced: %v", batches)
+ }
+ close(events)
+ synctest.Wait()
+ })
+}