avoid spamming trigger and event channels while draining

This commit is contained in:
Dominic Breuker committed 2022-10-25 22:58:55 +02:00
1 parent 2bf9f913ae
commit d6985d700f
7 files changed
+31 -18

No files matched your search

+10
View File
@@ -25,6 +25,7 @@ type FSWatcher struct {
w Walker
maxWatchers int
eventSize int
drain bool
}
func NewFSWatcher() *FSWatcher {
@@ -33,9 +34,14 @@ func NewFSWatcher() *FSWatcher {
w: walker.NewWalker(),
maxWatchers: inotify.MaxWatchers,
eventSize: inotify.EventSize,
drain: true,
}
}
func (fs *FSWatcher) Enable() {
fs.drain = false
}
func (fs *FSWatcher) Close() {
fs.i.Close()
}
@@ -116,6 +122,10 @@ func (fs *FSWatcher) observe(triggerCh chan struct{}, dataCh chan []byte, errCh
for {
n, err := fs.i.Read(buf)
if fs.drain {
continue
}
triggerCh <- struct{}{}
if err != nil {
errCh <- fmt.Errorf("reading inotify buffer: %v", err)