Skip to content

Update thread.ParallelizeWithCancel to thread.ParallelizeWithCancelOnFailure that no longer takes a context.CancelFunc #3246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions private/buf/bufgen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,10 @@ func (g *generator) execPlugins(
// out: gen/proto
// - name: insertion-point-writer
// out: gen/proto
ctx, cancel := context.WithCancel(ctx)
defer cancel()
if err := thread.Parallelize(
ctx,
jobs,
thread.ParallelizeWithCancel(cancel),
thread.ParallelizeWithCancelOnFailure(),
); err != nil {
if errs := multierr.Errors(err); len(errs) > 0 {
return nil, errs[0]
Expand Down
4 changes: 1 addition & 3 deletions private/bufpkg/bufprotoplugin/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ func (g *generator) Generate(
)
}
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
if err := thread.Parallelize(ctx, jobs, thread.ParallelizeWithCancel(cancel)); err != nil {
if err := thread.Parallelize(ctx, jobs, thread.ParallelizeWithCancelOnFailure()); err != nil {
return nil, err
}
codeGeneratorResponse, err := protopluginResponseWriter.ToCodeGeneratorResponse()
Expand Down
6 changes: 2 additions & 4 deletions private/pkg/bandeps/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,7 @@ func (c *checker) populateState(ctx context.Context, state *state, externalConfi
},
)
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
if err := thread.Parallelize(ctx, jobs, thread.ParallelizeWithCancel(cancel)); err != nil {
if err := thread.Parallelize(ctx, jobs, thread.ParallelizeWithCancelOnFailure()); err != nil {
return err
}

Expand All @@ -161,5 +159,5 @@ func (c *checker) populateState(ctx context.Context, state *state, externalConfi
},
)
}
return thread.Parallelize(ctx, jobs, thread.ParallelizeWithCancel(cancel))
return thread.Parallelize(ctx, jobs, thread.ParallelizeWithCancelOnFailure())
}
21 changes: 13 additions & 8 deletions private/pkg/thread/thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ func Parallelize(ctx context.Context, jobs []func(context.Context) error, option
if multiplier < 1 {
multiplier = 1
}
var cancel context.CancelFunc
if parallelizeOptions.cancelOnFailure {
ctx, cancel = context.WithCancel(ctx)
defer cancel()
}
semaphoreC := make(chan struct{}, Parallelism()*multiplier)
var retErr error
var wg sync.WaitGroup
Expand Down Expand Up @@ -100,8 +105,8 @@ func Parallelize(ctx context.Context, jobs []func(context.Context) error, option
lock.Lock()
retErr = multierr.Append(retErr, err)
lock.Unlock()
if parallelizeOptions.cancel != nil {
parallelizeOptions.cancel()
if cancel != nil {
cancel()
}
}
// This will never block.
Expand Down Expand Up @@ -129,17 +134,17 @@ func ParallelizeWithMultiplier(multiplier int) ParallelizeOption {
}
}

// ParallelizeWithCancel returns a new ParallelizeOption that will call the
// given context.CancelFunc if any job fails.
func ParallelizeWithCancel(cancel context.CancelFunc) ParallelizeOption {
// ParallelizeWithCancelOnFailure returns a new ParallelizeOption that will attempt
// to cancel all other jobs via context cancellation if any job fails.
func ParallelizeWithCancelOnFailure() ParallelizeOption {
return func(parallelizeOptions *parallelizeOptions) {
parallelizeOptions.cancel = cancel
parallelizeOptions.cancelOnFailure = true
}
}

type parallelizeOptions struct {
multiplier int
cancel context.CancelFunc
multiplier int
cancelOnFailure bool
}

func newParallelizeOptions() *parallelizeOptions {
Expand Down