/
os.go
298 lines (243 loc) · 4.28 KB
/
os.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
package config
import (
"bytes"
"errors"
"log"
"sync"
"time"
"github.com/micro/go-micro/client"
)
type os struct {
exit chan bool
opts Options
sync.RWMutex
cset *ChangeSet
vals Values
idx int
watchers map[int]*watcher
}
type watcher struct {
exit chan bool
path []string
value Value
updates chan Value
}
func newOS(opts ...Option) Config {
options := Options{
PollInterval: DefaultPollInterval,
Reader: NewReader(),
}
for _, o := range opts {
o(&options)
}
if options.Client == nil {
options.Client = client.DefaultClient
}
if options.Sources == nil {
// Set a os source
options.Sources = append(options.Sources, NewSource(SourceClient(options.Client)))
}
o := &os{
exit: make(chan bool),
opts: options,
watchers: make(map[int]*watcher),
}
go o.run()
return o
}
func (o *os) run() {
t := time.NewTicker(o.opts.PollInterval)
for {
select {
case <-t.C:
o.sync()
case <-o.exit:
t.Stop()
return
}
}
}
func (o *os) loaded() bool {
var loaded bool
o.RLock()
if o.vals != nil {
loaded = true
}
o.RUnlock()
return loaded
}
func (o *os) update() {
var watchers []*watcher
o.RLock()
for _, w := range o.watchers {
watchers = append(watchers, w)
}
o.RUnlock()
for _, w := range watchers {
select {
case w.updates <- Get(w.path...):
default:
}
}
}
// sync loads all the sources, calls the parser and updates the config
func (o *os) sync() {
if len(o.opts.Sources) == 0 {
log.Printf("Zero sources available to sync")
return
}
var sets []*ChangeSet
for _, source := range o.opts.Sources {
ch, err := source.Read()
// should we actually skip failing sources?
// best effort merging right? but what if we
// already have good config? that would be screwed
if err != nil {
o.RLock()
vals := o.vals
o.RUnlock()
// if we have no config, we're going to try
// load something
if vals == nil {
continue
} else {
return
}
}
sets = append(sets, ch)
}
set, err := o.opts.Reader.Parse(sets...)
if err != nil {
log.Printf("Failed to parse ChangeSets %v", err)
return
}
o.Lock()
o.vals, _ = o.opts.Reader.Values(set)
o.cset = set
o.Unlock()
o.update()
}
func (o *os) Close() error {
select {
case <-o.exit:
return nil
default:
close(o.exit)
}
return nil
}
func (o *os) Get(path ...string) Value {
if !o.loaded() {
o.sync()
}
o.Lock()
defer o.Unlock()
// did sync actually work?
if o.vals != nil {
return Get(path...)
}
ch := o.cset
// we are truly screwed, trying to load in a hacked way
v, err := o.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 = &ChangeSet{
Timestamp: time.Now(),
Source: o.String(),
Data: []byte(`{}`),
}
}
v, _ = o.opts.Reader.Values(ch)
}
// lets set it just because
o.vals = v
if o.vals != nil {
return Get(path...)
}
// ok we're going hardcore now
return newValue(nil)
}
func (o *os) Del(path ...string) {
if !o.loaded() {
o.sync()
}
o.Lock()
defer o.Unlock()
if o.vals != nil {
Del(path...)
}
}
func (o *os) Set(val interface{}, path ...string) {
if !o.loaded() {
o.sync()
}
o.Lock()
defer o.Unlock()
if o.vals != nil {
Set(val, path...)
}
}
func (o *os) Bytes() []byte {
if !o.loaded() {
o.sync()
}
o.Lock()
defer o.Unlock()
if o.vals == nil {
return []byte{}
}
return Bytes()
}
func (o *os) Options() Options {
return o.opts
}
func (o *os) String() string {
return "os"
}
func (o *os) Watch(path ...string) (Watcher, error) {
value := o.Get(path...)
o.Lock()
w := &watcher{
exit: make(chan bool),
path: path,
value: value,
updates: make(chan Value, 1),
}
id := o.idx
o.watchers[id] = w
o.idx++
o.Unlock()
go func() {
<-w.exit
o.Lock()
delete(o.watchers, id)
o.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(Bytes(), Bytes()) {
continue
}
w.value = v
return v, nil
}
}
}
func (w *watcher) Stop() error {
select {
case <-w.exit:
default:
close(w.exit)
}
return nil
}