diff --git a/internal/errgroup/errgroup.go b/internal/errgroup/errgroup.go index e6d1ae1..984bdf0 100644 --- a/internal/errgroup/errgroup.go +++ b/internal/errgroup/errgroup.go @@ -16,7 +16,7 @@ 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 } @@ -24,19 +24,12 @@ type Group struct { // 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)) } @@ -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() } @@ -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) } @@ -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 diff --git a/zmq4_timeout_test.go b/zmq4_timeout_test.go index 76d41b5..725f1a8 100644 --- a/zmq4_timeout_test.go +++ b/zmq4_timeout_test.go @@ -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 (