-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathegctx.go
67 lines (51 loc) · 1.06 KB
/
egctx.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
67
package toolbelt
import (
"context"
"golang.org/x/sync/errgroup"
)
type ErrGroupSharedCtx struct {
eg *errgroup.Group
ctx context.Context
}
type CtxErrFunc func(ctx context.Context) error
func NewErrGroupSharedCtx(ctx context.Context, funcs ...CtxErrFunc) *ErrGroupSharedCtx {
eg, ctx := errgroup.WithContext(ctx)
egCtx := &ErrGroupSharedCtx{
eg: eg,
ctx: ctx,
}
egCtx.Go(funcs...)
return egCtx
}
func (egc *ErrGroupSharedCtx) Go(funcs ...CtxErrFunc) {
for _, f := range funcs {
fn := f
egc.eg.Go(func() error {
return fn(egc.ctx)
})
}
}
func (egc *ErrGroupSharedCtx) Wait() error {
return egc.eg.Wait()
}
type ErrGroupSeparateCtx struct {
eg *errgroup.Group
}
func NewErrGroupSeparateCtx() *ErrGroupSeparateCtx {
eg := &errgroup.Group{}
egCtx := &ErrGroupSeparateCtx{
eg: eg,
}
return egCtx
}
func (egc *ErrGroupSeparateCtx) Go(ctx context.Context, funcs ...CtxErrFunc) {
for _, f := range funcs {
fn := f
egc.eg.Go(func() error {
return fn(ctx)
})
}
}
func (egc *ErrGroupSeparateCtx) Wait() error {
return egc.eg.Wait()
}