forked from blind-oracle/dnstap-bgp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncer.go
221 lines (174 loc) · 3.67 KB
/
syncer.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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"time"
)
type syncerCfg struct {
Listen string
SyncInterval string
Peers []string
}
type getAllFunc func() []*cacheEntry
type addFunc func(*cacheEntry, bool) bool
type syncFunc func(string, int, error)
type syncer struct {
s *http.Server
c *http.Client
syncInterval time.Duration
peers []string
getAll getAllFunc
add addFunc
syncCb syncFunc
shutdown chan struct{}
}
func newSyncer(cf *syncerCfg, getAll getAllFunc, add addFunc, syncCb syncFunc) (s *syncer, err error) {
s = &syncer{
getAll: getAll,
add: add,
peers: cf.Peers,
syncCb: syncCb,
shutdown: make(chan struct{}),
syncInterval: 10 * time.Minute,
}
if cf.SyncInterval != "" {
if s.syncInterval, err = time.ParseDuration(cf.SyncInterval); err != nil {
return nil, fmt.Errorf("Unable to parse syncInterval: %s", err)
}
}
if len(cf.Peers) > 0 {
s.c = &http.Client{
Timeout: 5 * time.Second,
}
}
if s.syncInterval > 0 {
go s.syncScheduler()
}
if cf.Listen == "" {
return
}
addr, err := net.ResolveTCPAddr("tcp", cf.Listen)
if err != nil {
return
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return
}
s.s = &http.Server{}
http.HandleFunc("/fetch", s.handleFetch)
http.HandleFunc("/put", s.handlePut)
go func() {
if err := s.s.Serve(l); err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
}()
return
}
func (s *syncer) handleFetch(wr http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
wr.WriteHeader(400)
return
}
json.NewEncoder(wr).Encode(s.getAll())
}
func (s *syncer) handlePut(wr http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
c := &cacheEntry{}
if err := json.NewDecoder(r.Body).Decode(c); err != nil && err != io.EOF {
wr.WriteHeader(400)
fmt.Fprintf(wr, "Bad request: %s", err)
return
}
s.add(c, false)
}
func (s *syncer) broadcast(e *cacheEntry) (err error) {
if s.c == nil {
return
}
for _, p := range s.peers {
if err = s.send(e, p); err != nil {
return
}
}
return
}
func (s *syncer) callPeer(p, handler, method string, body io.ReadCloser) (resp *http.Response, err error) {
u, _ := url.Parse(fmt.Sprintf("http://%s/%s", p, handler))
r := &http.Request{
Method: method,
URL: u,
Body: body,
}
if resp, err = s.c.Do(r); err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("HTTP Code not 200: %d", resp.StatusCode)
}
return
}
func (s *syncer) syncScheduler() {
t := time.NewTicker(s.syncInterval)
for {
select {
case <-t.C:
s.syncAll()
case <-s.shutdown:
return
}
}
}
func (s *syncer) syncAll() {
for _, p := range s.peers {
new := 0
es, err := s.fetchRemote(p)
if err != nil {
s.syncCb(p, 0, err)
continue
}
for _, e := range es {
if s.add(e, false) {
new++
}
}
log.Printf("Syncer: got %d (%d new) entries from peer %s", len(es), new, p)
s.syncCb(p, new, nil)
}
return
}
func (s *syncer) fetchRemote(p string) (es []*cacheEntry, err error) {
resp, err := s.callPeer(p, "fetch", "GET", nil)
if err != nil {
return
}
defer resp.Body.Close()
if err = json.NewDecoder(resp.Body).Decode(&es); err != nil {
return
}
return
}
func (s *syncer) send(e *cacheEntry, p string) (err error) {
js, _ := json.Marshal(e)
b := bytes.NewReader(js)
resp, err := s.callPeer(p, "put", "PUT", ioutil.NopCloser(b))
if err != nil {
return
}
resp.Body.Close()
return
}
func (s *syncer) close() error {
close(s.shutdown)
c, f := context.WithTimeout(context.Background(), 5*time.Second)
defer f()
return s.s.Shutdown(c)
}