Waitgroup
waitgroup that supports context and timeout.
Install / Use
/learn @rfyiamcool/WaitgroupREADME
waitgroup
extend std waitgroup
feature:
- easy api
- add timeout
- add context
- concurrency go func
when waitgroup force to exit with timeout, may cause goroutines leak problem.
usage:
see more example in test
func Test_Simple(t *testing.T) {
counter := 0
lock := sync.Mutex{}
fn := func() error {
lock.Lock()
defer lock.Unlock()
counter++
return nil
}
wg, ctx := New()
wg.Async(fn)
wg.AsyncMany(fn, 5)
<-ctx.Done()
wg.Wait()
assert.Equal(t, wg.IsError(), false)
assert.Equal(t, counter, 6)
}
func Test_Ctx(t *testing.T) {
counter := 0
lock := sync.Mutex{}
fn := func() error {
lock.Lock()
defer lock.Unlock()
counter++
return nil
}
errval := errors.New("errfn")
errfn := func() error {
lock.Lock()
defer lock.Unlock()
counter++
return errval
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
wg, cctx := NewWithContext(ctx)
wg.Async(errfn)
wg.AsyncMany(fn, 5)
select {
case <-cctx.Done():
case <-ctx.Done():
}
assert.Equal(t, counter, 6)
assert.Equal(t, wg.IsError(), true)
assert.Equal(t, errval, wg.Errs[0])
}
func Test_WaitTimeout(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
wg, cctx := NewWithContext(ctx)
// _ = cctx
wg.Async(
func() error {
time.Sleep(5 * time.Second)
return io.ErrClosedPipe
},
)
wg.WaitTimeout(1 * time.Second)
<-cctx.Done()
}
Related Skills
openhue
340.5kControl Philips Hue lights and scenes via the OpenHue CLI.
sag
340.5kElevenLabs text-to-speech with mac-style say UX.
weather
340.5kGet current weather and forecasts via wttr.in or Open-Meteo
tweakcc
1.5kCustomize Claude Code's system prompts, create custom toolsets, input pattern highlighters, themes/thinking verbs/spinners, customize input box & user message styling, support AGENTS.md, unlock private/unreleased features, and much more. Supports both native/npm installs on all platforms.
