Use channels over WaitGroup in needsUpdate()
Using a WaitGroup forced the code to wait for every goroutine to finish. Using channels allows us to exit early if any of a packages sources need an update. No point in waiting for the other requests to finish if we know there is an update.
This commit is contained in:
1 file changed
+21
-9
@@ -6,7 +6,6 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -143,25 +142,38 @@ func getCommit(url string, branch string, protocols []string) string {
|
||||
}
|
||||
|
||||
func (infos shaInfos) needsUpdate() bool {
|
||||
var wg sync.WaitGroup
|
||||
hasUpdate := false
|
||||
//used to signal we have gone through all sources and found nothing
|
||||
finished := make(chan struct{})
|
||||
alive := 0
|
||||
|
||||
//if we find an update we use this to exit early and return true
|
||||
hasUpdate := make(chan struct{})
|
||||
|
||||
checkHash := func(url string, info shaInfo) {
|
||||
defer wg.Done()
|
||||
hash := getCommit(url, info.Brach, info.Protocols)
|
||||
if hash != "" && hash != info.SHA {
|
||||
hasUpdate = true
|
||||
hasUpdate <- struct{}{}
|
||||
} else {
|
||||
finished <- struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
for url, info := range infos {
|
||||
wg.Add(1)
|
||||
alive++
|
||||
go checkHash(url, info)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
return hasUpdate
|
||||
for {
|
||||
select {
|
||||
case <- hasUpdate:
|
||||
return true
|
||||
case <- finished:
|
||||
alive--
|
||||
if alive == 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func inStore(pkgName string) shaInfos {
|
||||
|
||||
Reference in new issue
Block a user