🐬 Code logic improvement in pool.retrieveWorker

This commit is contained in:
Andy Pan 2019-01-27 04:25:36 +08:00
parent 689d74c63b
commit 5c666692e9
2 changed files with 26 additions and 34 deletions

30
pool.go
View File

@ -189,21 +189,27 @@ func (p *Pool) decRunning() {
// retrieveWorker returns a available worker to run the tasks.
func (p *Pool) retrieveWorker() *Worker {
var w *Worker
var waiting bool
p.lock.Lock()
idleWorkers := p.workers
n := len(idleWorkers) - 1
if n < 0 {
waiting = p.Running() >= p.Cap()
} else {
if n >= 0 {
w = idleWorkers[n]
idleWorkers[n] = nil
p.workers = idleWorkers[:n]
p.lock.Unlock()
}
if waiting {
} else if p.Running() < p.Cap() {
p.lock.Unlock()
if cacheWorker := p.workerCache.Get(); cacheWorker != nil {
w = cacheWorker.(*Worker)
} else {
w = &Worker{
pool: p,
task: make(chan f, workerChanCap),
}
w.run()
}
} else {
for {
p.cond.Wait()
l := len(p.workers) - 1
@ -216,16 +222,6 @@ func (p *Pool) retrieveWorker() *Worker {
break
}
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
}

View File

@ -193,21 +193,27 @@ func (p *PoolWithFunc) decRunning() {
// retrieveWorker returns a available worker to run the tasks.
func (p *PoolWithFunc) retrieveWorker() *WorkerWithFunc {
var w *WorkerWithFunc
var waiting bool
p.lock.Lock()
idleWorkers := p.workers
n := len(idleWorkers) - 1
if n < 0 {
waiting = p.Running() >= p.Cap()
} else {
if n >= 0 {
w = idleWorkers[n]
idleWorkers[n] = nil
p.workers = idleWorkers[:n]
p.lock.Unlock()
}
if waiting {
} else if p.Running() < p.Cap() {
p.lock.Unlock()
if cacheWorker := p.workerCache.Get(); cacheWorker != nil {
w = cacheWorker.(*WorkerWithFunc)
} else {
w = &WorkerWithFunc{
pool: p,
args: make(chan interface{}, workerChanCap),
}
w.run()
}
} else {
for {
p.cond.Wait()
l := len(p.workers) - 1
@ -220,16 +226,6 @@ func (p *PoolWithFunc) retrieveWorker() *WorkerWithFunc {
break
}
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
}