-
Notifications
You must be signed in to change notification settings - Fork 1
/
output_brick.go
61 lines (52 loc) · 1.03 KB
/
output_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
package flow
import (
"github.com/RyouZhang/async-go"
)
type OutputBrick struct {
name string
lc ILifeCycle
kernal func(*Message) error
gracefulStop func()
errQueue chan error
}
func (b *OutputBrick) Name() string {
return b.name
}
func (b *OutputBrick) AddLifeCycle(lc ILifeCycle) {
b.lc = lc
}
func (b *OutputBrick) Linked(inQueue <-chan *Message) {
go b.loop(inQueue)
}
func (b *OutputBrick) Errors() <-chan error {
return b.errQueue
}
func (b *OutputBrick) loop(inQueue <-chan *Message) {
defer func() {
close(b.errQueue)
b.lc.Done()
}()
for msg := range inQueue {
_, err := async.Safety(func() (interface{}, error) {
err := b.kernal(msg)
return nil, err
})
if err != nil {
b.errQueue <- err
}
}
if b.gracefulStop != nil {
b.gracefulStop()
}
}
func NewOutputBrick(
name string,
kernal func(*Message) error,
gracefulStop func()) *OutputBrick {
return &OutputBrick{
name: name,
kernal: kernal,
gracefulStop: gracefulStop,
errQueue: make(chan error, 8),
}
}