-
Notifications
You must be signed in to change notification settings - Fork 111
/
toNSQMulti.go
141 lines (123 loc) · 2.79 KB
/
toNSQMulti.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package library
import (
"encoding/json"
"github.com/bitly/go-nsq"
"github.com/nytlabs/streamtools/st/blocks" // blocks
"github.com/nytlabs/streamtools/st/util"
"time"
)
// specify those channels we're going to use to communicate with streamtools
type ToNSQMulti struct {
blocks.Block
queryrule chan chan interface{}
inrule chan interface{}
in chan interface{}
out chan interface{}
quit chan interface{}
nsqdTCPAddrs string
topic string
}
// a bit of boilerplate for streamtools
func NewToNSQMulti() blocks.BlockInterface {
return &ToNSQMulti{}
}
func (b *ToNSQMulti) Setup() {
b.Kind = "ToNSQMulti"
b.Desc = "sends messages to an NSQ topic in batches"
b.in = b.InRoute("in")
b.inrule = b.InRoute("rule")
b.queryrule = b.QueryRoute("rule")
b.quit = b.Quit()
}
// connects to an NSQ topic and emits each message into streamtools.
func (b *ToNSQMulti) Run() {
var writer *nsq.Writer
var batch [][]byte
interval := time.Duration(1 * time.Second)
maxBatch := 100
dump := time.NewTicker(interval)
for {
select {
case <-dump.C:
if writer == nil || len(batch) == 0 {
break
}
_, _, err := writer.MultiPublish(b.topic, batch)
if err != nil {
b.Error(err.Error())
}
batch = nil
case ruleI := <-b.inrule:
//rule := ruleI.(map[string]interface{})
topic, err := util.ParseString(ruleI, "Topic")
if err != nil {
b.Error(err)
break
}
nsqdTCPAddrs, err := util.ParseString(ruleI, "NsqdTCPAddrs")
if err != nil {
b.Error(err)
break
}
intervalS, err := util.ParseString(ruleI, "Interval")
if err != nil {
b.Error("bad input")
break
}
dur, err := time.ParseDuration(intervalS)
if err != nil {
b.Error(err)
break
}
if dur <= 0 {
b.Error("interval must be positive")
break
}
batchSize, err := util.ParseFloat(ruleI, "MaxBatch")
if err != nil {
b.Error("error parsing batch size")
break
}
if writer != nil {
writer.Stop()
}
maxBatch = int(batchSize)
interval = dur
dump.Stop()
dump = time.NewTicker(interval)
writer = nsq.NewWriter(nsqdTCPAddrs)
b.topic = topic
b.nsqdTCPAddrs = nsqdTCPAddrs
case msg := <-b.in:
if writer == nil {
break
}
msgByte, err := json.Marshal(msg)
if err != nil {
b.Error(err)
}
batch = append(batch, msgByte)
if len(batch) > maxBatch {
_, _, err := writer.MultiPublish(b.topic, batch)
if err != nil {
b.Error(err.Error())
break
}
batch = nil
}
case <-b.quit:
if writer != nil {
writer.Stop()
}
dump.Stop()
return
case c := <-b.queryrule:
c <- map[string]interface{}{
"Topic": b.topic,
"NsqdTCPAddrs": b.nsqdTCPAddrs,
"MaxBatch": maxBatch,
"Interval": interval.String(),
}
}
}
}