Skip to content

Commit

Permalink
chore: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Mar 23, 2023
1 parent 55e222d commit 4b495fd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -389,21 +390,20 @@ func (p *Pool) revertWorker(worker *goWorker) bool {
p.cond.Broadcast()
return false
}

worker.lastUsed = p.nowTime()
p.lock.Lock()

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()
Expand Down
6 changes: 3 additions & 3 deletions pool_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -395,21 +396,20 @@ func (p *PoolWithFunc) revertWorker(worker *goWorkerWithFunc) bool {
p.cond.Broadcast()
return false
}

worker.lastUsed = p.nowTime()
p.lock.Lock()

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()
Expand Down

0 comments on commit 4b495fd

Please sign in to comment.