Wait purge goroutine exit in stop function to prevent send on closed channel
This commit is contained in:
@@ -91,6 +91,7 @@ type WorkerPool struct {
|
|||||||
tasks chan func()
|
tasks chan func()
|
||||||
workersWaitGroup sync.WaitGroup
|
workersWaitGroup sync.WaitGroup
|
||||||
tasksWaitGroup sync.WaitGroup
|
tasksWaitGroup sync.WaitGroup
|
||||||
|
purgerWaitGroup sync.WaitGroup
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
stopped bool
|
stopped bool
|
||||||
}
|
}
|
||||||
@@ -138,6 +139,7 @@ func New(maxWorkers, maxCapacity int, options ...Option) *WorkerPool {
|
|||||||
pool.tasks = make(chan func(), pool.maxCapacity)
|
pool.tasks = make(chan func(), pool.maxCapacity)
|
||||||
|
|
||||||
// Start purger goroutine
|
// Start purger goroutine
|
||||||
|
pool.purgerWaitGroup.Add(1)
|
||||||
go pool.purge()
|
go pool.purge()
|
||||||
|
|
||||||
// Start minWorkers workers
|
// Start minWorkers workers
|
||||||
@@ -377,11 +379,14 @@ func (p *WorkerPool) stop(waitForCompletion bool) {
|
|||||||
// Wait for all workers to exit
|
// Wait for all workers to exit
|
||||||
if waitForCompletion {
|
if waitForCompletion {
|
||||||
p.workersWaitGroup.Wait()
|
p.workersWaitGroup.Wait()
|
||||||
|
p.purgerWaitGroup.Wait()
|
||||||
|
close(p.tasks)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// purge represents the work done by the purger goroutine
|
// purge represents the work done by the purger goroutine
|
||||||
func (p *WorkerPool) purge() {
|
func (p *WorkerPool) purge() {
|
||||||
|
defer p.purgerWaitGroup.Done()
|
||||||
|
|
||||||
idleTicker := time.NewTicker(p.idleTimeout)
|
idleTicker := time.NewTicker(p.idleTimeout)
|
||||||
defer idleTicker.Stop()
|
defer idleTicker.Stop()
|
||||||
|
|||||||
Reference in New Issue
Block a user