-
Notifications
You must be signed in to change notification settings - Fork 0
/
workersDaisyChain.go
55 lines (43 loc) · 1.54 KB
/
workersDaisyChain.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
46
47
48
49
50
51
52
53
54
55
// daisy chain of workers
package workersDaisyChain
// generic data to be transmitted between workers
type dataEnvelope interface{}
// the type of the worker function
type workerFun func(wid uint64, inch chan dataEnvelope, outch chan dataEnvelope)
type workerConfig struct {
// the number of workers
numOfWorkers uint64
// the function every worker runs as a goroutine
wf workerFun
}
// Start the daisy chain of workers passing the number of workers, a workerFun,
// and the data to be sent to the first worker in the chain.
// Return the final data processed by and received from the last worker in the chain
func StartDaisyChainOfWorkers(numOfWorkers uint64, worker workerFun, d dataEnvelope) dataEnvelope {
// set up the worker config structure
var wConfig workerConfig
wConfig.numOfWorkers = numOfWorkers
wConfig.wf = worker
// channel used to send data to the first worker
ch0 := make(chan dataEnvelope)
defer close(ch0)
// channel used by the last worker to send data to the caller that
// triggered the daisy chain
var chf chan dataEnvelope
var preCh chan dataEnvelope
var postCh chan dataEnvelope
preCh = ch0
// start the daisy chain of workers
for wid := uint64(1); wid <= wConfig.numOfWorkers; wid++ {
postCh = make(chan dataEnvelope)
go wConfig.wf(wid, preCh, postCh)
preCh = postCh
}
// chf is the channel used by the last worker to send data to the caller
// that triggered the daisy chain
chf = postCh
// send the data to the first worker
ch0 <- d
// receive and return the final data from the last worker
return <-chf
}