worker_pool.go 850 B

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package scheduler
  5. import (
  6. "github.com/miniflux/miniflux/model"
  7. "github.com/miniflux/miniflux/reader/feed"
  8. )
  9. // WorkerPool handle a pool of workers.
  10. type WorkerPool struct {
  11. queue chan model.Job
  12. }
  13. // Push send a list of jobs to the queue.
  14. func (w *WorkerPool) Push(jobs model.JobList) {
  15. for _, job := range jobs {
  16. w.queue <- job
  17. }
  18. }
  19. // NewWorkerPool creates a pool of background workers.
  20. func NewWorkerPool(feedHandler *feed.Handler, nbWorkers int) *WorkerPool {
  21. workerPool := &WorkerPool{
  22. queue: make(chan model.Job),
  23. }
  24. for i := 0; i < nbWorkers; i++ {
  25. worker := &Worker{id: i, feedHandler: feedHandler}
  26. go worker.Run(workerPool.queue)
  27. }
  28. return workerPool
  29. }