Skip to content

Commit

Permalink
[checks] introduce semaphore
Browse files Browse the repository at this point in the history
  • Loading branch information
Songmu committed Jan 15, 2019
1 parent 2eacd45 commit 00a1ef0
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions checks/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os"
"runtime"
"strings"
"sync"

Expand Down Expand Up @@ -111,13 +112,16 @@ type checker interface {
func runChecks(checkers []checker, w io.Writer) error {
ch := make(chan *result)
total := len(checkers)
sem := make(chan struct{}, runtime.NumCPU()*2)
go func() {
wg := &sync.WaitGroup{}
wg.Add(total)
for _, c := range checkers {
go func(c checker) {
defer wg.Done()
sem <- struct{}{}
ch <- c.check()
<-sem
}(c)
}
wg.Wait()
Expand Down

0 comments on commit 00a1ef0

Please sign in to comment.