pool.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package worker // import "miniflux.app/v2/internal/worker"
  4. import (
  5. "sync"
  6. "miniflux.app/v2/internal/model"
  7. "miniflux.app/v2/internal/storage"
  8. )
  9. // Pool manages a set of background workers that process feed refresh jobs.
  10. type Pool struct {
  11. queue chan model.Job
  12. wg sync.WaitGroup
  13. }
  14. // Push sends a list of jobs to the queue.
  15. func (p *Pool) Push(jobs model.JobList) {
  16. for _, job := range jobs {
  17. p.queue <- job
  18. }
  19. }
  20. // Shutdown closes the job queue and waits for all workers to finish their current jobs.
  21. func (p *Pool) Shutdown() {
  22. close(p.queue)
  23. p.wg.Wait()
  24. }
  25. // NewPool creates a pool of background workers.
  26. func NewPool(store *storage.Storage, nbWorkers int) *Pool {
  27. workerPool := &Pool{
  28. queue: make(chan model.Job),
  29. }
  30. for i := range nbWorkers {
  31. workerPool.wg.Add(1)
  32. worker := &worker{id: i, store: store}
  33. go worker.Run(workerPool.queue, &workerPool.wg)
  34. }
  35. return workerPool
  36. }