-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
244 lines (206 loc) · 6.53 KB
/
main.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"io/ioutil"
"strconv"
"time"
"github.com/bitly/go-nsq"
"github.com/garyburd/redigo/redis"
"github.com/segmentio/go-log"
"github.com/segmentio/nsq_to_redis/broadcast"
"github.com/segmentio/nsq_to_redis/list"
"github.com/segmentio/nsq_to_redis/pubsub"
"github.com/segmentio/nsq_to_redis/ratelimit"
"github.com/segmentio/statsdclient"
"github.com/tj/docopt"
"github.com/tj/go-gracefully"
)
var version = "2.1.0"
const usage = `
Usage:
nsq_to_redis
--topic name [--channel name]
[--max-attempts n] [--max-in-flight n]
[--statsd addr]
[--statsd-prefix prefix]
[--lookupd-http-address addr...]
[--nsqd-tcp-address addr...]
[--redis-address addr]
[--flush-interval t]
[--max-idle n]
[--idle-timeout t]
[--list name] [--list-size n]
[--publish name]
[--level name]
[--ratelimit-key key]
[--ratelimit-max-rate n]
[--ratelimit-max-keys n]
nsq_to_redis -h | --help
nsq_to_redis --version
Options:
--lookupd-http-address addr nsqlookupd addresses [default: :4161]
--nsqd-tcp-address addr nsqd tcp addresses
--redis-address addr redis address [default: :6379]
--max-attempts n nsq max message attempts [default: 5]
--max-in-flight n nsq messages in-flight [default: 250]
--flush-interval t time to buffer redis commands before flushing [default: 0s]
--max-idle n redis max idle connections [default: 15]
--idle-timeout t idle connection timeout [default: 1m]
--list-size n redis list size [default: 100]
--list name redis list template
--publish name redis channel template
--topic name nsq consumer topic name
--channel name nsq consumer channel name [default: nsq_to_redis]
--level name log level [default: info]
--statsd addr tcp address [default: ]
--statsd-prefix prefix prefix for statsd [default: nsq_to_redis.]
--ratelimit-key key a key to use for ratelimits, for example "api_key" [default: ]
--ratelimit-max-rate n max writes for each key per second, N <= 0 will not limit [default: 0]
--ratelimit-max-keys n max keys to keep in memory (lru cache) [default: 500]
-h, --help output help information
-v, --version output version
`
func main() {
args, err := docopt.Parse(usage, nil, true, version, false)
if err != nil {
log.Fatalf("error parsing arguments: %s", err)
}
lookupds := args["--lookupd-http-address"].([]string)
channel := args["--channel"].(string)
topic := args["--topic"].(string)
var metrics *statsd.Client
if addr := args["--statsd"].(string); addr != "" {
metrics, err = statsd.Dial(addr)
} else {
metrics = statsd.NewClient(ioutil.Discard)
}
metrics.Prefix(args["--statsd-prefix"].(string))
idleTimeout, err := time.ParseDuration(args["--idle-timeout"].(string))
if err != nil {
log.Fatalf("error parsing idle timeout: %s", err)
}
flushInterval, err := time.ParseDuration(args["--flush-interval"].(string))
if err != nil {
log.Fatalf("error parsing flush-interval: %s", err)
}
if flushInterval < 0 {
log.Fatalf("flush-interval must not be a negative value")
}
maxIdle, err := strconv.Atoi(args["--max-idle"].(string))
if err != nil {
log.Fatalf("error parsing max-idle: %s", err)
}
if maxIdle > 100 {
log.Fatalf("max-idle must be below 100")
}
pool := &redis.Pool{
IdleTimeout: idleTimeout,
MaxIdle: maxIdle,
MaxActive: 100,
Dial: dial(args["--redis-address"].(string)),
TestOnBorrow: ping,
}
broadcast := broadcast.New(&broadcast.Options{
Redis: pool,
Metrics: metrics,
Log: log.Log,
Ratelimiter: ratelimiter(args),
RatelimitKey: args["--ratelimit-key"].(string),
FlushInterval: flushInterval,
})
config := config(args)
consumer, err := nsq.NewConsumer(topic, channel, config)
if err != nil {
log.Fatalf("error starting consumer: %s", err)
}
log.SetLevelString(args["--level"].(string))
// Pub/Sub support.
if format, ok := args["--publish"].(string); ok {
log.Info("publishing to %q", format)
pubsub, err := pubsub.New(&pubsub.Options{
Format: format,
Log: log.Log,
Metrics: metrics,
})
if err != nil {
log.Fatalf("error starting pubsub: %s", err)
}
broadcast.Add(pubsub)
}
// Capped list support.
if format, ok := args["--list"].(string); ok {
size, err := strconv.Atoi(args["--list-size"].(string))
if err != nil {
log.Fatalf("error parsing --list-size: %s", err)
}
log.Info("listing to %q (size=%d)", format, size)
list, err := list.New(&list.Options{
Format: format,
Log: log.Log,
Metrics: metrics,
Size: int64(size),
})
if err != nil {
log.Fatalf("error starting list: %s", err)
}
broadcast.Add(list)
}
consumer.AddConcurrentHandlers(broadcast, maxIdle)
nsqds := args["--nsqd-tcp-address"].([]string)
if len(nsqds) > 0 {
err = consumer.ConnectToNSQDs(nsqds)
} else {
err = consumer.ConnectToNSQLookupds(lookupds)
}
if err != nil {
log.Fatalf("error connecting to nsqds: %s", err)
}
gracefully.Shutdown()
log.Info("stopping")
consumer.Stop()
<-consumer.StopChan
broadcast.Stop()
<-broadcast.Done
log.Info("bye :)")
}
// Parse NSQ configuration from args.
func config(args map[string]interface{}) *nsq.Config {
config := nsq.NewConfig()
n, err := strconv.Atoi(args["--max-attempts"].(string))
if err != nil {
log.Fatalf("error parsing --max-attempts: %s", err)
}
config.MaxAttempts = uint16(n)
n, err = strconv.Atoi(args["--max-in-flight"].(string))
if err != nil {
log.Fatalf("error parsing --max-in-flight: %s", err)
}
config.MaxInFlight = n
return config
}
// Parse Ratelimiter configuration and return
// a new ratelimiter or nil.
func ratelimiter(args map[string]interface{}) *ratelimit.Ratelimiter {
rate, err := strconv.Atoi(args["--ratelimit-max-rate"].(string))
if err != nil {
log.Fatalf("error parsing --ratelimit-max-rate: %s", err)
}
if rate <= 0 {
return nil
}
keys, err := strconv.Atoi(args["--ratelimit-max-keys"].(string))
if err != nil {
log.Fatalf("error parsing --ratelimit-max-keys: %s", err)
}
return ratelimit.New(rate, keys)
}
// Dialer.
func dial(addr string) func() (redis.Conn, error) {
return func() (redis.Conn, error) {
return redis.Dial("tcp", addr)
}
}
// Idle connection test.
func ping(client redis.Conn, t time.Time) error {
_, err := client.Do("PING")
return err
}