Skip to content

Commit

Permalink
feat: wg wrpper options
Browse files Browse the repository at this point in the history
  • Loading branch information
daheige committed Jan 9, 2022
1 parent 9b73bb5 commit 962c419
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 38 deletions.
28 changes: 16 additions & 12 deletions chanwrap/chan_wrap_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,28 @@ var est = struct{}{}

// WrapImpl wrapper impl
type WrapImpl struct {
bufNum int
bufCap int
bufCh chan struct{}
recoveryFunc func()
}

// New create wrapImpl entity
func New(c int) wrapper.Wrapper {
w := &WrapImpl{
bufNum: c,
bufCh: make(chan struct{}, c),
recoveryFunc: grecover.DefaultRecovery,
func New(opts ...wrapper.Options) wrapper.Wrapper {
w := &WrapImpl{}

var option = &wrapper.Option{}
for _, o := range opts {
o(option)
}

w.recoveryFunc = option.RecoveryFunc
if w.recoveryFunc == nil {
w.recoveryFunc = grecover.DefaultRecovery
}

w.bufCap = option.BufCap
w.bufCh = make(chan struct{}, w.bufCap)

return w
}

Expand All @@ -44,16 +53,11 @@ func (c *WrapImpl) WrapWithRecover(fn func()) {

// Wait wait all goroutine finish
func (c *WrapImpl) Wait() {
for i := 0; i < c.bufNum; i++ {
for i := 0; i < c.bufCap; i++ {
<-c.bufCh
}
}

// WithRecover set recover func
func (c *WrapImpl) WithRecover(recoveryFunc func()) {
c.recoveryFunc = recoveryFunc
}

func (c *WrapImpl) done() {
c.bufCh <- est
}
5 changes: 4 additions & 1 deletion chanwrap/chan_wrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package chanwrap
import (
"log"
"testing"

"github.com/go-god/wrapper"
)

func TestWrapper(t *testing.T) {
var wg = New(2)
var wg = New(wrapper.WithBufCap(2))
wg.Wrap(func() {
log.Println("1111")
})

wg.WrapWithRecover(func() {
log.Println(2222)
panic("mock panic test")
Expand Down
9 changes: 5 additions & 4 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ package main
import (
"log"

"github.com/go-god/wrapper"
"github.com/go-god/wrapper/factory"
)

func main() {
wrapper := factory.New(factory.ChWrapper, 2)
wrapper.Wrap(func() {
chWrap := factory.New(factory.ChWrapper, wrapper.WithBufCap(2))
chWrap.Wrap(func() {
log.Println("chan wrapper: 1111")
})
wrapper.Wrap(func() {
chWrap.Wrap(func() {
log.Println("chan wrapper: 2222")
})

wrapper.Wait()
chWrap.Wait()

// factory.WgWrapper No need to pass second parameter.
wg := factory.New(factory.WgWrapper)
Expand Down
10 changes: 3 additions & 7 deletions factory/wrap_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/go-god/wrapper/waitgroup"
)

type constructor func() wrapper.Wrapper
type constructor func(opts ...wrapper.Options) wrapper.Wrapper

const (
// WgWrapper waitGroup wrapper
Expand All @@ -17,15 +17,11 @@ const (

var wrapperMap = map[string]constructor{
WgWrapper: waitgroup.New,
ChWrapper: nil,
ChWrapper: chanwrap.New,
}

// New create wrapper interface
func New(name string, c ...int) wrapper.Wrapper {
if name == ChWrapper && len(c) > 0 && c[0] > 0 {
return chanwrap.New(c[0])
}

func New(name string, opts ...wrapper.Options) wrapper.Wrapper {
if w, ok := wrapperMap[name]; ok {
return w()
}
Expand Down
2 changes: 1 addition & 1 deletion factory/wrap_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func TestNew(t *testing.T) {
wrap := New("wg", 1)
wrap := New("wg")
wrap.Wrap(func() {
log.Println(1111)
})
Expand Down
9 changes: 5 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@ package main
import (
"log"

"github.com/go-god/wrapper"
"github.com/go-god/wrapper/factory"
)

func main() {
wrapper := factory.New(factory.ChWrapper, 2)
wrapper.Wrap(func() {
chWrap := factory.New(factory.ChWrapper, wrapper.WithBufCap(2))
chWrap.Wrap(func() {
log.Println("chan wrapper: 1111")
})
wrapper.Wrap(func() {
chWrap.Wrap(func() {
log.Println("chan wrapper: 2222")
})

wrapper.Wait()
chWrap.Wait()

// factory.WgWrapper No need to pass second parameter.
wg := factory.New(factory.WgWrapper)
Expand Down
16 changes: 11 additions & 5 deletions waitgroup/waitgroup_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,21 @@ type WrapImpl struct {
}

// New create wrapper entity
func New() wrapper.Wrapper {
func New(opts ...wrapper.Options) wrapper.Wrapper {
w := &WrapImpl{
recoveryFunc: grecover.DefaultRecovery,
}

var option = &wrapper.Option{}
for _, o := range opts {
o(option)
}

w.recoveryFunc = option.RecoveryFunc
if w.recoveryFunc == nil {
w.recoveryFunc = grecover.DefaultRecovery
}

return w
}

Expand All @@ -40,7 +50,3 @@ func (w *WrapImpl) WrapWithRecover(fn func()) {
fn()
}()
}

func (w *WrapImpl) WithRecover(recoveryFunc func()) {
w.recoveryFunc = recoveryFunc
}
1 change: 0 additions & 1 deletion waitgroup/waitgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ func mockRecovery() {

func TestWrapper(t *testing.T) {
var wg = New()
wg.WithRecover(mockRecovery)
wg.Wrap(func() {
log.Println("1111")
})
Expand Down
26 changes: 23 additions & 3 deletions wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,29 @@ type Wrapper interface {
// WrapWithRecover safely execute func in goroutine
WrapWithRecover(fn func())

// WithRecover set recover func
WithRecover(recoveryFunc func())

// Wait wait all goroutine finish
Wait()
}

// Option wrapper option
type Option struct {
BufCap int
RecoveryFunc func()
}

// Options option func
type Options func(o *Option)

// WithBufCap set buf cap
func WithBufCap(c int) Options {
return func(o *Option) {
o.BufCap = c
}
}

// WithRecover set recover func
func WithRecover(recoveryFunc func()) Options {
return func(o *Option) {
o.RecoveryFunc = recoveryFunc
}
}

0 comments on commit 962c419

Please sign in to comment.