/nursery

Control flow mechanism for managing goroutines.

Primary LanguageGo

Nursery

Build Status codecov GoDoc

Nursery provides a control flow mechanism for guaranteeing that multiple goroutines will exit before continuing execution. This library was inspired by the concept of nurseries discussed here:

https://vorpus.org/blog/notes-on-structured-concurrency-or-go-statement-considered-harmful/

Examples

// Call Supervise() to create a scope for concurrency to exist. Supervise() will block
// while goroutines run.
err := nursery.Supervise(context.Background(), func(n *nursery.N) {
    // Spawn many goroutines.
    for _, url := range urls {
        url := url
        n.Go(func() error {
            return crawl(url)
        })
    }
})

// At this point, all goroutines have exited. The value of err is the first non-nil
// error returned by a goroutine.
if err != nil {
    return err
}