-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
544 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package group | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/inngest/inngestgo/step" | ||
) | ||
|
||
type Result struct { | ||
Error error | ||
Value any | ||
} | ||
|
||
func Parallel( | ||
ctx context.Context, | ||
fns ...func(ctx context.Context, | ||
) (any, error)) []Result { | ||
ctx = context.WithValue(ctx, step.ParallelKey, true) | ||
|
||
results := []Result{} | ||
isPlanned := false | ||
ch := make(chan struct{}, 1) | ||
var unexpectedPanic any | ||
for _, fn := range fns { | ||
fn := fn | ||
go func(fn func(ctx context.Context) (any, error)) { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
if _, ok := r.(step.ControlHijack); ok { | ||
isPlanned = true | ||
} else { | ||
unexpectedPanic = r | ||
} | ||
} | ||
ch <- struct{}{} | ||
}() | ||
|
||
value, err := fn(ctx) | ||
results = append(results, Result{Error: err, Value: value}) | ||
}(fn) | ||
<-ch | ||
} | ||
|
||
if unexpectedPanic != nil { | ||
// Repanic to let our normal panic recovery handle it | ||
panic(unexpectedPanic) | ||
} | ||
|
||
if isPlanned { | ||
panic(step.ControlHijack{}) | ||
} | ||
|
||
return results | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.