mirror of
https://github.com/panjf2000/ants.git
synced 2025-12-18 19:31:02 +00:00
🐬 Code logic improvement in pool.retrieveWorker
This commit is contained in:
parent
689d74c63b
commit
5c666692e9
30
pool.go
30
pool.go
@ -189,21 +189,27 @@ func (p *Pool) decRunning() {
|
|||||||
// retrieveWorker returns a available worker to run the tasks.
|
// retrieveWorker returns a available worker to run the tasks.
|
||||||
func (p *Pool) retrieveWorker() *Worker {
|
func (p *Pool) retrieveWorker() *Worker {
|
||||||
var w *Worker
|
var w *Worker
|
||||||
var waiting bool
|
|
||||||
|
|
||||||
p.lock.Lock()
|
p.lock.Lock()
|
||||||
idleWorkers := p.workers
|
idleWorkers := p.workers
|
||||||
n := len(idleWorkers) - 1
|
n := len(idleWorkers) - 1
|
||||||
if n < 0 {
|
if n >= 0 {
|
||||||
waiting = p.Running() >= p.Cap()
|
|
||||||
} else {
|
|
||||||
w = idleWorkers[n]
|
w = idleWorkers[n]
|
||||||
idleWorkers[n] = nil
|
idleWorkers[n] = nil
|
||||||
p.workers = idleWorkers[:n]
|
p.workers = idleWorkers[:n]
|
||||||
p.lock.Unlock()
|
p.lock.Unlock()
|
||||||
}
|
} else if p.Running() < p.Cap() {
|
||||||
|
p.lock.Unlock()
|
||||||
if waiting {
|
if cacheWorker := p.workerCache.Get(); cacheWorker != nil {
|
||||||
|
w = cacheWorker.(*Worker)
|
||||||
|
} else {
|
||||||
|
w = &Worker{
|
||||||
|
pool: p,
|
||||||
|
task: make(chan f, workerChanCap),
|
||||||
|
}
|
||||||
|
w.run()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
for {
|
for {
|
||||||
p.cond.Wait()
|
p.cond.Wait()
|
||||||
l := len(p.workers) - 1
|
l := len(p.workers) - 1
|
||||||
@ -216,16 +222,6 @@ func (p *Pool) retrieveWorker() *Worker {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
p.lock.Unlock()
|
p.lock.Unlock()
|
||||||
} else if w == nil {
|
|
||||||
p.lock.Unlock()
|
|
||||||
if cacheWorker := p.workerCache.Get(); cacheWorker != nil {
|
|
||||||
return cacheWorker.(*Worker)
|
|
||||||
}
|
|
||||||
w = &Worker{
|
|
||||||
pool: p,
|
|
||||||
task: make(chan f, workerChanCap),
|
|
||||||
}
|
|
||||||
w.run()
|
|
||||||
}
|
}
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
|||||||
30
pool_func.go
30
pool_func.go
@ -193,21 +193,27 @@ func (p *PoolWithFunc) decRunning() {
|
|||||||
// retrieveWorker returns a available worker to run the tasks.
|
// retrieveWorker returns a available worker to run the tasks.
|
||||||
func (p *PoolWithFunc) retrieveWorker() *WorkerWithFunc {
|
func (p *PoolWithFunc) retrieveWorker() *WorkerWithFunc {
|
||||||
var w *WorkerWithFunc
|
var w *WorkerWithFunc
|
||||||
var waiting bool
|
|
||||||
|
|
||||||
p.lock.Lock()
|
p.lock.Lock()
|
||||||
idleWorkers := p.workers
|
idleWorkers := p.workers
|
||||||
n := len(idleWorkers) - 1
|
n := len(idleWorkers) - 1
|
||||||
if n < 0 {
|
if n >= 0 {
|
||||||
waiting = p.Running() >= p.Cap()
|
|
||||||
} else {
|
|
||||||
w = idleWorkers[n]
|
w = idleWorkers[n]
|
||||||
idleWorkers[n] = nil
|
idleWorkers[n] = nil
|
||||||
p.workers = idleWorkers[:n]
|
p.workers = idleWorkers[:n]
|
||||||
p.lock.Unlock()
|
p.lock.Unlock()
|
||||||
}
|
} else if p.Running() < p.Cap() {
|
||||||
|
p.lock.Unlock()
|
||||||
if waiting {
|
if cacheWorker := p.workerCache.Get(); cacheWorker != nil {
|
||||||
|
w = cacheWorker.(*WorkerWithFunc)
|
||||||
|
} else {
|
||||||
|
w = &WorkerWithFunc{
|
||||||
|
pool: p,
|
||||||
|
args: make(chan interface{}, workerChanCap),
|
||||||
|
}
|
||||||
|
w.run()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
for {
|
for {
|
||||||
p.cond.Wait()
|
p.cond.Wait()
|
||||||
l := len(p.workers) - 1
|
l := len(p.workers) - 1
|
||||||
@ -220,16 +226,6 @@ func (p *PoolWithFunc) retrieveWorker() *WorkerWithFunc {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
p.lock.Unlock()
|
p.lock.Unlock()
|
||||||
} else if w == nil {
|
|
||||||
p.lock.Unlock()
|
|
||||||
if cacheWorker := p.workerCache.Get(); cacheWorker != nil {
|
|
||||||
return cacheWorker.(*WorkerWithFunc)
|
|
||||||
}
|
|
||||||
w = &WorkerWithFunc{
|
|
||||||
pool: p,
|
|
||||||
args: make(chan interface{}, workerChanCap),
|
|
||||||
}
|
|
||||||
w.run()
|
|
||||||
}
|
}
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user