-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: syncs - move goutil.ErrGroup to sub pkg syncs.ErrGroup
- Loading branch information
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package syncs | ||
|
||
import ( | ||
"context" | ||
|
||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
// ErrGroup is a collection of goroutines working on subtasks that | ||
// are part of the same overall task. | ||
// | ||
// Refers: | ||
// | ||
// https://github.com/neilotoole/errgroup | ||
// https://github.com/fatih/semgroup | ||
type ErrGroup struct { | ||
*errgroup.Group | ||
} | ||
|
||
// NewCtxErrGroup instance | ||
func NewCtxErrGroup(ctx context.Context, limit ...int) (*ErrGroup, context.Context) { | ||
egg, ctx1 := errgroup.WithContext(ctx) | ||
if len(limit) > 0 && limit[0] > 0 { | ||
egg.SetLimit(limit[0]) | ||
} | ||
|
||
eg := &ErrGroup{Group: egg} | ||
return eg, ctx1 | ||
} | ||
|
||
// NewErrGroup instance | ||
func NewErrGroup(limit ...int) *ErrGroup { | ||
eg := &ErrGroup{Group: new(errgroup.Group)} | ||
|
||
if len(limit) > 0 && limit[0] > 0 { | ||
eg.SetLimit(limit[0]) | ||
} | ||
return eg | ||
} | ||
|
||
// Add one or more handler at once | ||
func (g *ErrGroup) Add(handlers ...func() error) { | ||
for _, handler := range handlers { | ||
g.Go(handler) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package syncs_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/gookit/goutil" | ||
"github.com/gookit/goutil/netutil/httpreq" | ||
"github.com/gookit/goutil/testutil" | ||
"github.com/gookit/goutil/testutil/assert" | ||
) | ||
|
||
func TestNewErrGroup(t *testing.T) { | ||
httpreq.SetTimeout(3000) | ||
|
||
eg := goutil.NewErrGroup() | ||
eg.Add(func() error { | ||
resp, err := httpreq.Get(testSrvAddr + "/get") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println(testutil.ParseBodyToReply(resp.Body)) | ||
return nil | ||
}, func() error { | ||
resp := httpreq.MustResp(httpreq.Post(testSrvAddr+"/post", "hi")) | ||
fmt.Println(testutil.ParseBodyToReply(resp.Body)) | ||
return nil | ||
}) | ||
|
||
err := eg.Wait() | ||
assert.NoErr(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package syncs_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/gookit/goutil/testutil" | ||
) | ||
|
||
var testSrvAddr string | ||
|
||
func TestMain(m *testing.M) { | ||
s := testutil.NewEchoServer() | ||
defer s.Close() | ||
testSrvAddr = "http://" + s.Listener.Addr().String() | ||
fmt.Println("Test server listen on:", testSrvAddr) | ||
|
||
m.Run() | ||
} |