This repository has been archived by the owner on Nov 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait.go
66 lines (60 loc) · 1.57 KB
/
wait.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Package wait provides Group, an extended version of sync.WaitGroup.
//
// Deprecated: Use golang.org/x/sync/errgroup instead.
package wait
import "sync"
// A Group waits for a collection of goroutines to exit.
// With Wait, one may wait for all goroutines to exit and
// get the first error that was seen.
// Cooperative cancellation is also supported via Quit
// and the broadcast chan passed to each goroutine.
// The zero value of Group is ready to be used.
type Group struct {
wg sync.WaitGroup
mu sync.Mutex
quit chan struct{}
closed bool
err error
}
// Go runs f in a new goroutine.
// The quit chan is closed to indicate that f should exit early,
// so f is expected to periodically receive from quit
// and immediately return nil if a value arrives.
// Quit is called automatically if f returns a non-nil error.
func (g *Group) Go(f func(quit <-chan struct{}) error) {
g.mu.Lock()
if g.quit == nil {
g.quit = make(chan struct{})
}
g.mu.Unlock()
g.wg.Add(1)
go func() {
err := f(g.quit)
if err != nil {
g.mu.Lock()
if g.err == nil {
g.err = err
}
g.mu.Unlock()
g.Quit()
}
g.wg.Done()
}()
}
// Quit asks all goroutines started with Go to exit
// (by closing the quit chan given to each function).
// After the first call to Quit, subsequent calls have no effect.
func (g *Group) Quit() {
g.mu.Lock()
if !g.closed && g.quit != nil {
close(g.quit)
g.closed = true
}
g.mu.Unlock()
}
// Wait waits for all goroutines to exit and returns
// the first non-nil error seen.
func (g *Group) Wait() error {
g.wg.Wait()
return g.err
}