Skip to content

Commit

Permalink
Fix async write to map and print result (#25)
Browse files Browse the repository at this point in the history
Fix async write to map and print result. It was sometimes cause such error:
```
runtime: fatal ERROR: concurrent map read and map write
```
  • Loading branch information
mszostok authored Mar 15, 2020
1 parent 25929b2 commit 62b4264
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
14 changes: 10 additions & 4 deletions internal/printer/tty.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@ package printer
import (
"fmt"
"strings"
"sync"
"time"

"github.com/fatih/color"
"github.com/mszostok/codeowners-validator/internal/check"
)

type TTYPrinter struct{}
type TTYPrinter struct {
m sync.RWMutex
}

func (tty *TTYPrinter) PrintCheckResult(checkName string, duration time.Duration, checkOut check.Output) {
tty.m.Lock()
defer tty.m.Unlock()

func (tty TTYPrinter) PrintCheckResult(checkName string, duration time.Duration, checkOut check.Output) {
header := color.New(color.Bold).PrintfFunc()
issueBody := color.New(color.FgWhite).PrintfFunc()
okCheck := color.New(color.FgGreen).PrintlnFunc()
Expand All @@ -32,7 +38,7 @@ func (tty TTYPrinter) PrintCheckResult(checkName string, duration time.Duration,
}
}

func (TTYPrinter) severityPrintfFunc(severity check.SeverityType) func(format string, a ...interface{}) {
func (*TTYPrinter) severityPrintfFunc(severity check.SeverityType) func(format string, a ...interface{}) {
p := color.New()
switch severity {
case check.Warning:
Expand All @@ -44,7 +50,7 @@ func (TTYPrinter) severityPrintfFunc(severity check.SeverityType) func(format st
return p.PrintfFunc()
}

func (TTYPrinter) PrintSummary(allCheck, failedChecks int) {
func (*TTYPrinter) PrintSummary(allCheck, failedChecks int) {
failures := "no"
if failedChecks > 0 {
failures = fmt.Sprintf("%d", failedChecks)
Expand Down
4 changes: 3 additions & 1 deletion internal/runner/runner_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Printer interface {
// CheckRunner runs all registered checks in parallel.
// Needs to be initialized via NewCheckRunner func.
type CheckRunner struct {
m sync.RWMutex
log logrus.FieldLogger
codeowners []codeowners.Entry
repoPath string
Expand Down Expand Up @@ -75,7 +76,6 @@ func (r *CheckRunner) Run(ctx context.Context) {

r.collectMetrics(out)

// TODO(mszostok): concurrency support (lock per print)
r.printer.PrintCheckResult(c.Name(), time.Since(startTime), out)
}(c)
}
Expand All @@ -96,6 +96,8 @@ func (r *CheckRunner) ShouldExitWithCheckFailure() bool {
}

func (r *CheckRunner) collectMetrics(checkOut check.Output) {
r.m.Lock()
defer r.m.Unlock()
for _, i := range checkOut.Issues {
r.allFoundIssues[i.Severity]++
}
Expand Down

0 comments on commit 62b4264

Please sign in to comment.