Skip to content

go routines #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@
# Dependency directories (remove the comment below to include it)
# vendor/

dist/

inotify-proxy
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
31 changes: 23 additions & 8 deletions internal/watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import (
"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() {
Expand All @@ -33,16 +37,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)

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

func walkSingleDirectory(directoryToWalk string) {
mu.Lock()
defer mu.Unlock()
defer wg.Done()
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)

Expand Down Expand Up @@ -98,6 +111,8 @@ func isFileChanged(path string) bool {
}

func garbageCollection() {
mu.Lock()
defer mu.Unlock()
for path, _ := range fileMap {
if !util.FileExists(path) {
delete(fileMap, path)
Expand Down