webhookd/src/worker/dispatcher.go

36 lines
703 B
Go
Raw Normal View History

2014-10-31 23:18:38 +00:00
package worker
import (
"fmt"
)
var WorkerQueue chan chan WorkRequest
var WorkQueue = make(chan WorkRequest, 100)
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++ {
fmt.Println("Starting worker", i+1)
worker := NewWorker(i+1, WorkerQueue)
worker.Start()
}
go func() {
for {
select {
case work := <-WorkQueue:
fmt.Println("Received work requeust")
go func() {
worker := <-WorkerQueue
fmt.Println("Dispatching work request")
worker <- work
}()
}
}
}()
}