cmd/tree/validate: Fix race and drop errs chan

This commit is contained in:
Daniele Rondina 2020-08-23 12:27:51 +02:00
parent a3ada624a7
commit 908b6d2bd4

View File

@ -55,6 +55,8 @@ type ValidateOpts struct {
Mutex sync.Mutex Mutex sync.Mutex
BrokenPkgs int BrokenPkgs int
BrokenDeps int BrokenDeps int
Errors []error
} }
func (o *ValidateOpts) IncrBrokenPkgs() { func (o *ValidateOpts) IncrBrokenPkgs() {
@ -69,6 +71,12 @@ func (o *ValidateOpts) IncrBrokenDeps() {
o.BrokenDeps++ o.BrokenDeps++
} }
func (o *ValidateOpts) AddError(err error) {
o.Mutex.Lock()
defer o.Mutex.Unlock()
o.Errors = append(o.Errors, err)
}
func validatePackage(p pkg.Package, checkType string, opts *ValidateOpts, reciper tree.Builder, cacheDeps *pkg.InMemoryDatabase) error { func validatePackage(p pkg.Package, checkType string, opts *ValidateOpts, reciper tree.Builder, cacheDeps *pkg.InMemoryDatabase) error {
var errstr string var errstr string
var ans error var ans error
@ -279,8 +287,7 @@ func validatePackage(p pkg.Package, checkType string, opts *ValidateOpts, recipe
func validateWorker(i int, func validateWorker(i int,
wg *sync.WaitGroup, wg *sync.WaitGroup,
c <-chan pkg.Package, c <-chan pkg.Package,
opts *ValidateOpts, opts *ValidateOpts) {
errs chan error) {
defer wg.Done() defer wg.Done()
@ -290,14 +297,16 @@ func validateWorker(i int,
// Check buildtime compiler/deps // Check buildtime compiler/deps
err := validatePackage(p, "buildtime", opts, opts.BuildtimeReciper, opts.BuildtimeCacheDeps) err := validatePackage(p, "buildtime", opts, opts.BuildtimeReciper, opts.BuildtimeCacheDeps)
if err != nil { if err != nil {
errs <- err opts.AddError(err)
continue
} }
} else if opts.OnlyRuntime { } else if opts.OnlyRuntime {
// Check runtime installer/deps // Check runtime installer/deps
err := validatePackage(p, "runtime", opts, opts.RuntimeReciper, opts.RuntimeCacheDeps) err := validatePackage(p, "runtime", opts, opts.RuntimeReciper, opts.RuntimeCacheDeps)
if err != nil { if err != nil {
errs <- err opts.AddError(err)
continue
} }
} else { } else {
@ -305,14 +314,14 @@ func validateWorker(i int,
// Check runtime installer/deps // Check runtime installer/deps
err := validatePackage(p, "runtime", opts, opts.RuntimeReciper, opts.RuntimeCacheDeps) err := validatePackage(p, "runtime", opts, opts.RuntimeReciper, opts.RuntimeCacheDeps)
if err != nil { if err != nil {
errs <- err opts.AddError(err)
return continue
} }
// Check buildtime compiler/deps // Check buildtime compiler/deps
err = validatePackage(p, "buildtime", opts, opts.BuildtimeReciper, opts.BuildtimeCacheDeps) err = validatePackage(p, "buildtime", opts, opts.BuildtimeReciper, opts.BuildtimeCacheDeps)
if err != nil { if err != nil {
errs <- err opts.AddError(err)
} }
} }
@ -412,13 +421,12 @@ func NewTreeValidateCommand() *cobra.Command {
} }
all := make(chan pkg.Package) all := make(chan pkg.Package)
errs := make(chan error)
var wg = new(sync.WaitGroup) var wg = new(sync.WaitGroup)
for i := 0; i < concurrency; i++ { for i := 0; i < concurrency; i++ {
wg.Add(1) wg.Add(1)
go validateWorker(i, wg, all, &opts, errs) go validateWorker(i, wg, all, &opts)
} }
for _, p := range reciper.GetDatabase().World() { for _, p := range reciper.GetDatabase().World() {
all <- p all <- p
@ -428,11 +436,10 @@ func NewTreeValidateCommand() *cobra.Command {
// Wait separately and once done close the channel // Wait separately and once done close the channel
go func() { go func() {
wg.Wait() wg.Wait()
close(errs)
}() }()
stringerrs := []string{} stringerrs := []string{}
for e := range errs { for _, e := range opts.Errors {
stringerrs = append(stringerrs, e.Error()) stringerrs = append(stringerrs, e.Error())
} }
sort.Strings(stringerrs) sort.Strings(stringerrs)