如何从fsnotify中过滤重复的系统消息

huangapple go评论85阅读模式
英文:

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 := &lt;-watcher.Events:
				if !ok {
					return
				}
				log.Println(&quot;event:&quot;, event.Name, event.Op)

                // Writing in this way reduces some messages:
				if event.Op&amp;fsnotify.Rename == fsnotify.Rename {
					// do ...
				} else if event.Op&amp;fsnotify.Create == fsnotify.Create {
					// do ...
				} else if event.Op&amp;fsnotify.Write == fsnotify.Write {
					// do ...
				} else if event.Op&amp;fsnotify.Remove == fsnotify.Remove {
					// do ...
				}
			case _, ok := &lt;-watcher.Errors:
				if !ok {
					return
				}
			}
		}
	}()
	
	err = watcher.Add(&quot;e:/.../demo&quot;)
	if err != nil {
		log.Fatal(err)
	}
	&lt;-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 := &lt;-watcher.Events:
            if !ok {
                return
            }
            fPath := strings.ReplaceAll(event.Name, &quot;\\&quot;, &quot;/&quot;)
            pathKey, _ := syncMap.Load(fPath)
            if pathKey != 1 {
                // ...
                syncMap.Store(fPath, 1)

                go func() {
                    time.Sleep(time.Second * 2)
                    syncMap.Delete(fPath)
                }()
            }
        case _, ok := &lt;-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 := &lt;-watcher.Events:
			if !ok {
				return
			}
			log.Println(&quot;event:&quot;, event.Name, event.Op)
			_, seen := seenMap[event.Name]
			if !seen {
				// Writing in this way reduces some messages:
				if event.Op&amp;fsnotify.Rename == fsnotify.Rename {
					// do ...
				} else if event.Op&amp;fsnotify.Create == fsnotify.Create {
					// do ...
				} else if event.Op&amp;fsnotify.Write == fsnotify.Write {
					// do ...
				} else if event.Op&amp;fsnotify.Remove == fsnotify.Remove {
					// do ...
				}
				seenMap[event.Name] = true
			}
		case _, ok := &lt;-watcher.Errors:
			if !ok {
				return
			}
		}
	}
}()

huangapple
  • 本文由 发表于 2022年12月12日 21:06:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/74771556.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定