From d94b0abf649be6863df984f7e790b92441803cc1 Mon Sep 17 00:00:00 2001 From: Yann Hamon Date: Sun, 31 May 2020 17:03:02 +0200 Subject: [PATCH] make logging non-concurrent, remove mutexes from output pkg --- main.go | 25 ++++++++++++++++++++----- pkg/output/json.go | 4 ---- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 01855c2..1d82d7a 100644 --- a/main.go +++ b/main.go @@ -21,9 +21,9 @@ import ( ) type validationResult struct { - kind, version string - err error - skipped bool + filename, kind, version string + err error + skipped bool } // filter returns true if the file should be skipped @@ -171,6 +171,18 @@ func realMain() int { 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() var wg sync.WaitGroup for i := 0; i < nWorkers; i++ { @@ -189,9 +201,10 @@ func realMain() int { res := validateFile(f, registries, k8sVersion, c, filter) f.Close() - for _, resourceValidation := range res { - o.Write(filename, resourceValidation.kind, resourceValidation.version, resourceValidation.err, resourceValidation.skipped) + for i, _ := range res { + res[i].filename = filename } + validationResults <- res } } }() @@ -199,6 +212,8 @@ func realMain() int { wg.Wait() o.Flush() + close(validationResults) + logWG.Wait() return 0 } diff --git a/pkg/output/json.go b/pkg/output/json.go index 2c6f6d0..5090a9e 100644 --- a/pkg/output/json.go +++ b/pkg/output/json.go @@ -3,7 +3,6 @@ package output import ( "encoding/json" "fmt" - "sync" ) type result struct { @@ -15,7 +14,6 @@ type result struct { } type JSONOutput struct { - sync.Mutex withSummary bool quiet bool 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) { - o.Lock() - defer o.Unlock() msg, st := "", "" s := status(err, skipped)