diff --git a/pool.go b/pool.go index e27d586..37151cc 100644 --- a/pool.go +++ b/pool.go @@ -243,16 +243,20 @@ func (p *Pool) retrieveWorker() (w *goWorker) { p.blockingNum++ p.cond.Wait() p.blockingNum-- - if p.Running() == 0 { + var nw int + if nw = p.Running(); nw == 0 { p.lock.Unlock() if !p.IsClosed() { spawnWorker() } return } - - w = p.workers.detach() - if w == nil { + if w = p.workers.detach(); w == nil { + if nw < capacity { + p.lock.Unlock() + spawnWorker() + return + } goto Reentry } diff --git a/pool_func.go b/pool_func.go index 3d77487..b14be42 100644 --- a/pool_func.go +++ b/pool_func.go @@ -270,6 +270,11 @@ func (p *PoolWithFunc) retrieveWorker() (w *goWorkerWithFunc) { } l := len(p.workers) - 1 if l < 0 { + if p.Running() < p.Cap() { + p.lock.Unlock() + spawnWorker() + return + } goto Reentry } w = p.workers[l] diff --git a/worker.go b/worker.go index 1a52d65..3fb8333 100644 --- a/worker.go +++ b/worker.go @@ -59,6 +59,8 @@ func (w *goWorker) run() { w.pool.options.Logger.Printf("worker exits from panic: %s\n", string(buf[:n])) } } + // Call Signal() here in case there are goroutines waiting for available workers. + w.pool.cond.Signal() }() for f := range w.task { diff --git a/worker_func.go b/worker_func.go index 14a63c2..a123dc3 100644 --- a/worker_func.go +++ b/worker_func.go @@ -59,6 +59,8 @@ func (w *goWorkerWithFunc) run() { w.pool.options.Logger.Printf("worker with func exits from panic: %s\n", string(buf[:n])) } } + // Call Signal() here in case there are goroutines waiting for available workers. + w.pool.cond.Signal() }() for args := range w.args {