diff --git a/pool.go b/pool.go index af79b67..ab97fee 100644 --- a/pool.go +++ b/pool.go @@ -355,6 +355,7 @@ func (p *Pool) retrieveWorker() (w worker) { p.lock.Unlock() return } + p.addWaiting(1) p.cond.Wait() // block and wait for an available worker p.addWaiting(-1) @@ -389,21 +390,20 @@ func (p *Pool) revertWorker(worker *goWorker) bool { p.cond.Broadcast() return false } - worker.lastUsed = p.nowTime() - p.lock.Lock() + worker.lastUsed = p.nowTime() + + p.lock.Lock() // To avoid memory leaks, add a double check in the lock scope. // Issue: https://github.com/panjf2000/ants/issues/113 if p.IsClosed() { p.lock.Unlock() return false } - if err := p.workers.insert(worker); err != nil { p.lock.Unlock() return false } - // Notify the invoker stuck in 'retrieveWorker()' of there is an available worker in the worker queue. p.cond.Signal() p.lock.Unlock() diff --git a/pool_func.go b/pool_func.go index c55b88e..390cd06 100644 --- a/pool_func.go +++ b/pool_func.go @@ -361,6 +361,7 @@ func (p *PoolWithFunc) retrieveWorker() (w worker) { p.lock.Unlock() return } + p.addWaiting(1) p.cond.Wait() // block and wait for an available worker p.addWaiting(-1) @@ -395,21 +396,20 @@ func (p *PoolWithFunc) revertWorker(worker *goWorkerWithFunc) bool { p.cond.Broadcast() return false } - worker.lastUsed = p.nowTime() - p.lock.Lock() + worker.lastUsed = p.nowTime() + + p.lock.Lock() // To avoid memory leaks, add a double check in the lock scope. // Issue: https://github.com/panjf2000/ants/issues/113 if p.IsClosed() { p.lock.Unlock() return false } - if err := p.workers.insert(worker); err != nil { p.lock.Unlock() return false } - // Notify the invoker stuck in 'retrieveWorker()' of there is an available worker in the worker queue. p.cond.Signal() p.lock.Unlock()