-
Notifications
You must be signed in to change notification settings - Fork 111
/
join.go
67 lines (62 loc) · 1.14 KB
/
join.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
package library
import (
"github.com/nytlabs/streamtools/st/blocks" // blocks
)
type Join struct {
blocks.Block
inA chan interface{}
inB chan interface{}
clear chan interface{}
out chan interface{}
quit chan interface{}
}
func NewJoin() blocks.BlockInterface {
return &Join{}
}
func (b *Join) Setup() {
b.Kind = "Join"
b.Desc = "joins two streams together, emitting the joined message once it's been seen on both inputs"
b.inA = b.InRoute("inA")
b.inB = b.InRoute("inB")
b.clear = b.InRoute("clear")
b.quit = b.Quit()
b.out = b.Broadcast()
}
func (b *Join) Run() {
A := make(chan interface{}, 1000)
B := make(chan interface{}, 1000)
for {
select {
case <-b.quit:
return
case msg := <-b.inA:
select {
case A <- msg:
default:
b.Error("the A queue is overflowing")
}
case msg := <-b.inB:
select {
case B <- msg:
default:
b.Error("the B queue is overflowing")
}
case <-b.clear:
Clear:
for {
select {
case <-A:
case <-B:
default:
break Clear
}
}
}
for len(A) > 0 && len(B) > 0 {
b.out <- map[string]interface{}{
"A": <-A,
"B": <-B,
}
}
}
}