webhookd/pkg/worker/dispatcher.go

36 lines
791 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 (
"github.com/ncarlier/webhookd/pkg/logger"
)
2014-10-31 23:18:38 +00:00
var WorkerQueue chan chan WorkRequest
var WorkQueue = make(chan WorkRequest, 100)
// 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.
WorkerQueue = make(chan chan WorkRequest, nworkers)
// Now, create all of our workers.
for i := 0; i < nworkers; i++ {
2018-01-09 09:16:33 +00:00
logger.Debug.Println("Starting worker", i+1)
2014-10-31 23:18:38 +00:00
worker := NewWorker(i+1, WorkerQueue)
worker.Start()
}
go func() {
for {
select {
case work := <-WorkQueue:
go func() {
worker := <-WorkerQueue
2018-07-24 15:36:19 +00:00
logger.Debug.Printf("Dispatching work request: %s#%d", work.Name, work.ID)
2014-10-31 23:18:38 +00:00
worker <- work
}()
}
}
}()
}