pool.go 787 B

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright 2018 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 worker // import "miniflux.app/worker"
  5. import (
  6. "miniflux.app/model"
  7. "miniflux.app/storage"
  8. )
  9. // Pool handles a pool of workers.
  10. type Pool struct {
  11. queue chan model.Job
  12. }
  13. // Push send a list of jobs to the queue.
  14. func (p *Pool) Push(jobs model.JobList) {
  15. for _, job := range jobs {
  16. p.queue <- job
  17. }
  18. }
  19. // NewPool creates a pool of background workers.
  20. func NewPool(store *storage.Storage, nbWorkers int) *Pool {
  21. workerPool := &Pool{
  22. queue: make(chan model.Job),
  23. }
  24. for i := 0; i < nbWorkers; i++ {
  25. worker := &Worker{id: i, store: store}
  26. go worker.Run(workerPool.queue)
  27. }
  28. return workerPool
  29. }