Fix: Gopool should reuse worker with certainty
@@ -80,12 +80,16 @@ func (g *Pool) execute(f func(ctx context.Context)) {
// Do pick one idle goroutine to do the f once
func (g *Pool) Do(f func(context.Context)) *Pool {
defer log.Recover()
+ select {
+ case g.pending <- f: // try to reuse worker first
+ default:
select {
case g.pending <- f: // block if workers are busy
case g.workers <- struct{}{}:
g.wg.Add(1)
go g.loop(f)
}
+ }
return g
}
