Skip to content

Commit

Permalink
Fix go vet errors
Browse files Browse the repository at this point in the history
Unfortunatelly the errgroup.Group is not copyable so
had to handle implicity initialization in all methods.
  • Loading branch information
egorse committed Dec 13, 2023
1 parent d94467d commit a5b84a6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
16 changes: 14 additions & 2 deletions internal/errgroup/errgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,23 @@ import (
// The Group is superior errgroup.Group which aborts whole group
// execution when parent context is cancelled
type Group struct {
grp errgroup.Group
grp *errgroup.Group
ctx context.Context
}

// WithContext creates Group and store inside parent context
// so the Go method would respect parent context cancellation
func WithContext(ctx context.Context) (*Group, context.Context) {
grp, child_ctx := errgroup.WithContext(ctx)
return &Group{grp: *grp, ctx: ctx}, child_ctx
return &Group{grp: grp, ctx: ctx}, child_ctx
}

// Go runs the provided f function in a dedicated goroutine and waits for its
// completion or for the parent context cancellation.
func (g *Group) Go(f func() error) {
if g.grp == nil {
g.grp = &errgroup.Group{}
}
g.grp.Go(g.wrap(f))
}

Expand All @@ -38,6 +41,9 @@ func (g *Group) Go(f func() error) {
// If the error group was created via WithContext then the Wait returns error
// of cancelled parent context prior any functions calls complete.
func (g *Group) Wait() error {
if g.grp == nil {
g.grp = &errgroup.Group{}
}
return g.grp.Wait()
}

Expand All @@ -49,6 +55,9 @@ func (g *Group) Wait() error {
//
// The limit must not be modified while any goroutines in the group are active.
func (g *Group) SetLimit(n int) {
if g.grp == nil {
g.grp = &errgroup.Group{}
}
g.grp.SetLimit(n)
}

Expand All @@ -57,6 +66,9 @@ func (g *Group) SetLimit(n int) {
//
// The return value reports whether the goroutine was started.
func (g *Group) TryGo(f func() error) bool {
if g.grp == nil {
g.grp = &errgroup.Group{}
}
return g.grp.TryGo(g.wrap(f))
}

Expand Down
1 change: 1 addition & 0 deletions rep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func TestCancellation(t *testing.T) {

defer wg.Done()
repCtx, cancel := context.WithCancel(context.Background())
defer cancel()
rep := zmq4.NewRep(repCtx)
defer rep.Close()

Expand Down

0 comments on commit a5b84a6

Please sign in to comment.