Search code examples
gogo-fsnotify

How to filter duplicate system messages from fsnotify


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\2.md WRITE
2022/12/12 21:01:57 event: e:\...\demo\2.md WRITE
2022/12/12 21:01:57 event: e:\...\demo\2.md WRITE
2022/12/12 21:01:57 event: e:\...\demo\2.md WRITE
2022/12/12 21:01:57 event: e:\...\demo\2.md WRITE
2022/12/12 21:01:57 event: e:\...\demo\2.md WRITE
2022/12/12 21:01:57 event: e:\...\demo\2.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
            }
        }
    }
}()

Solution

  • 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
                }
            }
        }
    }()