-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
164 lines (144 loc) · 3.18 KB
/
watcher.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
package nsrecorder // import "jw4.us/nsrecorder"
import (
"context"
"encoding/json"
"fmt"
"log"
"net"
"os"
"strings"
"time"
nsq "github.com/nsqio/go-nsq"
"github.com/pkg/errors"
)
func NewWatcher(ctx context.Context, store Store) *Watcher {
w := &Watcher{ctx: ctx, store: store, msg: make(chan *nsq.Message)}
w.start()
return w
}
type Watcher struct {
ctx context.Context
store Store
msg chan *nsq.Message
consumer *nsq.Consumer
}
func (w *Watcher) HandleMessage(message *nsq.Message) error {
message.Touch()
w.log(message)
return nil
}
func (w *Watcher) Stop() {
<-w.consumer.StopChan
}
func (w *Watcher) start() {
topic, ok := w.ctx.Value("topic").(string)
if !ok {
panic("no topic")
}
channel, ok := w.ctx.Value("channel").(string)
if !ok {
panic("no channel")
}
lookupd, ok := w.ctx.Value("lookupd").([]string)
if !ok {
panic("no lookupd")
}
config := nsq.NewConfig()
config.ClientID = "nsr"
config.Hostname, _ = os.Hostname()
config.UserAgent = "nsr go client"
config.MaxInFlight = 10
var err error
if w.consumer, err = nsq.NewConsumer(topic, channel, config); err != nil {
panic(err)
}
w.consumer.AddConcurrentHandlers(w, 10)
go w.loop()
if err = w.consumer.ConnectToNSQLookupds(lookupd); err != nil {
panic(err)
}
}
func (w *Watcher) loop() {
messages := []*nsq.Message{}
for {
after := time.After(5 * time.Second)
select {
case msg := <-w.msg:
messages = append(messages, msg)
case <-after:
if len(messages) > 0 {
w.handleBatch(messages)
messages = []*nsq.Message{}
}
case <-w.ctx.Done():
w.consumer.Stop()
return
}
}
}
func (w *Watcher) handleBatch(messages []*nsq.Message) {
fmt.Printf("Processing: %v\n", time.Now())
clients := []Client{}
lookups := []Lookup{}
for _, msg := range messages {
c, l, err := parse(msg)
if err != nil {
log.Printf("error in parse: %v", err)
msg.Requeue(-1)
continue
}
clients = append(clients, c)
lookups = append(lookups, l)
}
err := w.store.Accept(clients, lookups)
for _, msg := range messages {
switch err {
case nil:
msg.Finish()
default:
log.Printf("error in store.Accept: %v", err)
msg.Requeue(-1)
}
}
}
func (w *Watcher) log(message *nsq.Message) {
w.msg <- message
}
func parse(rawmsg *nsq.Message) (Client, Lookup, error) {
var (
client Client
lookup Lookup
err error
hosts, qhosts, aips []string
)
msg := Message{}
if err = json.Unmarshal(rawmsg.Body, &msg); err != nil {
log.Printf("unmarshaling nsq message: %v\n%s", err, rawmsg.Body)
return client, lookup, errors.Wrap(err, "unmarshaling nsq.Message")
}
client.IP = msg.ClientIP
client.Name = msg.ClientIP
if hosts, err = net.LookupAddr(msg.ClientIP); err == nil {
client.Name = hosts[0]
}
lookup.When = msg.Time
lookup.Client = msg.ClientIP
for _, q := range msg.Msg.Question {
qhosts = append(qhosts, q.Name)
}
lookup.Host = strings.Join(qhosts, ",")
for _, a := range msg.Msg.Answer {
if a.A != "" {
aips = append(aips, a.A)
}
if a.AAAA != "" {
aips = append(aips, a.AAAA)
}
}
if len(aips) > 0 {
lookup.FirstIP = aips[0]
lookup.AllIPs = make([]string, len(aips))
copy(lookup.AllIPs, aips)
}
return client, lookup, nil
}