-
Notifications
You must be signed in to change notification settings - Fork 3
/
block.go
121 lines (101 loc) · 2.76 KB
/
block.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package steps
import (
"fmt"
"github.com/antongulenko/golib"
"github.com/bitflow-stream/go-bitflow/bitflow"
"github.com/bitflow-stream/go-bitflow/script/reg"
)
type BlockingProcessor struct {
bitflow.NoopProcessor
block *golib.BoolCondition
key string
}
func (p *BlockingProcessor) Sample(sample *bitflow.Sample, header *bitflow.Header) error {
p.block.Wait()
return p.NoopProcessor.Sample(sample, header)
}
func (p *BlockingProcessor) String() string {
return fmt.Sprintf("block (key: %v)", p.key)
}
func (p *BlockingProcessor) Close() {
p.Release()
p.NoopProcessor.Close()
}
func (p *BlockingProcessor) Release() {
p.block.Broadcast()
}
type BlockerList struct {
Blockers []*BlockingProcessor
}
func (l *BlockerList) ReleaseAll() {
for _, blocker := range l.Blockers {
blocker.Release()
}
}
func (l *BlockerList) Add(blocker *BlockingProcessor) {
l.Blockers = append(l.Blockers, blocker)
}
type ReleasingProcessor struct {
bitflow.NoopProcessor
blockers *BlockerList
key string
}
func (p *ReleasingProcessor) Close() {
p.blockers.ReleaseAll()
p.NoopProcessor.Close()
}
func (p *ReleasingProcessor) String() string {
return fmt.Sprintf("release all blocks with key %v", p.key)
}
type BlockManager struct {
blockers map[string]*BlockerList
}
func NewBlockManager() *BlockManager {
return &BlockManager{
blockers: make(map[string]*BlockerList),
}
}
func (m *BlockManager) GetList(key string) *BlockerList {
list, ok := m.blockers[key]
if !ok {
list = new(BlockerList)
m.blockers[key] = list
}
return list
}
func (m *BlockManager) NewBlocker(key string) *BlockingProcessor {
blocker := &BlockingProcessor{
block: golib.NewBoolCondition(),
key: key,
}
m.GetList(key).Add(blocker)
return blocker
}
func (m *BlockManager) NewReleaser(key string) *ReleasingProcessor {
return &ReleasingProcessor{
blockers: m.GetList(key),
key: key,
}
}
func (m *BlockManager) RegisterBlockingProcessor(b reg.ProcessorRegistry) {
b.RegisterStep("block",
func(p *bitflow.SamplePipeline, params map[string]interface{}) error {
if err := AddDecoupleStep(p, params); err != nil {
return err
}
p.Add(m.NewBlocker(params["key"].(string)))
return nil
},
"Block further processing of the samples until a release() with the same key is closed. Creates a new goroutine, input buffer size must be specified.").
Required("key", reg.String()).
Required("buf", reg.Int())
}
func (m *BlockManager) RegisterReleasingProcessor(b reg.ProcessorRegistry) {
b.RegisterStep("releaseOnClose",
func(p *bitflow.SamplePipeline, params map[string]interface{}) error {
p.Add(m.NewReleaser(params["key"].(string)))
return nil
},
"When this step is closed, release all instances of block() with the same key value").
Optional("key", reg.String(), "")
}