Panic-safe drop-in replacement for x/sync/errgroup.
Don't let your whole server crash because of a single goroutine.
go get github.com/kucherenkovova/[email protected]
func handleStuff(w http.ResponseWriter, req *http.Request) {
g, ctx := safegroup.WithContext(req.Context())
g.Go(queryDB)
g.Go(sendEmail)
g.Go(func() error {
panic("oops, buggy code")
return nil
})
// Wait for all tasks to complete.
if err := g.Wait(); err != nil {
if errors.Is(err, safegroup.ErrPanic) {
sendAlert(ctx, err)
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprint(w, "successfully done")
}