Skip to content

kucherenkovova/safegroup

Repository files navigation

safegroup

Go Reference Go Report Card

Panic-safe drop-in replacement for x/sync/errgroup.

Don't let your whole server crash because of a single goroutine.

Installation

go get github.com/kucherenkovova/[email protected]

Usage

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")
}

About

Panic-safe drop-in replacement for x/sync/errgroup

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages