forked from cloudflare/go-stream
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchannelFanout.go
46 lines (41 loc) · 994 Bytes
/
channelFanout.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
package stream
// ChannelFanoutOperator does a fanout of info across a set of downstream channels.
// acts synchronously in that it will write to all downstreams before reading the next upstream channel.
type ChannelFanoutOperator struct {
*HardStopChannelCloser
*BaseIn
outputs []chan Object
err error
}
func NewChannelFanoutOp() *ChannelFanoutOperator {
op := &ChannelFanoutOperator{NewHardStopChannelCloser(), NewBaseIn(CHAN_SLACK), make([]chan Object, 0), nil}
return op
}
func (op *ChannelFanoutOperator) Add(newChannel chan Object) {
op.outputs = append(op.outputs, newChannel)
}
func (op *ChannelFanoutOperator) Run() error {
defer func() {
for _, out := range op.outputs {
close(out)
}
}()
for {
select {
case obj, ok := <-op.In():
if ok {
for _, out := range op.outputs {
select {
case out <- obj:
case <-op.StopNotifier:
return nil
}
}
} else {
return nil
}
case <-op.StopNotifier:
return nil
}
}
}