-
Notifications
You must be signed in to change notification settings - Fork 75
/
default.go
347 lines (283 loc) · 5.11 KB
/
default.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package config
import (
"bytes"
"errors"
"log"
"sync"
"time"
"github.com/micro/go-config/reader"
"github.com/micro/go-config/reader/json"
"github.com/micro/go-config/source"
)
type config struct {
exit chan bool
opts Options
sync.RWMutex
// the current merged set
set *source.ChangeSet
// the current values
vals reader.Values
// all the sets
sets []*source.ChangeSet
// all the sources
sources []source.Source
idx int
watchers map[int]*watcher
}
type watcher struct {
exit chan bool
path []string
value Value
updates chan Value
}
func newConfig(opts ...Option) Config {
options := Options{
Reader: json.NewReader(),
}
for _, o := range opts {
o(&options)
}
c := &config{
exit: make(chan bool),
opts: options,
watchers: make(map[int]*watcher),
sources: options.Source,
}
for i, s := range options.Source {
go c.watch(i, s)
}
return c
}
func (c *config) watch(idx int, s source.Source) {
c.Lock()
c.sets = append(c.sets, nil)
c.Unlock()
// watches a source for changes
watch := func(idx int, s source.Watcher) error {
for {
// get changeset
cs, err := s.Next()
if err != nil {
return err
}
c.Lock()
// save
c.sets[idx] = cs
// merge sets
set, err := c.opts.Reader.Parse(c.sets...)
if err != nil {
return err
}
// set values
c.vals, _ = c.opts.Reader.Values(set)
c.set = set
c.Unlock()
// send watch updates
c.update()
}
}
for {
// watch the source
w, err := s.Watch()
if err != nil {
time.Sleep(time.Second)
continue
}
done := make(chan bool)
// the stop watch func
go func() {
select {
case <-done:
case <-c.exit:
}
w.Stop()
}()
// block watch
if err := watch(idx, w); err != nil {
// do something better
time.Sleep(time.Second)
}
// close done chan
close(done)
// if the config is closed exit
select {
case <-c.exit:
return
default:
}
}
}
func (c *config) loaded() bool {
var loaded bool
c.RLock()
if c.vals != nil {
loaded = true
}
c.RUnlock()
return loaded
}
func (c *config) update() {
var watchers []*watcher
c.RLock()
for _, w := range c.watchers {
watchers = append(watchers, w)
}
c.RUnlock()
for _, w := range watchers {
select {
case w.updates <- c.vals.Get(w.path...):
default:
}
}
}
// sync loads all the sources, calls the parser and updates the config
func (c *config) sync() {
var sets []*source.ChangeSet
c.Lock()
// read the source
for _, source := range c.sources {
ch, err := source.Read()
if err != nil {
continue
}
sets = append(sets, ch)
}
// merge sets
set, err := c.opts.Reader.Parse(sets...)
if err != nil {
return
}
// set values
c.vals, _ = c.opts.Reader.Values(set)
c.set = set
c.Unlock()
// update watchers
c.update()
}
// reload reads the sets and creates new values
func (c *config) reload() {
c.Lock()
// merge sets
set, err := c.opts.Reader.Parse(c.sets...)
if err != nil {
c.Unlock()
return
}
// set values
c.vals, _ = c.opts.Reader.Values(set)
c.set = set
c.Unlock()
// update watchers
c.update()
}
func (c *config) Close() error {
select {
case <-c.exit:
return nil
default:
close(c.exit)
}
return nil
}
func (c *config) Get(path ...string) Value {
if !c.loaded() {
c.sync()
}
c.Lock()
defer c.Unlock()
// did sync actually work?
if c.vals != nil {
return c.vals.Get(path...)
}
ch := c.set
// we are truly screwed, trying to load in a hacked way
v, err := c.opts.Reader.Values(ch)
if err != nil {
log.Printf("Failed to read values %v trying again", err)
// man we're so screwed
// Let's try hack this
// We should really be better
if ch == nil || ch.Data == nil {
ch = &source.ChangeSet{
Timestamp: time.Now(),
Source: "config",
Data: []byte(`{}`),
}
}
v, _ = c.opts.Reader.Values(ch)
}
// lets set it just because
c.vals = v
if c.vals != nil {
return c.vals.Get(path...)
}
// ok we're going hardcore now
return newValue()
}
func (c *config) Bytes() []byte {
if !c.loaded() {
c.sync()
}
c.Lock()
defer c.Unlock()
if c.vals == nil {
return []byte{}
}
return c.vals.Bytes()
}
func (c *config) Load(sources ...source.Source) error {
for _, source := range sources {
set, _ := source.Read()
c.Lock()
c.sources = append(c.sources, source)
c.sets = append(c.sets, set)
idx := len(c.sets) - 1
c.Unlock()
go c.watch(idx, source)
}
c.reload()
return nil
}
func (c *config) Watch(path ...string) (Watcher, error) {
value := c.Get(path...)
c.Lock()
w := &watcher{
exit: make(chan bool),
path: path,
value: value,
updates: make(chan Value, 1),
}
id := c.idx
c.watchers[id] = w
c.idx++
c.Unlock()
go func() {
<-w.exit
c.Lock()
delete(c.watchers, id)
c.Unlock()
}()
return w, nil
}
func (w *watcher) Next() (Value, error) {
for {
select {
case <-w.exit:
return nil, errors.New("watcher stopped")
case v := <-w.updates:
if bytes.Equal(w.value.Bytes(), v.Bytes()) {
continue
}
w.value = v
return v, nil
}
}
}
func (w *watcher) Stop() error {
select {
case <-w.exit:
default:
close(w.exit)
}
return nil
}