errgroup

package
v0.0.0-...-83adff0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 6, 2020 License: GPL-3.0 Imports: 4 Imported by: 0

README

go-common/errgroup

提供带recover的errgroup,err中包含详细堆栈信息

Documentation

Overview

Package errgroup provides synchronization, error propagation, and Context cancelation for groups of goroutines working on subtasks of a common task.

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
}

A 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.

Example (Ctx)
g, ctx := WithContext(context.Background())
g.Go(func() error {
	return fakeRunTask(ctx)
})
g.Go(func() error {
	return fakeRunTask(ctx)
})
if err := g.Wait(); err != nil {
	// handle err
}
Output:

Example (Group)
g := Group{}
g.Go(func() error {
	return fakeRunTask(context.Background())
})
g.Go(func() error {
	return fakeRunTask(context.Background())
})
if err := g.Wait(); err != nil {
	// handle err
}
Output:

Example (JustErrors)

JustErrors illustrates the use of a Group in place of a sync.WaitGroup to simplify goroutine counting and error handling. This example is derived from the sync.WaitGroup example at https://golang.org/pkg/sync/#example_WaitGroup.

var g Group
var urls = []string{
	"http://www.golang.org/",
	"http://www.google.com/",
	"http://www.somestupidname.com/",
}
for _, url := range urls {
	// Launch a goroutine to fetch the URL.
	url := url // https://golang.org/doc/faq#closures_and_goroutines
	g.Go(func() error {
		// Fetch the URL.
		resp, err := http.Get(url)
		if err == nil {
			resp.Body.Close()
		}
		return err
	})
}
// Wait for all HTTP fetches to complete.
if err := g.Wait(); err == nil {
	fmt.Println("Successfully fetched all URLs.")
}
Output:

Example (Maxproc)
g := Group{}
// set max concurrency
g.GOMAXPROCS(2)
g.Go(func() error {
	return fakeRunTask(context.Background())
})
g.Go(func() error {
	return fakeRunTask(context.Background())
})
if err := g.Wait(); err != nil {
	// handle err
}
Output:

Example (Parallel)

Parallel illustrates the use of a Group for synchronizing a simple parallel task: the "Google Search 2.0" function from https://talks.golang.org/2012/concurrency.slide#46, augmented with a Context and error-handling.

Google := func(ctx context.Context, query string) ([]Result, error) {
	g, ctx := WithContext(ctx)

	searches := []Search{Web, Image, Video}
	results := make([]Result, len(searches))
	for i, search := range searches {
		i, search := i, search // https://golang.org/doc/faq#closures_and_goroutines
		g.Go(func() error {
			result, err := search(ctx, query)
			if err == nil {
				results[i] = result
			}
			return err
		})
	}
	if err := g.Wait(); err != nil {
		return nil, err
	}
	return results, nil
}

results, err := Google(context.Background(), "golang")
if err != nil {
	fmt.Fprintln(os.Stderr, err)
	return
}
for _, result := range results {
	fmt.Println(result)
}
Output:

web result for "golang"
image result for "golang"
video result for "golang"
Example (Waitgroup)
var wg sync.WaitGroup
wg.Add(2)
go func() {
	// do something
	wg.Done()
}()
go func() {
	// do something
	wg.Done()
}()
wg.Wait()
Output:

func WithContext

func WithContext(ctx context.Context) (*Group, context.Context)

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) GOMAXPROCS

func (g *Group) GOMAXPROCS(n int)

GOMAXPROCS set max goroutine to work.

func (*Group) Go

func (g *Group) Go(f func() error)

Go calls the given function in a new goroutine.

The first call to return a non-nil error cancels the group; its error will be returned by Wait.

func (*Group) Wait

func (g *Group) Wait() error

Wait blocks until all function calls from the Go method have returned, then returns the first non-nil error (if any) from them.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL