mirror of
https://github.com/pyihe/go-pkg.git
synced 2025-09-26 20:11:21 +08:00
feature(syncs):
This commit is contained in:
31
syncs/waitgroup.go
Normal file
31
syncs/waitgroup.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package syncs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type WgWrapper struct {
|
||||
sync.WaitGroup
|
||||
}
|
||||
|
||||
func (w *WgWrapper) Wrap(cb func()) {
|
||||
w.Add(1)
|
||||
go func() {
|
||||
cb()
|
||||
w.Done()
|
||||
}()
|
||||
}
|
||||
|
||||
func (w *WgWrapper) WrapWithBlock(ctx context.Context, fn func() chan error) {
|
||||
w.Add(1)
|
||||
go func(cancelCtx context.Context, errCh chan error) {
|
||||
select {
|
||||
case <-cancelCtx.Done():
|
||||
break
|
||||
case <-errCh:
|
||||
break
|
||||
}
|
||||
w.Done()
|
||||
}(ctx, fn())
|
||||
}
|
Reference in New Issue
Block a user