Skip to content

Commit

Permalink
Re-structure according to fsnotify recommendations
Browse files Browse the repository at this point in the history
For some very obscure reason (or let's call it "badly explained"), the reading
from the event and error channel should take place in a separate goroutine.
See howeyc/fsnotify#7 for a discussion about it.
Apparently, something may block if done otherwise.

Although all material about this topic fails to give a code example that
exhibits this problem, I obey and put the events reader into its own goroutine.
  • Loading branch information
bronger committed May 23, 2021
1 parent a7c34d3 commit 52f1f8a
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions watchdog.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,8 @@ func longestPrefix(paths []string) string {
return result
}

func getWatcher() *fsnotify.Watcher {
watcher, err := fsnotify.NewWatcher()
check(err)
err = filepath.WalkDir(os.Args[2],
func addWatches(watcher *fsnotify.Watcher) {
err := filepath.WalkDir(os.Args[2],
func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
Expand All @@ -86,7 +84,6 @@ func getWatcher() *fsnotify.Watcher {
return nil
})
check(err)
return watcher
}

func eventsWatcher(watcher *fsnotify.Watcher, workItems chan<- workItem) {
Expand Down Expand Up @@ -186,11 +183,15 @@ func worker(workPackages <-chan []workItem) {
}

func main() {
watcher := getWatcher()
workItems := make(chan workItem)
workPackages := make(chan []workItem)
go workMarshaller(workItems, workPackages)
go worker(workPackages)
watcher, err := fsnotify.NewWatcher()
check(err)
done := make(chan bool)
go eventsWatcher(watcher, workItems)
logger.Println("Watching …")
eventsWatcher(watcher, workItems)
addWatches(watcher)
<-done
}

0 comments on commit 52f1f8a

Please sign in to comment.