|
@@ -12,33 +12,43 @@ import (
|
|
|
|
|
|
|
|
// Pool manages a set of background workers that process feed refresh jobs.
|
|
// Pool manages a set of background workers that process feed refresh jobs.
|
|
|
type Pool struct {
|
|
type Pool struct {
|
|
|
- queue chan model.Job
|
|
|
|
|
- wg sync.WaitGroup
|
|
|
|
|
|
|
+ queue chan model.Job
|
|
|
|
|
+ shutdown chan struct{}
|
|
|
|
|
+ shutdownOnce sync.Once
|
|
|
|
|
+ wg sync.WaitGroup
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Push sends a list of jobs to the queue.
|
|
// Push sends a list of jobs to the queue.
|
|
|
|
|
+// Jobs pushed after Shutdown are discarded.
|
|
|
func (p *Pool) Push(jobs model.JobList) {
|
|
func (p *Pool) Push(jobs model.JobList) {
|
|
|
for _, job := range jobs {
|
|
for _, job := range jobs {
|
|
|
- p.queue <- job
|
|
|
|
|
|
|
+ select {
|
|
|
|
|
+ case p.queue <- job:
|
|
|
|
|
+ case <-p.shutdown:
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Shutdown closes the job queue and waits for all workers to finish their current jobs.
|
|
|
|
|
|
|
+// Shutdown stops accepting new jobs and waits for all workers to finish their current jobs.
|
|
|
func (p *Pool) Shutdown() {
|
|
func (p *Pool) Shutdown() {
|
|
|
- close(p.queue)
|
|
|
|
|
|
|
+ p.shutdownOnce.Do(func() {
|
|
|
|
|
+ close(p.shutdown)
|
|
|
|
|
+ })
|
|
|
p.wg.Wait()
|
|
p.wg.Wait()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// NewPool creates a pool of background workers.
|
|
// NewPool creates a pool of background workers.
|
|
|
func NewPool(store *storage.Storage, nbWorkers int) *Pool {
|
|
func NewPool(store *storage.Storage, nbWorkers int) *Pool {
|
|
|
workerPool := &Pool{
|
|
workerPool := &Pool{
|
|
|
- queue: make(chan model.Job),
|
|
|
|
|
|
|
+ queue: make(chan model.Job),
|
|
|
|
|
+ shutdown: make(chan struct{}),
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
for i := range nbWorkers {
|
|
for i := range nbWorkers {
|
|
|
workerPool.wg.Add(1)
|
|
workerPool.wg.Add(1)
|
|
|
worker := &worker{id: i, store: store}
|
|
worker := &worker{id: i, store: store}
|
|
|
- go worker.Run(workerPool.queue, &workerPool.wg)
|
|
|
|
|
|
|
+ go worker.Run(workerPool.queue, workerPool.shutdown, &workerPool.wg)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return workerPool
|
|
return workerPool
|