-
Notifications
You must be signed in to change notification settings - Fork 9
/
dispatcher.go
45 lines (39 loc) · 893 Bytes
/
dispatcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package csprcollector
import (
"log"
)
func NewDispatcher(nworkers int, output Output, workQueue <-chan CSPRequest) *Dispatcher {
return &Dispatcher{
WorkerQueue: make(chan chan CSPRequest, nworkers),
WorkQueue: workQueue,
NumberOfWorkers: nworkers,
Output: output,
}
}
type Dispatcher struct {
WorkerQueue chan chan CSPRequest
WorkQueue <-chan CSPRequest
NumberOfWorkers int
Output Output
}
func (d *Dispatcher) Run() {
for i := 0; i < d.NumberOfWorkers; i++ {
log.Printf("Starting worker #%d.", i+1)
worker := NewWorker(i+1, d.WorkerQueue, d.Output)
worker.Start()
}
go d.start()
}
func (d *Dispatcher) start() {
for {
select {
case work := <-d.WorkQueue:
log.Print("Received work request.")
go func() {
worker := <-d.WorkerQueue
log.Print("Dispatching work request.")
worker <- work
}()
}
}
}