make logging non-concurrent, remove mutexes from output pkg

This commit is contained in:
Yann Hamon 2020-05-31 17:03:02 +02:00
parent 05da409a0a
commit d94b0abf64
2 changed files with 20 additions and 9 deletions

25
main.go
View file

@ -21,9 +21,9 @@ import (
) )
type validationResult struct { type validationResult struct {
kind, version string filename, kind, version string
err error err error
skipped bool skipped bool
} }
// filter returns true if the file should be skipped // filter returns true if the file should be skipped
@ -171,6 +171,18 @@ func realMain() int {
close(fileBatches) close(fileBatches)
}() }()
validationResults := make(chan []validationResult)
var logWG sync.WaitGroup
logWG.Add(1)
go func() {
defer logWG.Done()
for results := range validationResults {
for _, result := range results {
o.Write(result.filename, result.kind, result.version, result.err, result.skipped)
}
}
}()
c := cache.NewSchemaCache() c := cache.NewSchemaCache()
var wg sync.WaitGroup var wg sync.WaitGroup
for i := 0; i < nWorkers; i++ { for i := 0; i < nWorkers; i++ {
@ -189,9 +201,10 @@ func realMain() int {
res := validateFile(f, registries, k8sVersion, c, filter) res := validateFile(f, registries, k8sVersion, c, filter)
f.Close() f.Close()
for _, resourceValidation := range res { for i, _ := range res {
o.Write(filename, resourceValidation.kind, resourceValidation.version, resourceValidation.err, resourceValidation.skipped) res[i].filename = filename
} }
validationResults <- res
} }
} }
}() }()
@ -199,6 +212,8 @@ func realMain() int {
wg.Wait() wg.Wait()
o.Flush() o.Flush()
close(validationResults)
logWG.Wait()
return 0 return 0
} }

View file

@ -3,7 +3,6 @@ package output
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"sync"
) )
type result struct { type result struct {
@ -15,7 +14,6 @@ type result struct {
} }
type JSONOutput struct { type JSONOutput struct {
sync.Mutex
withSummary bool withSummary bool
quiet bool quiet bool
results []result results []result
@ -35,8 +33,6 @@ func NewJSONOutput(withSummary bool, quiet bool) Output {
} }
func (o *JSONOutput) Write(filename, kind, version string, err error, skipped bool) { func (o *JSONOutput) Write(filename, kind, version string, err error, skipped bool) {
o.Lock()
defer o.Unlock()
msg, st := "", "" msg, st := "", ""
s := status(err, skipped) s := status(err, skipped)