英文:
How to filter duplicate system messages from fsnotify
问题
我使用了"github.com/fsnotify/fsnotify"来监听文件变化,但是如何过滤掉一些重复的消息呢?
func Listener() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
log.Println("event:", event.Name, event.Op)
// 以这种方式编写可以减少一些消息:
if event.Op&fsnotify.Rename == fsnotify.Rename {
// do ...
} else if event.Op&fsnotify.Create == fsnotify.Create {
// do ...
} else if event.Op&fsnotify.Write == fsnotify.Write {
// do ...
} else if event.Op&fsnotify.Remove == fsnotify.Remove {
// do ...
}
case _, ok := <-watcher.Errors:
if !ok {
return
}
}
}
}()
err = watcher.Add("e:/.../demo")
if err != nil {
log.Fatal(err)
}
<-done
}
例如,写入和创建事件会多次发生,我已经发现官方的错误已经修复了,但似乎并没有完全解决。
2022/12/12 21:00:55 event: e:\...\demo\a.bbb CREATE
2022/12/12 21:00:55 event: e:\...\demo\a.bbb CREATE
2022/12/12 21:00:55 event: e:\...\demo\a.bbb CREATE
2022/12/12 21:01:57 event: e:\...\demo.md WRITE
2022/12/12 21:01:57 event: e:\...\demo.md WRITE
2022/12/12 21:01:57 event: e:\...\demo.md WRITE
2022/12/12 21:01:57 event: e:\...\demo.md WRITE
2022/12/12 21:01:57 event: e:\...\demo.md WRITE
2022/12/12 21:01:57 event: e:\...\demo.md WRITE
2022/12/12 21:01:57 event: e:\...\demo.md WRITE
你应该如何过滤这些消息?
############################
var syncMap sync.Map
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
fPath := strings.ReplaceAll(event.Name, "\\", "/")
pathKey, _ := syncMap.Load(fPath)
if pathKey != 1 {
// ...
syncMap.Store(fPath, 1)
go func() {
time.Sleep(time.Second * 2)
syncMap.Delete(fPath)
}()
}
case _, ok := <-watcher.Errors:
if !ok {
return
}
}
}
}()
英文:
I used "github.com/fsnotify/fsnotify" to listen for file changes, but how should I filter some messages too many times?
func Listener() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
log.Println("event:", event.Name, event.Op)
// Writing in this way reduces some messages:
if event.Op&fsnotify.Rename == fsnotify.Rename {
// do ...
} else if event.Op&fsnotify.Create == fsnotify.Create {
// do ...
} else if event.Op&fsnotify.Write == fsnotify.Write {
// do ...
} else if event.Op&fsnotify.Remove == fsnotify.Remove {
// do ...
}
case _, ok := <-watcher.Errors:
if !ok {
return
}
}
}
}()
err = watcher.Add("e:/.../demo")
if err != nil {
log.Fatal(err)
}
<-done
}
For example, write, create events occur several times, and I have found that the official bug has been repaired, but it does not seem to be fully resolved
2022/12/12 21:00:55 event: e:\...\demo\a.bbb CREATE
2022/12/12 21:00:55 event: e:\...\demo\a.bbb CREATE
2022/12/12 21:00:55 event: e:\...\demo\a.bbb CREATE
2022/12/12 21:01:57 event: e:\...\demo.md WRITE
2022/12/12 21:01:57 event: e:\...\demo.md WRITE
2022/12/12 21:01:57 event: e:\...\demo.md WRITE
2022/12/12 21:01:57 event: e:\...\demo.md WRITE
2022/12/12 21:01:57 event: e:\...\demo.md WRITE
2022/12/12 21:01:57 event: e:\...\demo.md WRITE
2022/12/12 21:01:57 event: e:\...\demo.md WRITE
How should I filter messages?
############################
var syncMap sync.Map
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
fPath := strings.ReplaceAll(event.Name, "\\", "/")
pathKey, _ := syncMap.Load(fPath)
if pathKey != 1 {
// ...
syncMap.Store(fPath, 1)
go func() {
time.Sleep(time.Second * 2)
syncMap.Delete(fPath)
}()
}
case _, ok := <-watcher.Errors:
if !ok {
return
}
}
}
}()
答案1
得分: 1
如果发出事件的库不在您的控制范围内,您只能更改如何处理重复事件。您可以使用map[string]bool
来跟踪您已经看到/处理过的事件,因此您可以修改您的代码如下:
seenMap := make(map[string]bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
log.Println("event:", event.Name, event.Op)
_, seen := seenMap[event.Name]
if !seen {
// 以这种方式编写可以减少一些消息:
if event.Op&fsnotify.Rename == fsnotify.Rename {
// do ...
} else if event.Op&fsnotify.Create == fsnotify.Create {
// do ...
} else if event.Op&fsnotify.Write == fsnotify.Write {
// do ...
} else if event.Op&fsnotify.Remove == fsnotify.Remove {
// do ...
}
seenMap[event.Name] = true
}
case _, ok := <-watcher.Errors:
if !ok {
return
}
}
}
}()
请注意,这只是修改代码的一种方式,用于处理重复事件。
英文:
If the library that emits events isn't under your control, you can only change how you handle the duplicates.
You could so this with a map[string]bool to keep track of the events you have already seen/processed, so modifying your code you could do this:
seenMap := make(map[string]bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
log.Println("event:", event.Name, event.Op)
_, seen := seenMap[event.Name]
if !seen {
// Writing in this way reduces some messages:
if event.Op&fsnotify.Rename == fsnotify.Rename {
// do ...
} else if event.Op&fsnotify.Create == fsnotify.Create {
// do ...
} else if event.Op&fsnotify.Write == fsnotify.Write {
// do ...
} else if event.Op&fsnotify.Remove == fsnotify.Remove {
// do ...
}
seenMap[event.Name] = true
}
case _, ok := <-watcher.Errors:
if !ok {
return
}
}
}
}()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论