Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
bufdev committed Nov 1, 2024
1 parent 85ff5ec commit 5aa5bdf
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions private/pkg/thread/thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,13 @@ func Parallelize(ctx context.Context, jobs []func(context.Context) error, option
}
semaphoreC := make(chan struct{}, Parallelism()*multiplier)
var errs []error
var wg sync.WaitGroup
var lock sync.Mutex
addError := func(err error) {
lock.Lock()
errs = append(errs, err)
lock.Unlock()
}
var wg sync.WaitGroup
var stop bool
for _, job := range jobs {
if stop {
Expand All @@ -90,20 +95,18 @@ func Parallelize(ctx context.Context, jobs []func(context.Context) error, option
select {
case <-ctx.Done():
stop = true
errs = append(errs, ctx.Err())
addError(ctx.Err())
case semaphoreC <- struct{}{}:
select {
case <-ctx.Done():
stop = true
errs = append(errs, ctx.Err())
addError(ctx.Err())
default:
job := job
wg.Add(1)
go func() {
if err := job(ctx); err != nil {
lock.Lock()
errs = append(errs, err)
lock.Unlock()
addError(err)
if cancel != nil {
cancel()
}
Expand Down

0 comments on commit 5aa5bdf

Please sign in to comment.