chore: code cleanup

This commit is contained in:
Andy Pan 2023-03-23 18:18:52 +08:00
parent 55e222d20f
commit 4b495fd500
2 changed files with 8 additions and 8 deletions

View File

@ -355,6 +355,7 @@ func (p *Pool) retrieveWorker() (w worker) {
p.lock.Unlock() p.lock.Unlock()
return return
} }
p.addWaiting(1) p.addWaiting(1)
p.cond.Wait() // block and wait for an available worker p.cond.Wait() // block and wait for an available worker
p.addWaiting(-1) p.addWaiting(-1)
@ -389,21 +390,20 @@ func (p *Pool) revertWorker(worker *goWorker) bool {
p.cond.Broadcast() p.cond.Broadcast()
return false 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. // To avoid memory leaks, add a double check in the lock scope.
// Issue: https://github.com/panjf2000/ants/issues/113 // Issue: https://github.com/panjf2000/ants/issues/113
if p.IsClosed() { if p.IsClosed() {
p.lock.Unlock() p.lock.Unlock()
return false return false
} }
if err := p.workers.insert(worker); err != nil { if err := p.workers.insert(worker); err != nil {
p.lock.Unlock() p.lock.Unlock()
return false return false
} }
// Notify the invoker stuck in 'retrieveWorker()' of there is an available worker in the worker queue. // Notify the invoker stuck in 'retrieveWorker()' of there is an available worker in the worker queue.
p.cond.Signal() p.cond.Signal()
p.lock.Unlock() p.lock.Unlock()

View File

@ -361,6 +361,7 @@ func (p *PoolWithFunc) retrieveWorker() (w worker) {
p.lock.Unlock() p.lock.Unlock()
return return
} }
p.addWaiting(1) p.addWaiting(1)
p.cond.Wait() // block and wait for an available worker p.cond.Wait() // block and wait for an available worker
p.addWaiting(-1) p.addWaiting(-1)
@ -395,21 +396,20 @@ func (p *PoolWithFunc) revertWorker(worker *goWorkerWithFunc) bool {
p.cond.Broadcast() p.cond.Broadcast()
return false 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. // To avoid memory leaks, add a double check in the lock scope.
// Issue: https://github.com/panjf2000/ants/issues/113 // Issue: https://github.com/panjf2000/ants/issues/113
if p.IsClosed() { if p.IsClosed() {
p.lock.Unlock() p.lock.Unlock()
return false return false
} }
if err := p.workers.insert(worker); err != nil { if err := p.workers.insert(worker); err != nil {
p.lock.Unlock() p.lock.Unlock()
return false return false
} }
// Notify the invoker stuck in 'retrieveWorker()' of there is an available worker in the worker queue. // Notify the invoker stuck in 'retrieveWorker()' of there is an available worker in the worker queue.
p.cond.Signal() p.cond.Signal()
p.lock.Unlock() p.lock.Unlock()