/
distconf.go
343 lines (305 loc) · 8.42 KB
/
distconf.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
package distconf
import (
"expvar"
"math"
"runtime"
"sync"
"time"
"github.com/signalfx/golib/v3/log"
"github.com/signalfx/golib/v3/logkey"
)
// DefaultLogger is used by package structs that don't have a default logger set.
var DefaultLogger = log.Logger(log.DefaultLogger.CreateChild())
// Distconf gets configuration data from the first backing that has it
type Distconf struct {
Logger log.Logger
readers []Reader
varsMutex sync.Mutex
infoMutex sync.RWMutex
registeredVars map[string]*registeredVariableTracker
distInfos map[string]DistInfo
callerFunc func(int) (uintptr, string, int, bool)
}
type registeredVariableTracker struct {
distvar configVariable
hasInitialized sync.Once
}
// New creates a Distconf from a list of backing readers
func New(readers []Reader) *Distconf {
return &Distconf{
Logger: DefaultLogger,
readers: readers,
registeredVars: make(map[string]*registeredVariableTracker),
distInfos: make(map[string]DistInfo),
}
}
type configVariable interface {
Update(newValue []byte) error
// Get but on an interface return. Oh how I miss you templates.
GenericGet() interface{}
GenericGetDefault() interface{}
Type() DistType
}
type noopCloser struct {
}
func (n *noopCloser) Close() {
//nolint
return
}
// DistType is used to type each of the DistInfos
type DistType int
const (
// StrType is type Str
StrType DistType = iota
// BoolType is type Bool
BoolType
// FloatType is type Float
FloatType
// DurationType is type Duration
DurationType
// IntType is type Int
IntType
)
// DistInfo is useful to unmarshal/marshal the Info expvar
type DistInfo struct {
File string `json:"file"`
Line int `json:"line"`
DefaultValue interface{} `json:"default_value"`
DistType DistType `json:"dist_type"`
}
func (c *Distconf) grabInfo(key string) {
if c.callerFunc == nil {
c.callerFunc = runtime.Caller
}
_, file, line, ok := c.callerFunc(2)
if !ok {
c.Logger.Log(logkey.DistconfKey, key, "unable to find call for distconf")
}
info := DistInfo{
File: file,
Line: line,
}
c.infoMutex.Lock()
defer c.infoMutex.Unlock()
c.distInfos[key] = info
}
// Var returns an expvar variable that shows all the current configuration variables and their
// current value
func (c *Distconf) Var() expvar.Var {
return expvar.Func(func() interface{} {
c.varsMutex.Lock()
defer c.varsMutex.Unlock()
m := make(map[string]interface{}, len(c.registeredVars))
for name, v := range c.registeredVars {
m[name] = v.distvar.GenericGet()
}
return m
})
}
// Info returns an expvar variable that shows the information for all configuration variables.
// Information consist of file, line, default value and type of variable.
func (c *Distconf) Info() expvar.Var {
return expvar.Func(func() interface{} {
c.infoMutex.RLock()
defer c.infoMutex.RUnlock()
m := make(map[string]DistInfo, len(c.distInfos))
for k, i := range c.distInfos {
v, ok := c.registeredVars[k]
if ok {
v := DistInfo{
File: i.File,
Line: i.Line,
DefaultValue: v.distvar.GenericGetDefault(),
DistType: v.distvar.Type(),
}
m[k] = v
}
}
return m
})
}
// Int object that can be referenced to get integer values from a backing config
func (c *Distconf) Int(key string, defaultVal int64) *Int {
c.grabInfo(key)
s := &intConf{
defaultVal: defaultVal,
Int: Int{
currentVal: defaultVal,
},
}
// Note: in race conditions 's' may not be the thing actually returned
ret, okCast := c.createOrGet(key, s).(*intConf)
if !okCast {
c.Logger.Log(logkey.DistconfKey, key, "Registering key with multiple types! FIX ME!!!!")
return nil
}
return &ret.Int
}
// Float object that can be referenced to get float values from a backing config
func (c *Distconf) Float(key string, defaultVal float64) *Float {
c.grabInfo(key)
s := &floatConf{
defaultVal: defaultVal,
Float: Float{
currentVal: math.Float64bits(defaultVal),
},
}
// Note: in race conditions 's' may not be the thing actually returned
ret, okCast := c.createOrGet(key, s).(*floatConf)
if !okCast {
c.Logger.Log(logkey.DistconfKey, key, "Registering key with multiple types! FIX ME!!!!")
return nil
}
return &ret.Float
}
// Str object that can be referenced to get string values from a backing config
func (c *Distconf) Str(key string, defaultVal string) *Str {
c.grabInfo(key)
s := &strConf{
defaultVal: defaultVal,
}
s.currentVal.Store(defaultVal)
// Note: in race conditions 's' may not be the thing actually returned
ret, okCast := c.createOrGet(key, s).(*strConf)
if !okCast {
c.Logger.Log(logkey.DistconfKey, key, "Registering key with multiple types! FIX ME!!!!")
return nil
}
return &ret.Str
}
// Bool object that can be referenced to get boolean values from a backing config
func (c *Distconf) Bool(key string, defaultVal bool) *Bool {
c.grabInfo(key)
var defautlAsInt int32
if defaultVal {
defautlAsInt = 1
} else {
defautlAsInt = 0
}
s := &boolConf{
defaultVal: defautlAsInt,
Bool: Bool{
currentVal: defautlAsInt,
},
}
// Note: in race conditions 's' may not be the thing actually returned
ret, okCast := c.createOrGet(key, s).(*boolConf)
if !okCast {
c.Logger.Log(logkey.DistconfKey, key, "Registering key with multiple types! FIX ME!!!!")
return nil
}
return &ret.Bool
}
// Duration returns a duration object that calls ParseDuration() on the given key
func (c *Distconf) Duration(key string, defaultVal time.Duration) *Duration {
c.grabInfo(key)
s := &durationConf{
defaultVal: defaultVal,
Duration: Duration{
currentVal: defaultVal.Nanoseconds(),
},
logger: log.NewContext(c.Logger).With(logkey.DistconfKey, key),
}
// Note: in race conditions 's' may not be the thing actually returned
ret, okCast := c.createOrGet(key, s).(*durationConf)
if !okCast {
c.Logger.Log(logkey.DistconfKey, key, "Registering key with multiple types! FIX ME!!!!")
return nil
}
return &ret.Duration
}
// Close this config framework's readers. Config variable results are undefined after this call.
func (c *Distconf) Close() {
c.varsMutex.Lock()
defer c.varsMutex.Unlock()
for _, backing := range c.readers {
backing.Close()
}
}
func (c *Distconf) refresh(key string, configVar configVariable) bool {
dynamicReadersOnPath := false
for _, backing := range c.readers {
if !dynamicReadersOnPath {
_, ok := backing.(Dynamic)
if ok {
dynamicReadersOnPath = true
}
}
v, e := backing.Get(key)
if e != nil {
c.Logger.Log(logkey.DistconfKey, key, log.Err, e, "Unable to read from backing")
continue
}
if v != nil {
e = configVar.Update(v)
if e != nil {
c.Logger.Log(logkey.DistconfKey, key, log.Err, e, "Invalid config bytes")
}
return dynamicReadersOnPath
}
}
e := configVar.Update(nil)
if e != nil {
c.Logger.Log(log.Err, e, "Unable to set bytes to nil/clear")
}
// If this is false, then the variable is fixed and can never change
return dynamicReadersOnPath
}
func (c *Distconf) watch(key string, configVar configVariable) {
for _, backing := range c.readers {
d, ok := backing.(Dynamic)
if ok {
err := d.Watch(key, c.onBackingChange)
if err != nil {
c.Logger.Log(logkey.DistconfKey, key, log.Err, err, "Unable to watch for config var")
}
}
}
}
func (c *Distconf) createOrGet(key string, defaultVar configVariable) configVariable {
c.varsMutex.Lock()
rv, exists := c.registeredVars[key]
if !exists {
rv = ®isteredVariableTracker{
distvar: defaultVar,
}
c.registeredVars[key] = rv
}
c.varsMutex.Unlock()
rv.hasInitialized.Do(func() {
dynamicOnPath := c.refresh(key, rv.distvar)
if dynamicOnPath {
c.watch(key, rv.distvar)
}
})
return rv.distvar
}
func (c *Distconf) onBackingChange(key string) {
c.varsMutex.Lock()
m, exists := c.registeredVars[key]
c.varsMutex.Unlock()
if !exists {
c.Logger.Log(logkey.DistconfKey, key, "Backing callback on variable that doesn't exist")
return
}
c.refresh(key, m.distvar)
}
// Reader can get a []byte value for a config key
type Reader interface {
Get(key string) ([]byte, error)
Close()
}
// Writer can modify Config properties
type Writer interface {
Write(key string, value []byte) error
}
type backingCallbackFunction func(string)
// A Dynamic config can change what it thinks a value is over time.
type Dynamic interface {
Watch(key string, callback backingCallbackFunction) error
}
// A ReaderWriter can both read and write configuration information
type ReaderWriter interface {
Reader
Writer
}