-
Notifications
You must be signed in to change notification settings - Fork 0
/
consulWatch.go
301 lines (247 loc) · 5.82 KB
/
consulWatch.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package consulWatch
import (
"errors"
"strings"
"time"
"github.com/cenkalti/backoff"
"github.com/hashicorp/consul/api"
)
type ConsulWatched struct {
Consul *api.Client
Prefix string
UpdateChan chan map[string]interface{}
CreateChan chan map[string]interface{}
DeleteChan chan map[string]interface{}
ErrChan chan error
WaitTimeout time.Duration
updateIndex map[string]uint64
stopChan chan bool
}
var WaitTimeout = 10 * time.Minute
var watchers = make([]*ConsulWatched, 1)
var (
ErrPrefix = errors.New("invalid prefix")
)
func (w *ConsulWatched) Watch() error {
// Check prefix format
if w.Prefix == "" {
return ErrPrefix
}
if w.Prefix[len(w.Prefix)-1] != '/' {
w.Prefix += "/"
}
if err := w.setDefaults(); err != nil {
return err
}
stopCh := make(chan bool)
w.stopChan = stopCh
w.updateIndex = make(map[string]uint64)
errCh := w.ErrChan
pairCh := make(chan api.KVPairs)
// The first goroutine we run just sits and waits for updated
// KVPairs from Consul. This keeps track of the ModifyIndex to use.
go func() {
var waitIndex uint64
for {
// Setup our variables and query options for the query
var pairs api.KVPairs
var meta *api.QueryMeta
queryOpts := &api.QueryOptions{
WaitIndex: waitIndex,
WaitTime: w.WaitTimeout,
}
// Perform a query with exponential backoff to get our pairs
err := backoff.Retry(func() error {
// If the quitCh is closed, then just return now and
// don't make anymore queries to ConsulConnection.
select {
case <-stopCh:
return nil
default:
}
// Query
var err error
pairs, meta, err = w.Consul.KV().List(w.Prefix, queryOpts)
// Been block waiting so first check if stopCh is closed and need to return now
select {
case <-stopCh:
return nil
default:
}
if err != nil {
errCh <- err
}
return err
}, w.backOffConf())
if err != nil {
// These get sent by list as said in mitchellh/consulstructure
continue
}
// Check for quit. If so, quit.
select {
case <-stopCh:
return
default:
}
// If we have the same index, then we didn't find any new values.
if meta.LastIndex == waitIndex {
continue
}
// Update our wait index
waitIndex = meta.LastIndex
// Send the pairs
pairCh <- pairs
}
}()
// Qsc settings
qscPeriod := 500 * time.Millisecond
qscTimeout := 5 * time.Second
// Listen for pair updates, wait the proper quiesence periods, and
// trigger configuration updates.
go func() {
init := false
var pairs api.KVPairs
var qscPeriodCh, qscTimeoutCh <-chan time.Time
for {
select {
case <-stopCh:
// Exit
return
case pairs = <-pairCh:
// Setup our qsc timers and reloop
qscPeriodCh = time.After(qscPeriod)
if qscTimeoutCh == nil {
qscTimeoutCh = time.After(qscTimeout)
}
// If we've initialized already, then we wait for qsc.
// Otherwise, we go through for the initial config.
if init {
continue
}
init = true
case <-qscPeriodCh:
case <-qscTimeoutCh:
}
// Set our timers to nil for the next data
qscPeriodCh = nil
qscTimeoutCh = nil
// Decode and send
if err := w.process(pairs); err != nil {
errCh <- err
}
}
}()
return nil
}
func (w *ConsulWatched) setDefaults() error {
// Create/use created consul if needed
if w.Consul == nil {
err := w.setDefaultConsul()
if err != nil {
return err
}
}
// Create requirement channels if needed
if w.UpdateChan == nil {
w.UpdateChan = make(chan map[string]interface{})
}
if w.ErrChan == nil {
w.ErrChan = make(chan error)
}
// Default wait timeout
if w.WaitTimeout == 0 {
w.WaitTimeout = WaitTimeout
}
return nil
}
func (w *ConsulWatched) setDefaultConsul() error {
if len(watchers) > 1 {
w.Consul = watchers[0].Consul
watchers = append(watchers, w)
return nil
}
config := api.DefaultConfig()
config.Address = "127.0.0.1:8500"
con, err := api.NewClient(config)
if err != nil {
return err
}
w.Consul = con
return nil
}
func (w *ConsulWatched) backOffConf() backoff.BackOff {
conf := backoff.NewExponentialBackOff()
conf.InitialInterval = 1 * time.Second
conf.MaxInterval = 10 * time.Second
conf.MaxElapsedTime = 0
return conf
}
func (w *ConsulWatched) Stop() {
w.stopChan <- true
for i, v := range watchers {
if v == w {
watchers[i] = watchers[len(watchers) - 1]
watchers = watchers[:len(watchers) - 1]
return
}
}
}
func (w *ConsulWatched) process(pairs api.KVPairs) error {
var created, deleted map[string]interface{}
updated := make(map[string]interface{})
// Split channels if needed
if w.CreateChan != nil {
created = make(map[string]interface{})
} else {
created = updated
}
if w.DeleteChan != nil {
deleted = make(map[string]interface{})
} else {
deleted = updated
}
// Create set of existing before processing keys
for k := range w.updateIndex {
deleted[k] = nil
}
for _, p := range pairs {
// Trim the Prefix off our key first
key := strings.TrimPrefix(p.Key, w.Prefix)
// Ignore . path
if key == "" {
continue
}
m := created
// Filter only updated values
if w.updateIndex != nil {
// Check if key already processed previously
if index, exists := w.updateIndex[key]; exists {
// Delete existing key from deleted set
delete(deleted, key)
// Check key index. If modified, when parse
if index == p.ModifyIndex {
continue
} else {
m = updated
}
}
w.updateIndex[key] = p.ModifyIndex
}
m[key] = p.Value
}
// Remove deleted entries from watcher list
for key := range deleted {
delete(w.updateIndex, key)
}
// Send updates
if len(updated) != 0 {
w.UpdateChan <- updated
}
if w.CreateChan != nil && len(created) != 0 {
w.CreateChan <- created
}
if w.DeleteChan != nil && len(deleted) != 0 {
w.DeleteChan <- deleted
}
return nil
}