-
Notifications
You must be signed in to change notification settings - Fork 0
/
calmerrgroup.go
52 lines (44 loc) · 1.07 KB
/
calmerrgroup.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
package calmerrgroup
import (
"context"
"fmt"
"runtime/debug"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
"golang.org/x/sync/errgroup"
)
// Wrapper on errgroup that suppresses panics, returning error instead
type Group struct {
inner *errgroup.Group
ctx context.Context
}
func WithContext(ctx context.Context) (*Group, context.Context) {
inner, gctx := errgroup.WithContext(ctx)
return &Group{inner: inner, ctx: gctx}, gctx
}
func (g *Group) Wait() error {
return g.inner.Wait()
}
func (g *Group) Go(f func() error) {
g.inner.Go(func() (err error) {
defer func() {
if r := recover(); r != nil {
debug.PrintStack()
if rerr, ok := r.(error); ok {
err = fmt.Errorf("errgroup panic: %w", rerr)
} else {
err = fmt.Errorf("errgroup panic: %v", r)
}
log := ctxlogrus.Extract(g.ctx)
if log != nil {
log.WithField("error.stacktrace", string(debug.Stack())).Error(err)
}
}
}()
return f()
})
}
func (g *Group) GoCtx(fn func(ctx context.Context) error) {
g.Go(func() error {
return fn(g.ctx)
})
}