Skip to content

Commit c54895a

Browse files
authored
concurrency/joino (#139)
`Join` runs multiple Runners concurrently and waits for all of them to complete. It returns a combined error if any of the Runners return an error. It does not cancel the context if one of the Runners fails; all Runners are allowed to complete. Signed-off-by: joshvanl <[email protected]>
1 parent d5a23c2 commit c54895a

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

concurrency/join.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright 2025 The Dapr Authors
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package concurrency
15+
16+
import (
17+
"context"
18+
"errors"
19+
"sync"
20+
)
21+
22+
// Join runs multiple Runners concurrently and waits for all of them to
23+
// complete. It returns a combined error if any of the Runners return an error.
24+
// It does not cancel the context if one of the Runners fails; all Runners are
25+
// allowed to complete.
26+
func Join(ctx context.Context, runners ...Runner) error {
27+
errs := make([]error, len(runners))
28+
var wg sync.WaitGroup
29+
wg.Add(len(runners))
30+
for i := range runners {
31+
go func(i int) {
32+
errs[i] = runners[i](ctx)
33+
wg.Done()
34+
}(i)
35+
}
36+
wg.Wait()
37+
38+
return errors.Join(errs...)
39+
}

0 commit comments

Comments
 (0)