Skip to content

Commit 0d2f54f

Browse files
authored
Merge pull request #1 from cmuench/feature/go-routines
go routines
2 parents 55d23b4 + e1ee26e commit 0d2f54f

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@
1616
# Dependency directories (remove the comment below to include it)
1717
# vendor/
1818

19+
dist/
20+
1921
inotify-proxy

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
99
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1010
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
1111
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
12+
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
13+
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
14+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
15+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
16+
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
17+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

internal/watcher/watcher.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ import (
66
"github.com/gookit/color"
77
"github.com/karrick/godirwalk"
88
"os"
9+
"sync"
910
"time"
1011
)
1112

13+
var mu sync.Mutex
14+
var wg sync.WaitGroup
15+
1216
func visit(osPathname string, de *godirwalk.Dirent) error {
1317
// we only process files
1418
if de.IsDir() {
@@ -33,16 +37,11 @@ func Watch(includedDirectories []string, watchFrequenceSeconds int, profile stri
3337
i := 0
3438

3539
for {
40+
wg.Add(len(includedDirectories))
3641
for _, directoryToWalk := range includedDirectories {
37-
err := godirwalk.Walk(directoryToWalk, &godirwalk.Options{
38-
Callback: visit,
39-
Unsorted: true,
40-
})
41-
42-
if err != nil {
43-
panic(err)
44-
}
42+
go walkSingleDirectory(directoryToWalk)
4543
}
44+
wg.Wait()
4645

4746
time.Sleep(time.Duration(watchFrequenceSeconds) * time.Second)
4847

@@ -55,6 +54,20 @@ func Watch(includedDirectories []string, watchFrequenceSeconds int, profile stri
5554
}
5655
}
5756

57+
func walkSingleDirectory(directoryToWalk string) {
58+
mu.Lock()
59+
defer mu.Unlock()
60+
defer wg.Done()
61+
err := godirwalk.Walk(directoryToWalk, &godirwalk.Options{
62+
Callback: visit,
63+
Unsorted: true,
64+
})
65+
66+
if err != nil {
67+
panic(err)
68+
}
69+
}
70+
5871
func isFileChanged(path string) bool {
5972
fileInfo, err := os.Stat(path)
6073

@@ -98,6 +111,8 @@ func isFileChanged(path string) bool {
98111
}
99112

100113
func garbageCollection() {
114+
mu.Lock()
115+
defer mu.Unlock()
101116
for path, _ := range fileMap {
102117
if !util.FileExists(path) {
103118
delete(fileMap, path)

0 commit comments

Comments
 (0)