-
Notifications
You must be signed in to change notification settings - Fork 6
/
snapshot.go
357 lines (332 loc) · 11 KB
/
snapshot.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
348
349
350
351
352
353
354
355
356
357
package configcat
import (
"errors"
"fmt"
"reflect"
"sync"
"sync/atomic"
"time"
)
// Snapshot holds a snapshot of the ConfigCat configuration.
// A snapshot is immutable once taken.
//
// A nil snapshot is OK to use and acts like a configuration
// with no keys.
type Snapshot struct {
logger *leveledLogger
config *config
hooks *Hooks
originalUser User
user reflect.Value
userTypeInfo *userTypeInfo
allKeys []string
// values holds the value for each possible value ID, as stored in config.values.
values []interface{}
// valueIds holds precalculated value IDs as stored in config.valueIds.
valueIds []valueID
// cache records the value IDs that have been
// recorded for values that are not known ahead
// of time. Zero IDs represent Zero IDs represent unevaluated results.
// Entries are accessed and updated atomically which
// provides a mechanism whereby the result for any given key
// is only computed once for a given snapshot.
//
// The slot for a given key k is found at cache[-precalc[keyID(k)]-1].
//
// The cache is created by makeCache.
cache []valueID
makeCache sync.Once
// evaluators maps keyID to the evaluator for that key.
evaluators []settingEvalFunc
}
// NewSnapshot returns a snapshot that always returns the given values.
//
// Each entry in the values map is keyed by a flag
// name and holds the value that the snapshot will return
// for that flag. Each value must be one of the types
// bool, int, float64, or string.
//
// The returned snapshot does not support variation IDs. That is, given a
// snapshot s returned by NewSnapshot:
// - s.GetKeyValueForVariationID returns "", nil.
// - s.GetVariationID returns "".
// - s.GetVariationIDs returns nil.
func NewSnapshot(logger Logger, values map[string]interface{}) (*Snapshot, error) {
valuesSlice := make([]interface{}, numKeys())
keys := make([]string, 0, len(values))
for name, val := range values {
switch val.(type) {
case bool, int, float64, string:
default:
return nil, fmt.Errorf("value for flag %q has unexpected type %T (%#v); must be bool, int, float64 or string", name, val, val)
}
id := idForKey(name, true)
if int(id) >= len(valuesSlice) {
// We've added a new key, so expand the slices.
// This should be a rare case, so don't worry about
// this happening several times within this loop.
valuesSlice1 := make([]interface{}, id+1)
copy(valuesSlice1, valuesSlice)
valuesSlice = valuesSlice1
}
valuesSlice[id] = val
keys = append(keys, name)
}
// Save some allocations by using the same closure for every key.
eval := func(id keyID, _ reflect.Value, _ *userTypeInfo, _ *evalLogBuilder, _ *leveledLogger) (valueID, string, *TargetingRule, *PercentageOption, error) {
return valueID(id) + 1, "", nil, nil, nil
}
evaluators := make([]settingEvalFunc, len(valuesSlice))
valueIds := make([]valueID, len(valuesSlice))
for i := range evaluators {
evaluators[i] = eval
valueIds[i] = valueID(i) + 1
}
return &Snapshot{
logger: newLeveledLogger(logger, LogLevelNone, nil),
evaluators: evaluators,
allKeys: keys,
values: valuesSlice,
valueIds: valueIds,
}, nil
}
func newSnapshot(cfg *config, user User, logger *leveledLogger, hooks *Hooks) *Snapshot {
if cfg != nil && (user == nil || user == cfg.defaultUser) {
return cfg.defaultUserSnapshot
}
return _newSnapshot(cfg, user, logger, hooks)
}
// _newSnapshot is like newSnapshot except that it doesn't check
// whether user is nil. It should only be used by the parseConfig code
// for initializing config.noUserSnapshot.
func _newSnapshot(cfg *config, user User, logger *leveledLogger, hooks *Hooks) *Snapshot {
snap := &Snapshot{
config: cfg,
user: reflect.ValueOf(user),
logger: logger,
originalUser: user,
hooks: hooks,
}
if cfg == nil {
return snap
}
if user != nil && !snap.user.IsNil() {
userInfo, err := cfg.getOrNewUserTypeInfo(snap.user.Type())
if err != nil {
logger.Errorf(0, "%v", err)
return snap
}
snap.userTypeInfo = userInfo
if userInfo.deref {
snap.user = snap.user.Elem()
}
}
snap.evaluators = cfg.evaluators
snap.values = cfg.values
snap.allKeys = cfg.allKeys
snap.valueIds = cfg.valueIds
return snap
}
// WithUser returns a copy of s associated with the
// given user. If snap is nil, it returns nil.
// If user is nil, it uses Config.DefaultUser.
func (snap *Snapshot) WithUser(user User) *Snapshot {
if snap == nil || snap.config == nil {
// Note: when there's no config, we know there are no
// rules that can change the values returned, so no
// need to do anything.
return snap
}
if user == nil || user == snap.config.defaultUser {
return snap.config.defaultUserSnapshot
}
return newSnapshot(snap.config, user, snap.logger, snap.hooks)
}
func (snap *Snapshot) value(id keyID, key string) interface{} {
if snap == nil {
return nil
}
if snap.logger.enabled(LogLevelInfo) || int(id) >= len(snap.valueIds) || (snap.hooks != nil && snap.hooks.OnFlagEvaluated != nil) {
// We want to see logs, or we don't know about the key so use the slow path.
return snap.valueFromDetails(id, key)
}
valID := snap.valueIds[id]
if valID > 0 {
// We've got a precalculated value for the key.
return snap.valueForID(valID)
}
if valID == 0 {
// The key isn't found in this configuration.
if !snap.logger.enabled(LogLevelError) {
return nil
}
// Use the default implementation which will do the
// appropriate logging for us.
return snap.valueFromDetails(id, key)
}
// Look up the key in the cache.
cacheIndex := int(-valID - 1)
snap.initCache()
if valID := atomic.LoadInt32(&snap.cache[cacheIndex]); valID > 0 {
// We've got a previous result, so return it. Note that we can only do this
// when we're not printing Info-level logs, because this avoids the usual
// logging of rule evaluation.
return snap.valueForID(valID)
}
return snap.valueFromDetails(id, key)
}
func (snap *Snapshot) initCache() {
snap.makeCache.Do(func() {
snap.cache = make([]valueID, snap.config.keysWithRules)
})
}
func (snap *Snapshot) valueFromDetails(id keyID, key string) interface{} {
if value, _, _, _, err := snap.details(id, key); err == nil {
return value
}
return nil
}
func (snap *Snapshot) details(id keyID, key string) (interface{}, string, *TargetingRule, *PercentageOption, error) {
if snap == nil {
return nil, "", nil, nil, errors.New("snapshot is nil")
}
if snap.evaluators == nil {
err := ErrConfigJsonMissing{Key: key}
snap.logger.Errorf(1000, err.Error())
return nil, "", nil, nil, err
}
var eval settingEvalFunc
if int(id) < len(snap.evaluators) {
eval = snap.evaluators[id]
}
if eval == nil {
err := ErrKeyNotFound{Key: key, AvailableKeys: snap.GetAllKeys()}
snap.logger.Errorf(1001, err.Error())
return nil, "", nil, nil, err
}
var builder *evalLogBuilder
if snap.logger.enabled(LogLevelInfo) {
builder = &evalLogBuilder{user: snap.originalUser}
}
valID, varID, targeting, percentage, err := eval(id, snap.user, snap.userTypeInfo, builder, snap.logger)
if err != nil {
snap.logger.Errorf(1002, "failed to evaluate setting '%s' (%s)", key, err.Error())
return nil, "", nil, nil, err
}
val := snap.valueForID(valID)
if snap.logger.enabled(LogLevelInfo) && builder != nil {
snap.logger.Infof(5000, builder.builder.String())
}
if v := snap.valueIds[id]; v < 0 {
snap.initCache()
cacheIndex := -v - 1
atomic.StoreInt32(&snap.cache[cacheIndex], valID)
}
if snap.hooks != nil && snap.hooks.OnFlagEvaluated != nil {
go snap.hooks.OnFlagEvaluated(&EvaluationDetails{
Value: val,
Data: EvaluationDetailsData{
Key: key,
VariationID: varID,
User: snap.originalUser,
FetchTime: snap.FetchTime(),
MatchedTargetingRule: targeting,
MatchedPercentageOption: percentage,
},
})
}
return val, varID, targeting, percentage, nil
}
func (snap *Snapshot) evalDetailsForKeyId(id keyID, key string, defaultValue interface{}) EvaluationDetails {
if snap == nil {
return EvaluationDetails{}
}
value, varID, targeting, percentage, err := snap.details(id, key)
if err != nil {
return EvaluationDetails{Value: defaultValue, Data: EvaluationDetailsData{
Key: key,
User: snap.originalUser,
IsDefaultValue: true,
Error: err,
FetchTime: snap.FetchTime(),
}}
}
return EvaluationDetails{Value: value, Data: EvaluationDetailsData{
Key: key,
VariationID: varID,
User: snap.originalUser,
FetchTime: snap.FetchTime(),
MatchedTargetingRule: targeting,
MatchedPercentageOption: percentage,
}}
}
// valueForID returns the actual value corresponding to
// the given value ID.
func (snap *Snapshot) valueForID(id valueID) interface{} {
return snap.values[id-1]
}
// GetValue returns a feature flag value regardless of type. If there is no
// value found, it returns nil; otherwise the returned value
// has one of the dynamic types bool, int, float64, or string.
//
// To use obtain the value of a typed feature flag, use
// one of the typed feature flag functions. For example:
//
// someFlag := configcat.Bool("someFlag", false)
// value := someFlag.Get(snap)
func (snap *Snapshot) GetValue(key string) interface{} {
return snap.value(idForKey(key, false), key)
}
// GetValueDetails returns the value and evaluation details of a feature flag or setting
// with respect to the current user, or nil if none is found.
func (snap *Snapshot) GetValueDetails(key string) EvaluationDetails {
return snap.evalDetailsForKeyId(idForKey(key, false), key, nil)
}
// GetAllValueDetails returns values along with evaluation details of all feature flags and settings.
func (snap *Snapshot) GetAllValueDetails() []EvaluationDetails {
if snap == nil {
return nil
}
keys := snap.GetAllKeys()
details := make([]EvaluationDetails, 0, len(keys))
for _, key := range keys {
details = append(details, snap.GetValueDetails(key))
}
return details
}
// GetKeyValueForVariationID returns the key and value that
// are associated with the given variation ID. If the
// variation ID isn't found, it returns "", nil.
func (snap *Snapshot) GetKeyValueForVariationID(id string) (string, interface{}) {
if snap == nil {
return "", nil
}
key, value := snap.config.getKeyAndValueForVariation(id)
if key == "" {
snap.logger.Errorf(2011, "could not find the setting for the specified variation ID: '%s'; returning nil", id)
return "", nil
}
return key, value
}
// GetAllKeys returns all the known keys in arbitrary order.
func (snap *Snapshot) GetAllKeys() []string {
if snap == nil {
return nil
}
return snap.allKeys
}
// GetAllValues returns all keys and values in freshly allocated key-value map.
func (snap *Snapshot) GetAllValues() map[string]interface{} {
keys := snap.GetAllKeys()
values := make(map[string]interface{}, len(keys))
for _, key := range keys {
values[key] = snap.GetValue(key)
}
return values
}
func (snap *Snapshot) FetchTime() time.Time {
if snap == nil || snap.config == nil {
return time.Time{}
}
return snap.config.fetchTime
}