Skip to content

Commit

Permalink
Use errgroup limiter where appropriate (#2926)
Browse files Browse the repository at this point in the history
* make use of errgroup.SetLimit

* here too

* gofmt
  • Loading branch information
peterebden authored Oct 17, 2023
1 parent 34cbcc0 commit 828d0d7
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 7 deletions.
4 changes: 1 addition & 3 deletions src/format/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,10 @@ func Format(config *core.Configuration, filenames []string, rewrite, quiet bool)
func formatAll(filenames <-chan string, parallelism int, rewrite, quiet bool) (bool, error) {
var changed int64
var g errgroup.Group
limiter := make(chan struct{}, parallelism)
g.SetLimit(parallelism)
for filename := range filenames {
filename := filename
g.Go(func() error {
limiter <- struct{}{}
defer func() { <-limiter }()
c, err := format(filename, rewrite, quiet)
if c {
atomic.AddInt64(&changed, 1)
Expand Down
5 changes: 1 addition & 4 deletions src/run/run_step.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,11 @@ func Run(state *core.BuildState, label core.AnnotatedOutputLabel, args []string,
func Parallel(ctx context.Context, state *core.BuildState, labels []core.AnnotatedOutputLabel, args []string, numTasks int, outputMode process.OutputMode, remote, env, detach, inTmp bool, dir string) int {
prepareRun()

limiter := make(chan struct{}, numTasks)
var g errgroup.Group
g.SetLimit(numTasks)
for _, label := range labels {
label := label // capture locally
g.Go(func() error {
limiter <- struct{}{}
defer func() { <-limiter }()

err := runWithOutput(ctx, state, label, args, outputMode, remote, env, detach, inTmp, dir)
if err != nil && ctx.Err() == nil {
log.Error("Command failed: %s", err)
Expand Down

0 comments on commit 828d0d7

Please sign in to comment.