fix: track completed items in the main loop instead of using the index
This commit is contained in:
@@ -261,6 +261,7 @@ func processItemsInParallelNoResult[T any](
|
|||||||
) error {
|
) error {
|
||||||
count := len(items)
|
count := len(items)
|
||||||
errors := make(chan error, count)
|
errors := make(chan error, count)
|
||||||
|
completions := make(chan struct{}, count)
|
||||||
|
|
||||||
semaphore := make(chan struct{}, maxWorkers)
|
semaphore := make(chan struct{}, maxWorkers)
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
@@ -288,20 +289,45 @@ func processItemsInParallelNoResult[T any](
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if progress != nil {
|
completions <- struct{}{}
|
||||||
progress.Update(index + 1)
|
|
||||||
}
|
|
||||||
}(i, item)
|
}(i, item)
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
close(errors)
|
close(errors)
|
||||||
|
close(completions)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
completed := 0
|
||||||
|
firstError := make(chan error, 1)
|
||||||
|
|
||||||
|
go func() {
|
||||||
for err := range errors {
|
for err := range errors {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
select {
|
||||||
|
case firstError <- err:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
for completed < count {
|
||||||
|
select {
|
||||||
|
case _, ok := <-completions:
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
completed++
|
||||||
|
if progress != nil {
|
||||||
|
progress.Update(completed)
|
||||||
|
}
|
||||||
|
case err := <-firstError:
|
||||||
return err
|
return err
|
||||||
|
case <-ctx.Done():
|
||||||
|
return fmt.Errorf("timeout: %w", ctx.Err())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user