From 5a89a053153b5ef3b80cdc5039124228f617cb93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=BCnch?= Date: Wed, 26 Aug 2020 10:04:36 +0200 Subject: [PATCH 1/2] Add go routines for parallel processing --- .gitignore | 2 ++ internal/watcher/watcher.go | 33 +++++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 2b9f596..68c5776 100644 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,6 @@ # Dependency directories (remove the comment below to include it) # vendor/ +dist/ + inotify-proxy diff --git a/internal/watcher/watcher.go b/internal/watcher/watcher.go index 9928aa0..8b92cae 100644 --- a/internal/watcher/watcher.go +++ b/internal/watcher/watcher.go @@ -1,14 +1,19 @@ package watcher import ( + "fmt" "github.com/cmuench/inotify-proxy/internal/profile/validator" "github.com/cmuench/inotify-proxy/internal/util" "github.com/gookit/color" "github.com/karrick/godirwalk" "os" + "sync" "time" ) +var mu sync.Mutex +var wg sync.WaitGroup + func visit(osPathname string, de *godirwalk.Dirent) error { // we only process files if de.IsDir() { @@ -33,16 +38,11 @@ func Watch(includedDirectories []string, watchFrequenceSeconds int, profile stri i := 0 for { + wg.Add(len(includedDirectories)) for _, directoryToWalk := range includedDirectories { - err := godirwalk.Walk(directoryToWalk, &godirwalk.Options{ - Callback: visit, - Unsorted: true, - }) - - if err != nil { - panic(err) - } + go walkSingleDirectory(directoryToWalk) } + wg.Wait() time.Sleep(time.Duration(watchFrequenceSeconds) * time.Second) @@ -55,6 +55,21 @@ func Watch(includedDirectories []string, watchFrequenceSeconds int, profile stri } } +func walkSingleDirectory(directoryToWalk string) { + mu.Lock() + defer mu.Unlock() + defer wg.Done() + fmt.Println("Watch " + directoryToWalk) + err := godirwalk.Walk(directoryToWalk, &godirwalk.Options{ + Callback: visit, + Unsorted: true, + }) + + if err != nil { + panic(err) + } +} + func isFileChanged(path string) bool { fileInfo, err := os.Stat(path) @@ -98,6 +113,8 @@ func isFileChanged(path string) bool { } func garbageCollection() { + mu.Lock() + defer mu.Unlock() for path, _ := range fileMap { if !util.FileExists(path) { delete(fileMap, path) From e1ee26e505da14ac826b7b12541d7693b7966bca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=BCnch?= Date: Sat, 29 Aug 2020 15:04:23 +0200 Subject: [PATCH 2/2] Remove debug message --- go.sum | 6 ++++++ internal/watcher/watcher.go | 2 -- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/go.sum b/go.sum index da2ff0d..d7aa59b 100644 --- a/go.sum +++ b/go.sum @@ -9,3 +9,9 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/internal/watcher/watcher.go b/internal/watcher/watcher.go index 8b92cae..85cb500 100644 --- a/internal/watcher/watcher.go +++ b/internal/watcher/watcher.go @@ -1,7 +1,6 @@ package watcher import ( - "fmt" "github.com/cmuench/inotify-proxy/internal/profile/validator" "github.com/cmuench/inotify-proxy/internal/util" "github.com/gookit/color" @@ -59,7 +58,6 @@ func walkSingleDirectory(directoryToWalk string) { mu.Lock() defer mu.Unlock() defer wg.Done() - fmt.Println("Watch " + directoryToWalk) err := godirwalk.Walk(directoryToWalk, &godirwalk.Options{ Callback: visit, Unsorted: true,