-
Notifications
You must be signed in to change notification settings - Fork 1
/
split_brick.go
69 lines (60 loc) · 1.23 KB
/
split_brick.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package flow
type SplitBrick struct {
name string
lc ILifeCycle
deepCopy func(*Message) (*Message, error)
chanSize int
errQueue chan error
outQueues []chan *Message
}
func (b *SplitBrick) Name() string {
return b.name
}
func (b *SplitBrick) AddLifeCycle(lc ILifeCycle) {
b.lc = lc
}
func (b *SplitBrick) Linked(inQueue <-chan *Message) {
go b.loop(inQueue)
}
func (b *SplitBrick) Errors() <-chan error {
return b.errQueue
}
func (b *SplitBrick) Output() <-chan *Message {
output := make(chan *Message, b.chanSize)
b.outQueues = append(b.outQueues, output)
return output
}
func (b *SplitBrick) loop(inQueue <-chan *Message) {
defer func() {
b.lc.Done()
}()
for msg := range inQueue {
for _, output := range b.outQueues {
if b.deepCopy != nil {
temp, err := b.deepCopy(msg)
if err != nil {
b.errQueue <- err
break
}
output <- temp
} else {
output <- msg
}
}
}
for _, output := range b.outQueues {
close(output)
}
}
func NewSplitBrick(
name string,
deepCopy func(*Message) (*Message, error),
chanSize int) *SplitBrick {
return &SplitBrick{
name: name,
deepCopy: deepCopy,
chanSize: chanSize,
outQueues: make([]chan *Message, 0),
errQueue: make(chan error, 8),
}
}