Add WaitGroup for HashingWorker

This commit is contained in:
Jan Bader 2020-11-22 01:02:07 +01:00
parent a3fa3d4e7c
commit e33d7e2ca0
2 changed files with 30 additions and 16 deletions

View File

@ -1,8 +1,11 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"log" "log"
"os"
"path/filepath"
"sync" "sync"
) )
@ -53,7 +56,7 @@ func (fm *FilesMap) IncomingWorker() {
close(fm.FilesHashing) close(fm.FilesHashing)
} }
func (fm *FilesMap) HashingWorker() { func (fm *FilesMap) HashingWorker(wg *sync.WaitGroup) {
for file := range fm.FilesHashing { for file := range fm.FilesHashing {
if *verbose { if *verbose {
fmt.Println("Hashing", file.path) fmt.Println("Hashing", file.path)
@ -69,7 +72,7 @@ func (fm *FilesMap) HashingWorker() {
file.hash = hash file.hash = hash
fm.FilesHashed <- file fm.FilesHashed <- file
} }
close(fm.FilesHashed) wg.Done()
} }
func (fm *FilesMap) HashedWorker(done chan bool) { func (fm *FilesMap) HashedWorker(done chan bool) {
@ -89,3 +92,18 @@ func (fm *FilesMap) HashedWorker(done chan bool) {
done <- true done <- true
} }
func (fm *FilesMap) WalkDirectories() {
for _, path := range flag.Args() {
filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
fm.FilesIncoming <- fileEntry{path, info.Size(), ""}
return nil
})
}
close(fm.FilesIncoming)
}

24
main.go
View File

@ -8,8 +8,10 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"strconv" "strconv"
"strings" "strings"
"sync"
) )
var fromFile = flag.String("from-file", "", "Load results file from <path>") var fromFile = flag.String("from-file", "", "Load results file from <path>")
@ -36,26 +38,20 @@ func main() {
} }
} else { } else {
done := make(chan bool) done := make(chan bool)
//for i := 0; i < runtime.GOMAXPROCS(0); i++ { wg := sync.WaitGroup{}
go filesMap.HashingWorker() for i := 0; i < runtime.GOMAXPROCS(0); i++ {
//} wg.Add(1)
go filesMap.HashingWorker(&wg)
}
go filesMap.IncomingWorker() go filesMap.IncomingWorker()
go filesMap.HashedWorker(done) go filesMap.HashedWorker(done)
for _, path := range flag.Args() { filesMap.WalkDirectories()
filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
filesMap.FilesIncoming <- fileEntry{path, info.Size(), ""} wg.Wait()
return nil close(filesMap.FilesHashed)
})
}
close(filesMap.FilesIncoming)
<-done <-done
} }