webhookd/pkg/worker/dispatcher.go

36 lines
815 B
Go
Raw Normal View History

2014-10-31 23:18:38 +00:00
package worker
2018-01-09 09:16:33 +00:00
import (
2023-10-03 18:02:57 +00:00
"log/slog"
2018-01-09 09:16:33 +00:00
)
2014-10-31 23:18:38 +00:00
// WorkerQueue is the global queue of Workers
2022-05-26 07:05:49 +00:00
var WorkerQueue chan chan Work
// WorkQueue is the global queue of work to dispatch
2022-05-26 07:05:49 +00:00
var WorkQueue = make(chan Work, 100)
2014-10-31 23:18:38 +00:00
// StartDispatcher is charged to start n workers.
2014-10-31 23:18:38 +00:00
func StartDispatcher(nworkers int) {
// First, initialize the channel we are going to but the workers' work channels into.
2022-05-26 07:05:49 +00:00
WorkerQueue = make(chan chan Work, nworkers)
2014-10-31 23:18:38 +00:00
// Now, create all of our workers.
for i := 0; i < nworkers; i++ {
2023-10-03 18:02:57 +00:00
slog.Debug("starting worker...", "worker", i+1)
2014-10-31 23:18:38 +00:00
worker := NewWorker(i+1, WorkerQueue)
worker.Start()
}
go func() {
for {
2023-10-04 19:20:54 +00:00
work := <-WorkQueue
go func() {
worker := <-WorkerQueue
slog.Debug("dispatching hook request", "hook", work.Name(), "id", work.ID())
worker <- work
}()
2014-10-31 23:18:38 +00:00
}
}()
}