Documentation
¶
Overview ¶
Package errgroup provides synchronization, error propagation, and Context cancellation for groups of goroutines working on subtasks of a common task. It wraps golang.org/x/sync/errgroup, with the addition that goroutines launched via Group.Go and Group.TryGo are wrapped with calm.Unpanic, so any panic is recovered and returned as an error rather than crashing the program.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group is a collection of goroutines working on subtasks that are part of the same overall task.
A zero Group is valid and does not cancel on error by default. A Group should not be reused for different tasks.
func WithContext ¶
WithContext returns a new Group and an associated Context derived from ctx.
The derived Context is canceled the first time a function passed to Go returns a non-nil error or the first time Wait returns, whichever occurs first.
func (*Group) Go ¶
Go calls the given function in a new goroutine. Panics inside f are recovered by calm.Unpanic and returned as errors.
The first call to return a non-nil error (or panic) cancels the group's context, if the group was created by calling WithContext. The error is then returned by Wait.
Go blocks until the new goroutine can be added without exceeding the configured limit.
Example ¶
package main
import (
"fmt"
"github.com/wood-jp/xerrors/errclass"
"github.com/wood-jp/xerrors/errgroup"
)
func main() {
g := errgroup.New()
g.Go(func() error {
panic("something went wrong")
})
err := g.Wait()
fmt.Println(errclass.GetClass(err))
}
Output: panic
func (*Group) SetLimit ¶
SetLimit limits the number of active goroutines in this group to at most n. A negative value indicates no limit. A zero value prevents new goroutines from starting.
func (*Group) TryGo ¶
TryGo calls the given function in a new goroutine only if the number of active goroutines in the group is currently below the configured limit. Panics inside f are recovered by calm.Unpanic and returned as errors.
The return value reports whether the goroutine was started. If TryGo would exceed the group's limit, it returns false without calling f.