This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
204 lines (162 loc) · 4.75 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
package main
import (
"bytes"
"compress/zlib"
"runtime/debug"
"regexp"
"runtime"
"time"
"github.com/EVE-Tools/emdr-to-nsq/lib/emds"
"github.com/EVE-Tools/emdr-to-nsq/lib/messageProcessing"
"github.com/boltdb/bolt"
"github.com/kelseyhightower/envconfig"
"github.com/mailru/easyjson"
"github.com/nsqio/go-nsq"
"github.com/pebbe/zmq4"
"github.com/sirupsen/logrus"
)
// Config holds the application's configuration info from the environment.
type Config struct {
LogLevel string `default:"info" envconfig:"log_level"`
EMDRRelayURL string `default:"tcp://relay-eu-germany-1.eve-emdr.com:8050" envconfig:"emdr_relay_url"`
NSQURL string `default:"nsqd:4150" envconfig:"nsq_url"`
GeneratorName string `envconfig:"generator_name"`
GeneratorVersion string `envconfig:"generator_version"`
CachePath string `default:"cache.db" envconfig:"cache_path"`
NameRegex *regexp.Regexp `ignored:"true"`
VersionRegex *regexp.Regexp `ignored:"true"`
}
// Stores main configuration
var config Config
// Channel for messages to be sent to NSQ
var nsqUpstream = make(chan []byte, 10000)
func main() {
// Load config and connect to queues
loadConfig()
connectToBolt()
connectToEMDR()
connectToNSQ()
// Terminate this goroutine, crash if all other goroutines exited
runtime.Goexit()
}
// Load configuration from environment and compile regexps
func loadConfig() {
envconfig.MustProcess("EMDR_TO_NSQ", &config)
logLevel, err := logrus.ParseLevel(config.LogLevel)
if err != nil {
panic(err)
}
logrus.SetLevel(logLevel)
if config.GeneratorName != "" {
config.NameRegex = regexp.MustCompile(config.GeneratorName)
}
if config.GeneratorVersion != "" {
config.VersionRegex = regexp.MustCompile(config.GeneratorVersion)
}
logrus.Info("\n ________ _______ ____ __ _ _______ ____ \n / ____/ |/ / __ \\/ __ \\ / /_____ / | / / ___// __ \\\n / __/ / /|_/ / / / / /_/ / / __/ __ \\ / |/ /\\__ \\/ / / /\n / /___/ / / / /_/ / _, _/ / /_/ /_/ / / /| /___/ / /_/ /\n/_____/_/ /_/_____/_/ |_| \\__/\\____/ /_/ |_//____/\\___\\_\\ v0.1")
logrus.Debugf("Config: %q", config)
}
// Connect to local boltdb
func connectToBolt() {
db, err := bolt.Open(config.CachePath, 0600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
panic(err)
}
messageProcessing.Initialize(db)
}
// Connect to EMDR and start listening for messages
func connectToEMDR() {
// create conn and pass to go listenToEMDR
connection, err := zmq4.NewSocket(zmq4.SUB)
if err != nil {
panic(err)
}
err = connection.SetSubscribe("")
if err != nil {
panic(err)
}
err = connection.Connect(config.EMDRRelayURL)
if err != nil {
panic(err)
}
// Ensure subscriber connection has time to complete
time.Sleep(time.Second)
go listenToEMDR(connection)
}
// Connect to NSQ and start processing messages
func connectToNSQ() {
// create conn and pass to go pushToNSQ
nsqConfig := nsq.NewConfig()
nsqConfig.Snappy = false
producer, err := nsq.NewProducer(config.NSQURL, nsqConfig)
if err != nil {
panic(err)
}
go pushToNSQ(producer)
}
// Push messages received to NSQ
func pushToNSQ(producer *nsq.Producer) {
defer producer.Stop()
for {
message := <-nsqUpstream
err := producer.Publish("orders", message)
if err != nil {
logrus.WithError(err).Warn("Could not publish message.")
}
}
}
// Listen for EMDR messages
func listenToEMDR(connection *zmq4.Socket) {
defer connection.Close()
for {
message, err := connection.RecvBytes(0)
if err != nil {
logrus.WithError(err).Error("Recv error.")
continue
}
go processMessage(message)
}
}
func processMessage(rawMessage []byte) {
message, err := decompress(rawMessage)
if err != nil {
logrus.WithError(err).Warn("Failed to decompress message.")
return
}
rowsets, err := messageProcessing.FilterMessage(config.NameRegex, config.VersionRegex, message)
if err != nil {
return
}
if len(rowsets) > 0 {
pushToOutbox(rowsets)
}
}
// Marshals and sends rowsets to NSQ goroutine.
func pushToOutbox(rowsets []emds.Rowset) error {
for _, rowset := range rowsets {
json, err := easyjson.Marshal(rowset)
if err != nil {
return err
}
nsqUpstream <- json
}
// Don't block OS memory for that long due to a bigger message every now and then
debug.FreeOSMemory()
return nil
}
// Decompresses message from EMDR.
func decompress(rawMessage []byte) ([]byte, error) {
// Decompress
rawMessageBuffer := bytes.NewReader(rawMessage)
rawMessageReader, err := zlib.NewReader(rawMessageBuffer)
if err != nil {
return nil, err
}
messageBuffer := new(bytes.Buffer)
_, err = messageBuffer.ReadFrom(rawMessageReader)
if err != nil {
return nil, err
}
rawMessageReader.Close()
return messageBuffer.Bytes(), nil
}