-
Notifications
You must be signed in to change notification settings - Fork 0
/
nsq_to_http.go
216 lines (187 loc) · 5.14 KB
/
nsq_to_http.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// This is a client that writes out to a http endpoint
package main
import (
"../../nsq"
"../../util"
"bytes"
"errors"
"flag"
"fmt"
"log"
"math/rand"
"net/url"
"os"
"os/signal"
"strings"
"syscall"
)
const (
ModeAll = iota
ModeRoundRobin
)
var (
showVersion = flag.Bool("version", false, "print version string")
topic = flag.String("topic", "", "nsq topic")
channel = flag.String("channel", "nsq_to_http", "nsq channel")
maxInFlight = flag.Int("max-in-flight", 200, "max number of messages to allow in flight")
verbose = flag.Bool("verbose", false, "enable verbose logging")
numPublishers = flag.Int("n", 100, "number of concurrent publishers")
roundRobin = flag.Bool("round-robin", false, "enable round robin mode")
throttleFraction = flag.Float64("throttle-fraction", 1.0, "publish only a fraction of messages")
httpTimeoutMs = flag.Int("http-timeout-ms", 20000, "timeout for HTTP connect/read/write (each)")
getAddrs = util.StringArray{}
postAddrs = util.StringArray{}
nsqdTCPAddrs = util.StringArray{}
lookupdHTTPAddrs = util.StringArray{}
)
func init() {
flag.Var(&postAddrs, "post", "HTTP address to make a POST request to. data will be in the body (may be given multiple times)")
flag.Var(&getAddrs, "get", "HTTP address to make a GET request to. '%s' will be printf replaced with data (may be given multiple times)")
flag.Var(&nsqdTCPAddrs, "nsqd-tcp-address", "nsqd TCP address (may be given multiple times)")
flag.Var(&lookupdHTTPAddrs, "lookupd-http-address", "lookupd HTTP address (may be given multiple times)")
}
type Publisher interface {
Publish(string, []byte) error
}
type PublishHandler struct {
Publisher
addresses util.StringArray
counter uint64
mode int
}
func (ph *PublishHandler) HandleMessage(m *nsq.Message) error {
// skip messages if rand float is greater than throttle
// short-circuit to avoid needless calls to rand
if *throttleFraction < 1.0 && rand.Float64() > *throttleFraction {
return nil
}
switch ph.mode {
case ModeAll:
for _, addr := range ph.addresses {
err := ph.Publish(addr, m.Body)
if err != nil {
return err
}
}
case ModeRoundRobin:
idx := ph.counter % uint64(len(ph.addresses))
err := ph.Publish(ph.addresses[idx], m.Body)
if err != nil {
return err
}
ph.counter++
}
return nil
}
type PostPublisher struct{}
func (p *PostPublisher) Publish(addr string, msg []byte) error {
buf := bytes.NewBuffer(msg)
resp, err := HttpPost(addr, buf)
if err != nil {
return err
}
resp.Body.Close()
if resp.StatusCode != 200 {
return errors.New(fmt.Sprintf("got status code %d", resp.StatusCode))
}
return nil
}
type GetPublisher struct{}
func (p *GetPublisher) Publish(addr string, msg []byte) error {
endpoint := fmt.Sprintf(addr, url.QueryEscape(string(msg)))
resp, err := HttpGet(endpoint)
if err != nil {
return err
}
resp.Body.Close()
if resp.StatusCode != 200 {
return errors.New(fmt.Sprintf("got status code %d", resp.StatusCode))
}
return nil
}
func main() {
var publisher Publisher
var addresses util.StringArray
var mode int
flag.Parse()
if *showVersion {
fmt.Printf("nsq_to_http v%s\n", util.BINARY_VERSION)
return
}
if *topic == "" || *channel == "" {
log.Fatalf("--topic and --channel are required")
}
if *maxInFlight < 0 {
log.Fatalf("--max-in-flight must be > 0")
}
if len(nsqdTCPAddrs) == 0 && len(lookupdHTTPAddrs) == 0 {
log.Fatalf("--nsqd-tcp-address or --lookupd-http-address required")
}
if len(nsqdTCPAddrs) > 0 && len(lookupdHTTPAddrs) > 0 {
log.Fatalf("use --nsqd-tcp-address or --lookupd-http-address not both")
}
if len(getAddrs) == 0 && len(postAddrs) == 0 {
log.Fatalf("--get or --post required")
}
if len(getAddrs) > 0 && len(postAddrs) > 0 {
log.Fatalf("use --get or --post not both")
}
if len(getAddrs) > 0 {
for _, get := range getAddrs {
if strings.Count(get, "%s") != 1 {
log.Fatal("invalid GET address - must be a printf string")
}
}
}
if *roundRobin {
mode = ModeRoundRobin
}
if *throttleFraction > 1.0 || *throttleFraction < 0.0 {
log.Fatalf("Throttle fraction must be between 0.0 and 1.0")
}
hupChan := make(chan os.Signal, 1)
termChan := make(chan os.Signal, 1)
signal.Notify(hupChan, syscall.SIGHUP)
signal.Notify(termChan, syscall.SIGINT, syscall.SIGTERM)
if len(postAddrs) > 0 {
publisher = &PostPublisher{}
addresses = postAddrs
} else {
publisher = &GetPublisher{}
addresses = getAddrs
}
r, err := nsq.NewReader(*topic, *channel)
if err != nil {
log.Fatalf(err.Error())
}
r.SetMaxInFlight(*maxInFlight)
r.VerboseLogging = *verbose
for i := 0; i < *numPublishers; i++ {
handler := &PublishHandler{
Publisher: publisher,
addresses: addresses,
mode: mode,
}
r.AddHandler(handler)
}
for _, addrString := range nsqdTCPAddrs {
err := r.ConnectToNSQ(addrString)
if err != nil {
log.Fatalf(err.Error())
}
}
for _, addrString := range lookupdHTTPAddrs {
log.Printf("lookupd addr %s", addrString)
err := r.ConnectToLookupd(addrString)
if err != nil {
log.Fatalf(err.Error())
}
}
select {
case <-r.ExitChan:
case <-hupChan:
r.Stop()
case <-termChan:
r.Stop()
}
}