Skip to content

Commit 98caea8

Browse files
committed
simplify
Signed-off-by: Andrew Thornton <[email protected]>
1 parent fe81bb3 commit 98caea8

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

modules/queue/workerpool.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -381,20 +381,37 @@ func (p *WorkerPool) pause() {
381381

382382
// Resume resumes the WorkerPool
383383
func (p *WorkerPool) Resume() {
384-
p.lock.Lock()
384+
p.lock.Lock() // can't defer unlock because of the zeroBoost at the end
385385
select {
386386
case <-p.resumed:
387+
// already resumed - there's nothing to do
387388
p.lock.Unlock()
389+
return
388390
default:
389-
p.paused = make(chan struct{})
390-
close(p.resumed)
391-
if !p.hasNoWorkerScaling() && p.numberOfWorkers == 0 && atomic.LoadInt64(&p.numInQueue) > 0 {
392-
p.zeroBoost()
393-
// p.zeroBoost will unlock the lock
394-
} else {
395-
p.lock.Unlock()
396-
}
397391
}
392+
393+
p.paused = make(chan struct{})
394+
close(p.resumed)
395+
396+
// OK now we need to check if we need to add some workers...
397+
if p.numberOfWorkers > 0 || p.hasNoWorkerScaling() || atomic.LoadInt64(&p.numInQueue) == 0 {
398+
// We either have workers, can't scale or there's no work to be done -> so just resume
399+
p.lock.Unlock()
400+
return
401+
}
402+
403+
// OK we got some work but no workers we need to think about boosting
404+
select {
405+
case <-p.baseCtx.Done():
406+
// don't bother boosting if the baseCtx is done
407+
p.lock.Unlock()
408+
return
409+
default:
410+
}
411+
412+
// OK we'd better add some boost workers!
413+
p.zeroBoost()
414+
// p.zeroBoost will unlock the lock
398415
}
399416

400417
// CleanUp will drain the remaining contents of the channel

0 commit comments

Comments
 (0)