mirror of
https://github.com/ncarlier/webhookd.git
synced 2024-09-20 17:05:27 +00:00
36 lines
703 B
Go
36 lines
703 B
Go
|
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
|
||
|
}()
|
||
|
}
|
||
|
}
|
||
|
}()
|
||
|
}
|