Skip to content

Commit

Permalink
Get rid of init. The go vet complain on noCopy is open
Browse files Browse the repository at this point in the history
  • Loading branch information
egorse committed Dec 13, 2023
1 parent c3a202d commit d94467d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 23 deletions.
28 changes: 5 additions & 23 deletions internal/errgroup/errgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,20 @@ 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) {
g.init()

if g.ctx == nil {
g.grp.Go(f)
return
}

g.grp.Go(g.wrap(f))
}

Expand All @@ -45,7 +38,6 @@ 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 {
g.init()
return g.grp.Wait()
}

Expand All @@ -57,7 +49,6 @@ 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) {
g.init()
g.grp.SetLimit(n)
}

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

return g.grp.TryGo(g.wrap(f))
}

// The init method provides backward compatibility to x/sync/errgroup.Group
// when developers can create error group by i.e. ```eg := &errgroup.Group{}```
func (g *Group) init() {
if g.grp == nil {
g.grp = &errgroup.Group{}
func (g *Group) wrap(f func() error) func() error {
if g.ctx == nil {
return f
}
}

func (g *Group) wrap(f func() error) func() error {
return func() error {
// If parent context is canceled,
// just return its error and do not call func f
Expand Down
1 change: 1 addition & 0 deletions zmq4_timeout_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2023 The go-zeromq Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package zmq4

import (
Expand Down

0 comments on commit d94467d

Please sign in to comment.