@@ -30,21 +30,15 @@ func WithContext(ctx context.Context) (*Group, context.Context) {
30
30
// Go runs the provided f function in a dedicated goroutine and waits for its
31
31
// completion or for the parent context cancellation.
32
32
func (g * Group ) Go (f func () error ) {
33
- if g .grp == nil {
34
- g .grp = & errgroup.Group {}
35
- }
36
- g .grp .Go (g .wrap (f ))
33
+ g .getErrGroup ().Go (g .wrap (f ))
37
34
}
38
35
39
36
// Wait blocks until all function calls from the Go method have returned, then
40
37
// returns the first non-nil error (if any) from them.
41
38
// If the error group was created via WithContext then the Wait returns error
42
39
// of cancelled parent context prior any functions calls complete.
43
40
func (g * Group ) Wait () error {
44
- if g .grp == nil {
45
- g .grp = & errgroup.Group {}
46
- }
47
- return g .grp .Wait ()
41
+ return g .getErrGroup ().Wait ()
48
42
}
49
43
50
44
// SetLimit limits the number of active goroutines in this group to at most n.
@@ -55,21 +49,15 @@ func (g *Group) Wait() error {
55
49
//
56
50
// The limit must not be modified while any goroutines in the group are active.
57
51
func (g * Group ) SetLimit (n int ) {
58
- if g .grp == nil {
59
- g .grp = & errgroup.Group {}
60
- }
61
- g .grp .SetLimit (n )
52
+ g .getErrGroup ().SetLimit (n )
62
53
}
63
54
64
55
// TryGo calls the given function in a new goroutine only if the number of
65
56
// active goroutines in the group is currently below the configured limit.
66
57
//
67
58
// The return value reports whether the goroutine was started.
68
59
func (g * Group ) TryGo (f func () error ) bool {
69
- if g .grp == nil {
70
- g .grp = & errgroup.Group {}
71
- }
72
- return g .grp .TryGo (g .wrap (f ))
60
+ return g .getErrGroup ().TryGo (g .wrap (f ))
73
61
}
74
62
75
63
func (g * Group ) wrap (f func () error ) func () error {
@@ -106,3 +94,14 @@ func (g *Group) wrap(f func() error) func() error {
106
94
}
107
95
}
108
96
}
97
+
98
+ // The getErrGroup returns actual x/sync/errgroup.Group.
99
+ // If the group is not allocated it would implicitly allocate it.
100
+ // Thats allows the internal/errgroup.Group be fully
101
+ // compatible to x/sync/errgroup.Group
102
+ func (g * Group ) getErrGroup () * errgroup.Group {
103
+ if g .grp == nil {
104
+ g .grp = & errgroup.Group {}
105
+ }
106
+ return g .grp
107
+ }
0 commit comments