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:
|
2014-11-06 16:03:23 +00:00
|
|
|
fmt.Println("Received work request")
|
2014-10-31 23:18:38 +00:00
|
|
|
go func() {
|
|
|
|
worker := <-WorkerQueue
|
|
|
|
|
|
|
|
fmt.Println("Dispatching work request")
|
|
|
|
worker <- work
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|