feature(syncs):

This commit is contained in:
pyihe
2022-07-12 15:13:40 +08:00
parent 4ca4ee9d4a
commit e4d7066311

31
syncs/waitgroup.go Normal file
View 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())
}