-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
348 lines (310 loc) · 10.6 KB
/
config.go
File metadata and controls
348 lines (310 loc) · 10.6 KB
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
348
// Copyright The ActForGood Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://github.com/actforgood/xconf/blob/main/LICENSE.
package xconf
import (
"reflect"
"runtime"
"strings"
"sync"
"time"
"github.com/spf13/cast"
)
// Config provides prototype for returning configurations.
type Config interface {
// Get returns a configuration value for a given key.
// The first parameter is the key to return the value for.
// The second parameter is optional, and represents a default
// value in case key is not found. It also has a role in inferring
// the type of key's value (if it exists) and thus key's value
// will be casted to default's value type.
Get(key string, def ...any) any
}
// DefaultConfig is the default implementation for the Config contract.
// It is based on a Loader to retrieve configuration from.
// Is implements [io.Closer] and thus Close should be called at
// your application shutdown in order to avoid memory leaks.
type DefaultConfig struct {
*defaultConfig // so we can use finalizer
}
type defaultConfig struct {
// loader to retrieve configuration from.
loader Loader
// configMap the loaded key-value configuration map.
configMap map[string]any
// observers contain the list of registered observers for changed keys.
observers []ConfigObserver
// refreshInterval represents the interval to reload the configMap.
// If it is <=0, reload will be disabled.
reloadInterval time.Duration
// reloadErrorHandler is an optional handler for errors occurred during reloading configuration.
// You can log the error, for example.
reloadErrorHandler func(error)
// ticker is used to reload the configMap at reloadInterval.
ticker *time.Ticker
// ignoreCaseSensitivity is a flag indicating whether keys' case sensitivity should be ignored.
ignoreCaseSensitivity bool
// mu is a concurrency semaphore for accessing the configMap.
mu *sync.RWMutex
// wg is a wait group used to notify main thread that reload goroutine stopped.
wg *sync.WaitGroup
// closed is a channel to notify reload goroutine to stop.
closed chan struct{}
}
// NewDefaultConfig instantiates a new default config object.
// The first parameter is the loader used as a source of getting the key-value configuration map.
// The second parameter represents a list of optional functions to configure the object.
func NewDefaultConfig(loader Loader, opts ...DefaultConfigOption) (*DefaultConfig, error) {
config := &DefaultConfig{&defaultConfig{
loader: loader,
mu: new(sync.RWMutex),
}}
// apply options, if any.
for _, opt := range opts {
opt(config)
}
if err := config.setConfigMap(); err != nil {
return nil, err
}
if config.reloadInterval > 0 {
config.ticker = time.NewTicker(config.reloadInterval)
config.closed = make(chan struct{}, 1)
config.wg = new(sync.WaitGroup)
config.wg.Go(config.reloadAsync)
// register also a finalizer, just in case, user forgets to call Close().
// Note: user should do not rely on this, it's recommended to explicitly call Close().
runtime.SetFinalizer(config, (*DefaultConfig).Close)
}
return config, nil
}
// Get returns a configuration value for a given key.
// The first parameter is the key to return the value for.
// The second parameter is optional, and represents a default
// value in case key is not found. It so has a role in inferring
// the type of key's value (if it exists) and thus key's value
// will be casted to default's value type.
// Only basic types (string, bool, int, uint, float, and their flavours),
// time.Duration, time.Time, []int, []string are covered.
// If a cast error occurs, the defaultValue is returned.
func (cfg *defaultConfig) Get(key string, def ...any) any {
if cfg.ignoreCaseSensitivity {
key = strings.ToUpper(key)
}
if cfg.reloadInterval > 0 {
// micro-optimization; in case reload is disabled, we don't have
// to protect with a mutex. See benchmarks.
cfg.mu.RLock()
}
value, foundKey := cfg.configMap[key]
if cfg.reloadInterval > 0 {
cfg.mu.RUnlock()
}
if len(def) > 0 {
defaultValue := def[0]
if !foundKey {
return defaultValue
}
if defaultValue != nil {
return castValueByDefault(value, defaultValue)
}
}
return value
}
// RegisterObserver adds a new observer that will get notified of keys changes.
func (cfg *defaultConfig) RegisterObserver(observer ConfigObserver) {
cfg.mu.Lock()
if cfg.observers == nil {
cfg.observers = []ConfigObserver{observer}
} else {
cfg.observers = append(cfg.observers, observer)
}
cfg.mu.Unlock()
}
// setConfigMap loads the config map.
func (cfg *defaultConfig) setConfigMap() error {
newConfigMap, err := cfg.loader.Load()
if err != nil {
return err
}
if cfg.ignoreCaseSensitivity {
toUppercaseConfigMap(newConfigMap)
}
cfg.mu.Lock()
oldConfigMap := cfg.configMap
cfg.configMap = newConfigMap
cfg.mu.Unlock()
cfg.notifyObservers(oldConfigMap, newConfigMap)
return nil
}
// notifyObservers computes changed (updated/deleted/new) keys on a config reload,
// and notifies registered observers about them, if there are any changed keys and observers.
func (cfg *defaultConfig) notifyObservers(oldConfigMap, newConfigMap map[string]any) {
cfg.mu.RLock()
defer cfg.mu.RUnlock()
if cfg.observers == nil || reflect.DeepEqual(oldConfigMap, newConfigMap) {
return
}
// max will be reached only if all old config map keys get deleted,
// highly improbable
maxChangedKeysCap := len(oldConfigMap) + len(newConfigMap)
changedKeys := make([]string, 0, maxChangedKeysCap)
for oldKey := range oldConfigMap { // compute updated/deleted keys
if !reflect.DeepEqual(oldConfigMap[oldKey], newConfigMap[oldKey]) {
changedKeys = append(changedKeys, oldKey)
}
}
for newKey := range newConfigMap { // compute new keys
if _, found := oldConfigMap[newKey]; !found {
changedKeys = append(changedKeys, newKey)
}
}
for _, notifyObserver := range cfg.observers {
notifyObserver(cfg, changedKeys...)
}
}
// reloadAsync reloads the config map asynchronous, interval based.
// Calling Close() will stop this goroutine.
func (cfg *defaultConfig) reloadAsync() {
for {
select {
case <-cfg.closed:
cfg.ticker.Stop()
return
case <-cfg.ticker.C:
if err := cfg.setConfigMap(); err != nil && cfg.reloadErrorHandler != nil {
cfg.reloadErrorHandler(err)
}
}
}
}
// close stops the underlying ticker used to reload config, avoiding memory leaks.
func (cfg *defaultConfig) close() {
if cfg != nil {
close(cfg.closed)
cfg.wg.Wait()
}
}
// Close stops the underlying ticker used to reload config, avoiding memory leaks.
// It should be called at your application shutdown.
// It implements [io.Closer] and the returned error can be disregarded (is nil all the time).
func (cfg *DefaultConfig) Close() error {
if cfg != nil && cfg.reloadInterval > 0 {
cfg.close()
runtime.SetFinalizer(cfg, nil)
}
return nil
}
// castValueByDefault casts a key's value to provided default value's type.
// Only basic types (string, bool, int, uint, float, and their flavours),
// time.Duration, time.Time, []int, []string are covered.
// If a cast error occurs, the defaultValue is returned.
func castValueByDefault(value, defaultValue any) any {
var (
castValue any
castErr error
)
switch defaultValue.(type) {
case string:
castValue, castErr = cast.ToStringE(value)
case int:
castValue, castErr = cast.ToIntE(value)
case uint:
castValue, castErr = cast.ToUintE(value)
case float64:
castValue, castErr = cast.ToFloat64E(value)
case bool:
castValue, castErr = cast.ToBoolE(value)
case time.Duration:
castValue, castErr = cast.ToDurationE(value)
case int64:
castValue, castErr = cast.ToInt64E(value)
case int32:
castValue, castErr = cast.ToInt32E(value)
case int16:
castValue, castErr = cast.ToInt16E(value)
case int8:
castValue, castErr = cast.ToInt8E(value)
case uint64:
castValue, castErr = cast.ToUint64E(value)
case uint32:
castValue, castErr = cast.ToUint32E(value)
case uint16:
castValue, castErr = cast.ToUint16E(value)
case uint8:
castValue, castErr = cast.ToUint8E(value)
case float32:
castValue, castErr = cast.ToFloat32E(value)
case time.Time:
castValue, castErr = cast.ToTimeE(value)
case []string:
castValue, castErr = cast.ToStringSliceE(value)
case []int:
castValue, castErr = cast.ToIntSliceE(value)
default:
castValue = value // not supported cast type, return directly the value
}
if castErr == nil {
return castValue
}
return defaultValue
}
// toUppercaseConfigMap transforms all (first level) keys to uppercase.
func toUppercaseConfigMap(configMap map[string]any) {
for key, value := range configMap {
delete(configMap, key)
// Note: here if a duplicate key exists, it will get overwritten.
configMap[strings.ToUpper(key)] = value
}
}
// DefaultConfigOption defines optional function for configuring
// a DefaultConfig object.
type DefaultConfigOption func(*DefaultConfig)
// DefaultConfigWithReloadInterval sets interval to reload configuration.
// Passing a value <= 0 disables the config reload.
//
// By default, configuration reload is disabled.
//
// Usage example:
//
// // enable config reload at an interval of 5 minutes:
// cfg, err := xconf.NewDefaultConfig(loader, xconf.DefaultConfigWithReloadInterval(5 * time.Minute))
func DefaultConfigWithReloadInterval(reloadInterval time.Duration) DefaultConfigOption {
return func(config *DefaultConfig) {
config.reloadInterval = reloadInterval
}
}
// DefaultConfigWithIgnoreCaseSensitivity disables case sensitivity for keys.
//
// For example, if the configuration map contains a key "Foo", calling Get() with "foo" / "FOO" / etc.
// will return Foo's value.
//
// Usage example:
//
// cfg, err := xconf.NewDefaultConfig(loader, xconf.DefaultConfigWithIgnoreCaseSensitivity())
// if err != nil {
// panic(err)
// }
// value1 := cfg.Get("foo")
// value2 := cfg.Get("FOO")
// value3 := cfg.Get("foO")
// // all values are equal
func DefaultConfigWithIgnoreCaseSensitivity() DefaultConfigOption {
return func(config *DefaultConfig) {
config.ignoreCaseSensitivity = true
}
}
// DefaultConfigWithReloadErrorHandler sets the handler for errors that may occur
// during reloading configuration, if DefaultConfigWithReloadInterval was applied.
// If reload fails, "old"/previous configuration is active.
//
// You can choose to log the error, for example.
//
// By default, error is simply ignored.
func DefaultConfigWithReloadErrorHandler(errHandler func(error)) DefaultConfigOption {
return func(config *DefaultConfig) {
config.reloadErrorHandler = errHandler
}
}
// ConfigObserver gets called to notify about changed keys on Config reload.
type ConfigObserver func(cfg Config, changedKeys ...string)