dispatcher.go 815 B

1234567891011121314151617181920212223242526272829303132333435
  1. package worker
  2. import (
  3. "log/slog"
  4. )
  5. // WorkerQueue is the global queue of Workers
  6. var WorkerQueue chan chan Work
  7. // WorkQueue is the global queue of work to dispatch
  8. var WorkQueue = make(chan Work, 100)
  9. // StartDispatcher is charged to start n workers.
  10. func StartDispatcher(nworkers int) {
  11. // First, initialize the channel we are going to but the workers' work channels into.
  12. WorkerQueue = make(chan chan Work, nworkers)
  13. // Now, create all of our workers.
  14. for i := 0; i < nworkers; i++ {
  15. slog.Debug("starting worker...", "worker", i+1)
  16. worker := NewWorker(i+1, WorkerQueue)
  17. worker.Start()
  18. }
  19. go func() {
  20. for {
  21. work := <-WorkQueue
  22. go func() {
  23. worker := <-WorkerQueue
  24. slog.Debug("dispatching hook request", "hook", work.Name(), "id", work.ID())
  25. worker <- work
  26. }()
  27. }
  28. }()
  29. }