Skip to content

Commit

Permalink
feat: refactor wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
daheige committed Apr 11, 2023
1 parent bcab281 commit ea51831
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 230 deletions.
50 changes: 0 additions & 50 deletions chanwrap/chan_wrap_test.go

This file was deleted.

5 changes: 2 additions & 3 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"log"

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

func main() {
chWrap := factory.New(factory.ChWrapper, wrapper.WithBufCap(2))
chWrap := wrapper.New(wrapper.ChWrapper, wrapper.WithBufCap(2))
chWrap.Wrap(func() {
log.Println("chan wrapper: 1111")
})
Expand All @@ -19,7 +18,7 @@ func main() {
chWrap.Wait()

// factory.WgWrapper No need to pass second parameter.
wg := factory.New(factory.WgWrapper)
wg := wrapper.New(wrapper.WgWrapper)
wg.Wrap(func() {
log.Println("wg wrapper:1111")
})
Expand Down
43 changes: 0 additions & 43 deletions factory/wrap_factory.go

This file was deleted.

15 changes: 0 additions & 15 deletions factory/wrap_factory_test.go

This file was deleted.

10 changes: 0 additions & 10 deletions grecover/recovery.go

This file was deleted.

24 changes: 24 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package wrapper

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

// Options optional function
type Option func(o *Options)

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

// WithBufCap set buf cap
func WithBufCap(c int) Option {
return func(o *Options) {
o.BufCap = c
}
}
11 changes: 3 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,12 @@ package main

import (
"log"

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

func main() {
// If the wrapper using the chan method needs to specify the number of
// goroutines to be executed,the wrapper.WithBufCap method needs to be called.
// Otherwise, after the Wait method is executed, some goroutines
// will exit without execution.
chWrap := factory.New(factory.ChWrapper, wrapper.WithBufCap(2))
chWrap := wrapper.New(wrapper.ChWrapper, wrapper.WithBufCap(2))
chWrap.Wrap(func() {
log.Println("chan wrapper: 1111")
})
Expand All @@ -33,7 +28,7 @@ func main() {
chWrap.Wait()

// factory.WgWrapper No need to pass second parameter.
wg := factory.New(factory.WgWrapper)
wg := wrapper.New(wrapper.WgWrapper)
wg.Wrap(func() {
log.Println("wg wrapper:1111")
})
Expand Down
51 changes: 0 additions & 51 deletions waitgroup/waitgroup_impl.go

This file was deleted.

52 changes: 37 additions & 15 deletions wrapper.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package wrapper

import "log"

// Wrapper wrap goroutine to run
type Wrapper interface {
// Wrap exec func in goroutine without recover catch
Expand All @@ -8,29 +10,49 @@ type Wrapper interface {
// WrapWithRecover safely execute func in goroutine
WrapWithRecover(fn func())

// Wait wait all goroutine finish
// Wait this func wait all goroutine finish
Wait()
}

// Option wrapper option
type Options struct {
BufCap int
RecoveryFunc func()
var wrapperMap = map[WrapType]constructor{
WgWrapper: NewWgWrapper,
ChWrapper: NewChanWrapper,
}

// New create wrapper interface
func New(wrapType WrapType, opts ...Option) Wrapper {
if w, ok := wrapperMap[wrapType]; ok {
return w(opts...)
}

panic("wrapper type not exists")
}

// Options option func
type Option func(o *Options)
type constructor func(opts ...Option) Wrapper

// WrapType wrap type
type WrapType int

// WithBufCap set buf cap
func WithBufCap(c int) Option {
return func(o *Options) {
o.BufCap = c
const (
// WgWrapper waitGroup wrapper
WgWrapper WrapType = iota
// ChWrapper chan wrapper
ChWrapper
)

// Register register wrapper
func Register(wrapType WrapType, c constructor) {
_, ok := wrapperMap[wrapType]
if ok {
panic("registered wrapper already exists")
}

wrapperMap[wrapType] = c
}

// WithRecover set recover func
func WithRecover(recoveryFunc func()) Option {
return func(o *Options) {
o.RecoveryFunc = recoveryFunc
// defaultRecovery default recover func.
func defaultRecovery() {
if e := recover(); e != nil {
log.Printf("wrapper exec recover:%v\n", e)
}
}
Loading

0 comments on commit ea51831

Please sign in to comment.