forked from mirror/ants
🐬 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.
|
||||
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
|
||||
}
|
||||
|
|
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.
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue