forked from ninjasphere/sphere-go-state-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
234 lines (177 loc) · 5.41 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
package main
import (
"fmt"
"net/url"
"os"
"os/signal"
"regexp"
"time"
"github.com/alecthomas/kingpin"
"github.com/garyburd/redigo/redis"
"github.com/juju/loggo"
"github.com/ninjablocks/sphere-go-state-service/health"
"github.com/ninjablocks/sphere-go-state-service/stats"
"github.com/rcrowley/go-metrics"
"github.com/rcrowley/go-metrics/librato"
"github.com/streadway/amqp"
"github.com/wolfeidau/punter"
)
var (
debug = kingpin.Flag("debug", "Enable debug mode.").OverrideDefaultFromEnvar("DEBUG").Bool()
workers = kingpin.Flag("workers", "Configure the number of workers.").Default("4").OverrideDefaultFromEnvar("WORKERS").Int()
redisURL = kingpin.Flag("redis", "REDIS url.").Default("redis://localhost:6379").OverrideDefaultFromEnvar("REDIS_URL").String()
rabbitmqURL = kingpin.Flag("rabbitmq", "rabbitmq url.").Default("amqp://guest:guest@localhost:5672").OverrideDefaultFromEnvar("RABBIT_URL").String()
libratoKey = kingpin.Flag("libratoKey", "Librato API key.").OverrideDefaultFromEnvar("LIBRATO_KEY").String()
statusAddr = kingpin.Flag("statusAddr", "Address to assign to the status listener.").OverrideDefaultFromEnvar("PORT").Default(":6100").String()
log = loggo.GetLogger("state-service")
routingKey = "*.$cloud.device.*.channel.*.event.state"
userRegex = regexp.MustCompile(`^(?P<user_id>[a-zA-Z0-9-_]+).\$cloud.device.(?P<device_id>\w+).channel.(?P<channel_id>[a-zA-Z0-9-_]+).event.state$`)
hostname = "unknown"
)
func main() {
kingpin.Version(Version)
kingpin.Parse()
// apply flags
if *debug {
loggo.GetLogger("").SetLogLevel(loggo.DEBUG)
} else {
loggo.GetLogger("").SetLogLevel(loggo.INFO)
}
rurl, err := url.Parse(*redisURL)
if err != nil {
panic(err)
}
hostname, err = os.Hostname()
if err != nil {
panic(err)
}
log.Infof("hostname %+v", hostname)
c := metrics.NewCounter()
metrics.Register("timeseries.messages_processed", c)
t := metrics.NewTimer()
metrics.Register("timeseries.messages_processed_time", t)
// go metrics.Log(metrics.DefaultRegistry, 30e9, glog.New(os.Stderr, "metrics: ", glog.Lmicroseconds))
startLibrato()
stats.StartRuntimeMetricsJob("prod")
ss := &stateStore{
pool: newPool(rurl.Host),
c: c,
t: t,
}
consumers := []*punter.Consumer{}
conf := &punter.Config{
AmqpURI: *rabbitmqURL,
Exchange: "amq.topic",
ExchangeType: "topic",
QueueName: "stateservice",
Key: routingKey,
MessageTTL: int32(600000), // How long to retain messages in the queue (10 minutes)
Durable: false, // Queue durable?
}
for i := 0; i < *workers; i++ {
consumer, err := punter.NewConsumer(conf, fmt.Sprintf("stateservice-consumer-%s", hostname), ss.stateHandler)
if err != nil {
panic(err)
}
consumers = append(consumers, consumer)
}
health.StartHttpListener(*statusAddr, BuildInfo)
sc := make(chan os.Signal, 1)
signal.Notify(sc, os.Interrupt, os.Kill)
// Block until a signal is received.
s := <-sc
log.Warningf("Got signal: %v", s)
log.Warningf("shutting down consumer")
for n, consumer := range consumers {
log.Infof("shutting down consumer %d", n)
if err := consumer.Shutdown(); err != nil {
log.Infof("error during shutdown: %s", err)
}
}
}
func newPool(server string) *redis.Pool {
return &redis.Pool{
MaxIdle: 3,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", server)
if err != nil {
return nil, err
}
return c, err
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
}
func startLibrato() {
if *libratoKey == "" {
log.Warningf("skipping librato job as no key is set.")
return
}
go librato.Librato(metrics.DefaultRegistry,
30e9, // interval
"[email protected]", // account owner email address
*libratoKey, // Librato API token
hostname, // source
[]float64{0.95}, // precentiles to send
time.Millisecond, // time unit
)
}
type stateStore struct {
pool *redis.Pool
c metrics.Counter
t metrics.Timer
}
func (ss *stateStore) stateHandler(deliveries <-chan amqp.Delivery, done chan error) {
for d := range deliveries {
ss.c.Inc(1)
start := time.Now()
log.Debugf(
"amqp key: %s payload: %dB delivery: [%v]",
d.RoutingKey,
len(d.Body),
d.DeliveryTag,
)
err := ss.savePayload(d.Body, d.RoutingKey)
if err != nil {
log.Errorf("failed to process payload: %s", err)
}
d.Ack(false)
ss.t.UpdateSince(start)
}
log.Debugf("handle: deliveries channel closed")
done <- nil
}
// cache the state in redis using a key based on state:{user_id}:{device_id}:{channel_id}
func (ss *stateStore) savePayload(body []byte, routingKey string) error {
params := getParams(routingKey)
if params == nil {
return fmt.Errorf("bad routing key - " + routingKey)
}
// state:123:b6b984190f:on-off
key := fmt.Sprintf("state:%s:%s:%s", params["user_id"], params["device_id"], params["channel_id"])
c := ss.pool.Get()
n, err := c.Do("SET", key, string(body))
if err != nil {
return err
}
log.Debugf("redis key = %s n = %v", key, n)
return nil
}
func getParams(routingKey string) map[string]string {
matches := userRegex.FindAllStringSubmatch(routingKey, -1)
if matches == nil {
return nil
}
params := make(map[string]string)
for i, attr := range userRegex.SubexpNames() {
if attr == "" {
continue
}
params[attr] = matches[0][i]
}
return params
}