-
Notifications
You must be signed in to change notification settings - Fork 0
/
fbclient.go
553 lines (519 loc) · 23.2 KB
/
fbclient.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
package featbit
import (
"encoding/json"
"fmt"
"github.com/featbit/featbit-go-sdk/factories"
. "github.com/featbit/featbit-go-sdk/interfaces"
"github.com/featbit/featbit-go-sdk/internal"
"github.com/featbit/featbit-go-sdk/internal/datasynchronization"
"github.com/featbit/featbit-go-sdk/internal/dataupdating"
"github.com/featbit/featbit-go-sdk/internal/types/data"
"github.com/featbit/featbit-go-sdk/internal/types/insight"
"github.com/featbit/featbit-go-sdk/internal/util"
"github.com/featbit/featbit-go-sdk/internal/util/log"
"sync"
"time"
)
type FBClient struct {
offline bool
dataStorage DataStorage
dataSynchronizer DataSynchronizer
dataUpdater DataUpdater
dataUpdateStatusProvider DataUpdateStatusProvider
insightProcessor InsightProcessor
evaluator *evaluator
getFlag func(key string) *data.FeatureFlag
sendEvent func(Event)
}
var (
envSecretInvalid = fmt.Errorf("invalid env secret")
hostInvalid = fmt.Errorf("invalid streaming url or event url")
initializationTimeout = fmt.Errorf("timeout encountered waiting for client initialization")
initializationFailed = fmt.Errorf("client initialization failed")
clientNotInitialized = fmt.Errorf("evaluation is called before client is initialized")
emptyClient = fmt.Errorf("empty client, please call constructor")
flagNotFound = fmt.Errorf("feature flag not found")
userInvalid = fmt.Errorf("invalid user")
evalFailed = fmt.Errorf("evaluation failed")
evalWrongType = fmt.Errorf("flag type doesn't match the request")
)
// NewFBClient creates a new client instance that connects to your feature flag center with the default configuration.
// For advanced configuration options, use MakeCustomFBClient. Calling NewFBClient is exactly equivalent to
// calling MakeCustomClient with the config parameter set to a default value.
//
// Unless it is configured to be offline with FBConfig.Offline, the client will begin attempting to connect to feature flag center as soon as you call this constructor.
//The constructor will return when it successfully connects, or when the timeout set by the FBConfig.StartWait parameter expires, whichever comes first.
//
// If the timeout(15s) elapsed without a successful connection, it still returns a client instance-- in an initializing state,
// where feature flags will return default values-- and the error value is initializationTimeout. In this case, it will still continue trying to connect in the background.
//
// If there was an unexpected error such that it cannot succeed by retrying-- for instance, the envSecret key is
// invalid or an DNS error-- it will return a client instance in an uninitialized state, and the error value is initializationFailed.
//
// The way to monitor the client's status, use FBClient.IsInitialized or FBClient.GetDataUpdateStatusProvider.
//
// client, _ := featbit.NewFBClient(envSecret, streamingUrl, eventUrl)
//
// if !client.IsInitialized() {
// // do whatever is appropriate if initialization has timed out
// }
//
// If you set FBConfig.StartWait to zero, the function will return immediately after creating the client instance, and do any further initialization in the background.
//
// client, _ := featbit.MakeCustomFBClient(envSecret, streamingUrl, eventUrl, config)
//
// // later...
// ok := client.GetDataSourceStatusProvider().WaitForOKState(10 * time.Second)
// if !ok {
// // do whatever is appropriate if initialization has timed out
// }
//
// The only time it returns nil instead of a client instance is if the client cannot be created at all due to
// an invalid configuration. This is rare, but could happen if for example you specified a custom TLS
// certificate file that did not load a valid certificate, you inputted an invalid env secret key, etc...
func NewFBClient(envSecret string, streamingUrl string, eventUrl string) (*FBClient, error) {
config := DefaultFBConfig
return MakeCustomFBClient(envSecret, streamingUrl, eventUrl, *config)
}
// MakeCustomFBClient creates a new client instance that connects to your feature flag center with the custom configuration.
//
// The FBConfig allows customization of all SDK properties; some of these are represented directly as
// fields in FBConfig, while others are set by builder methods on a more specific configuration object. See FBConfig for details.
//
// Unless it is configured to be offline with FBConfig.Offline, the client will begin attempting to connect to feature flag center as soon as you call this constructor.
//The constructor will return when it successfully connects, or when the timeout set by the FBConfig.StartWait parameter expires, whichever comes first.
//
// If the timeout(15s) elapsed without a successful connection, it still returns a client instance-- in an initializing state,
// where feature flags will return default values-- and the error value is initializationTimeout. In this case, it will still continue trying to connect in the background.
//
// If there was an unexpected error such that it cannot succeed by retrying-- for instance, the envSecret key is
// invalid or an DNS error-- it will return a client instance in an uninitialized state, and the error value is initializationFailed.
//
// The way to monitor the client's status, use FBClient.IsInitialized or FBClient.GetDataUpdateStatusProvider.
//
// client, _ := featbit.MakeCustomFBClient(envSecret, streamingUrl, eventUrl, config)
//
// if !client.IsInitialized() {
// // do whatever is appropriate if initialization has timed out
// }
//
// If you set FBConfig.StartWait to zero, the function will return immediately after creating the client instance, and do any further initialization in the background.
//
// client, _ := featbit.MakeCustomFBClient(envSecret, streamingUrl, eventUrl, config)
//
// // later...
// ok := client.GetDataSourceStatusProvider().WaitForOKState(10 * time.Second)
// if !ok {
// // do whatever is appropriate if initialization has timed out
// }
//
// The only time it returns nil instead of a client instance is if the client cannot be created at all due to
// an invalid configuration. This is rare, but could happen if for example you specified a custom TLS
// certificate file that did not load a valid certificate, you inputted an invalid env secret key, etc...
func MakeCustomFBClient(envSecret string, streamingUrl string, eventUrl string, config FBConfig) (*FBClient, error) {
logger := &log.SimpleLogger{Level: config.LogLevel}
log.SetLogger(logger)
if !config.Offline {
if !util.IsEnvSecretValid(envSecret) {
return nil, envSecretInvalid
} else if !util.IsUrl(streamingUrl) || !util.IsUrl(eventUrl) {
return nil, hostInvalid
}
} else {
log.LogInfo("FB GO SDK: SDK is in offline mode")
}
networkFactory := config.NetworkFactory
if networkFactory == nil {
networkFactory = factories.NewNetworkBuilder()
}
ctx, err := internal.FromConfig(envSecret, streamingUrl, eventUrl, networkFactory)
if err != nil {
return nil, err
}
client := &FBClient{offline: config.Offline}
// init components
// data storage
dataStorageFactory := config.DataStorageFactory
if dataStorageFactory == nil {
dataStorageFactory = factories.NewInMemoryStorageBuilder()
}
client.dataStorage, err = dataStorageFactory.CreateDataStorage(ctx)
if err != nil {
return nil, err
}
//evaluator
client.getFlag = func(key string) *data.FeatureFlag {
if item, e := client.dataStorage.Get(data.Features, key); e == nil {
if flag, ok := item.(*data.FeatureFlag); ok {
return flag
}
}
return nil
}
getSegment := func(key string) *data.Segment {
if item, e := client.dataStorage.Get(data.Segments, key); e == nil {
if segment, ok := item.(*data.Segment); ok {
return segment
}
}
return nil
}
client.evaluator = newEvaluator(client.getFlag, getSegment)
// data updater
dataUpdater := dataupdating.NewDataUpdaterImpl(client.dataStorage)
client.dataUpdater = dataUpdater
// data update status provider
client.dataUpdateStatusProvider = dataupdating.NewDataUpdateStatusProviderImpl(dataUpdater)
// run insight processor
insightProcessorFactory := config.InsightProcessorFactory
if client.offline {
insightProcessorFactory = factories.ExternalEventTrack()
} else if insightProcessorFactory == nil {
insightProcessorFactory = factories.NewInsightProcessorBuilder()
}
client.insightProcessor, err = insightProcessorFactory.CreateInsightProcessor(ctx)
if err != nil {
return nil, err
}
client.sendEvent = func(event Event) {
client.insightProcessor.Send(event)
}
// run data synchronizer
dataSynchronizerFactory := config.DataSynchronizerFactory
if client.offline {
dataSynchronizerFactory = factories.ExternalDataSynchronization()
} else if dataSynchronizerFactory == nil {
dataSynchronizerFactory = factories.NewStreamingBuilder()
}
client.dataSynchronizer, err = dataSynchronizerFactory.CreateDataSynchronizer(ctx, dataUpdater)
if err != nil {
return nil, err
}
ready := client.dataSynchronizer.Start()
if config.StartWait > 0 {
if _, ok := client.dataSynchronizer.(*datasynchronization.NullDataSynchronizer); !ok {
log.LogInfo("FB GO SDK: waiting for Client initialization in %d milliseconds", config.StartWait/time.Millisecond)
}
select {
case <-ready:
if !client.dataUpdater.StorageInitialized() && !config.Offline {
log.LogWarn("FB GO SDK: SDK just returns default variation because of no data found in the given environment")
}
if !client.dataSynchronizer.IsInitialized() {
log.LogWarn("FB GO SDK: SDK was not successfully initialized")
return client, initializationFailed
}
return client, nil
case <-time.After(config.StartWait):
log.LogWarn("FB GO SDK: timeout encountered when waiting for data update")
// it's rare, but prevent to block data synchronizer without waiting for termination of initialization
go func() { <-ready }()
return client, initializationTimeout
}
}
log.LogInfo("FB GO SDK: SDK starts in asynchronous mode")
go func() { <-ready }()
return client, nil
}
// IsInitialized tests whether the client is ready to be used.
// return true if the client is ready, or false if it is still initializing.
//
// If this value is true, it means the FBClient has succeeded at some point in connecting to feature flag center and
// has received feature flag data. It could still have encountered a connection problem after that point, so
// this does not guarantee that the flags are up-to-date; if you need to know its status in more Detail, use FBClient.GetDataUpdateStatusProvider.
//
// If this value is false, it means the client has not yet connected to feature flag center, or has permanently
// failed. In this state, feature flag evaluations will always return default values. You can use FBClient.GetDataUpdateStatusProvider
// to get current status of the client.
func (client *FBClient) IsInitialized() bool {
if client.dataSynchronizer == nil {
return false
}
return client.dataSynchronizer.IsInitialized()
}
// Close shuts down the FBClient. After calling this, the FBClient should no longer be used.
// The method will block until all pending events (if any) been sent.
func (client *FBClient) Close() error {
log.LogInfo("FB GO SDK: Java SDK client is closing")
if client.dataStorage != nil {
_ = client.dataStorage.Close()
}
if client.dataUpdateStatusProvider != nil {
_ = client.dataUpdateStatusProvider.Close()
}
if client.dataSynchronizer != nil {
_ = client.dataSynchronizer.Close()
}
if client.insightProcessor != nil {
_ = client.insightProcessor.Close()
}
return nil
}
// GetDataUpdateStatusProvider returns an interface for tracking the status of the interfaces.DataSynchronizer.
//
// The data synchronizer is the component that the SDK uses to get feature flags, segments such as a
// streaming connection. The interfaces.DataUpdateStatusProvider has methods
// for checking whether the interfaces.DataSynchronizer is currently operational and tracking changes in this status.
//
// The interfaces.DataUpdateStatusProvider is recommended to use when SDK starts in asynchronous mode
func (client *FBClient) GetDataUpdateStatusProvider() DataUpdateStatusProvider {
return client.dataUpdateStatusProvider
}
// IsFlagKnown returns true if feature flag is registered in the feature flag center,
// false if any error or flag is not existed
func (client *FBClient) IsFlagKnown(featureFlagKey string) bool {
if client.getFlag != nil {
return client.getFlag(featureFlagKey) != nil
}
return false
}
// Identify register a FBUser
func (client *FBClient) Identify(user FBUser) error {
if client.insightProcessor == nil {
return emptyClient
}
eventUser := insight.ConvertFBUserToEventUser(&user)
event := insight.NewUserEvent(eventUser)
client.sendEvent(event)
return nil
}
// TrackPercentageMetric reports that a user has performed an event, and associates it with a default value.
// This value is used by the experimentation feature in percentage custom metrics.
//
// The eventName normally corresponds to the event Name of a metric that you have created through the
// experiment dashboard in the feature flag center
func (client *FBClient) TrackPercentageMetric(user FBUser, eventName string) error {
return client.TrackNumericMetric(user, eventName, 1)
}
// TrackNumericMetric reports that a user has performed an event, and associates it with a metric value.
// This value is used by the experimentation feature in numeric custom metrics.
//
// The eventName normally corresponds to the event Name of a metric that you have created through the
// experiment dashboard in the feature flag center
func (client *FBClient) TrackNumericMetric(user FBUser, eventName string, metricValue float64) error {
if client.insightProcessor == nil {
return emptyClient
}
eventUser := insight.ConvertFBUserToEventUser(&user)
metric := insight.NewMetric(eventName, metricValue)
event := insight.NewMetricEvent(eventUser)
event.Add(metric)
client.sendEvent(event)
return nil
}
// TrackPercentageMetrics reports that a user tracks that a user performed a series of events with default values.
// These values are used by the experimentation feature in percentage custom metrics.
//
// The eventName normally corresponds to the event Name of a metric that you have created through the
// experiment dashboard in the feature flag center
func (client *FBClient) TrackPercentageMetrics(user FBUser, eventNames ...string) error {
if client.insightProcessor == nil {
return emptyClient
}
if len(eventNames) > 0 {
eventUser := insight.ConvertFBUserToEventUser(&user)
event := insight.NewMetricEvent(eventUser)
for _, eventName := range eventNames {
metric := insight.NewMetric(eventName, 1)
event.Add(metric)
}
client.sendEvent(event)
}
return nil
}
// TrackNumericMetrics reports that a user tracks that a user performed a series of events with metric values.
// These values are used by the experimentation feature in numeric custom metrics.
//
// The eventName normally corresponds to the event Name of a metric that you have created through the
// experiment dashboard in the feature flag center
func (client *FBClient) TrackNumericMetrics(user FBUser, metrics map[string]float64) error {
if client.insightProcessor == nil {
return emptyClient
}
if len(metrics) > 0 {
eventUser := insight.ConvertFBUserToEventUser(&user)
event := insight.NewMetricEvent(eventUser)
for eventName, metricValue := range metrics {
metric := insight.NewMetric(eventName, metricValue)
event.Add(metric)
}
client.sendEvent(event)
}
return nil
}
// Flush tells the FBClient that all pending events (if any) should be delivered as soon as possible.
// Flushing is asynchronous, so this method will return before it is complete.
// However, if you call Close(), events are guaranteed to be sent before that method returns.
func (client *FBClient) Flush() error {
if client.insightProcessor == nil {
return emptyClient
}
client.insightProcessor.Flush()
return nil
}
// evaluateInternal internal use for evaluate flag value
func (client *FBClient) evaluateInternal(featureFlagKey string, user *FBUser, requiredType string) (*evalResult, error) {
if !client.IsInitialized() {
log.LogWarn("FB GO SDK: evaluation is called before GO SDK client is initialized for feature flag, well using the default value")
return errorResult(ReasonClientNotReady, featureFlagKey, FlagNameUnknown), clientNotInitialized
}
flag := client.getFlag(featureFlagKey)
if flag == nil {
log.LogWarn("FB Go SDK: unknown feature flag %v; returning default value", featureFlagKey)
return errorResult(ReasonFlagNotFound, featureFlagKey, FlagNameUnknown), flagNotFound
}
if !user.IsValid() {
log.LogWarn("FB GO SDK: invalid user for feature flag %v, returning default value", featureFlagKey)
return errorResult(ReasonUserNotSpecified, featureFlagKey, FlagNameUnknown), userInvalid
}
eventUser := insight.ConvertFBUserToEventUser(user)
event := insight.NewFlagEvent(eventUser)
er := client.evaluator.evaluate(flag, user, event)
if !er.checkType(requiredType) {
return errorResult(ReasonWrongType, featureFlagKey, er.name), evalWrongType
}
if er.success {
client.sendEvent(event)
return er, nil
}
log.LogError("FB GO SDK: unexpected error in evaluation")
return errorResult(ReasonError, featureFlagKey, flag.Name), evalFailed
}
func (client *FBClient) evaluateDetail(featureFlagKey string, user *FBUser, requiredType string, defaultValue interface{}) (EvalDetail, error) {
er, err := client.evaluateInternal(featureFlagKey, user, requiredType)
if err != nil {
return EvalDetail{Variation: defaultValue, Reason: er.reason, KeyName: er.keyName, Name: er.name}, err
}
return er.castVariationByFlagType(requiredType, defaultValue)
}
// Variation calculates the value of a feature flag for a given user,
// return a string variation for the given user, or defaultValue if the flag is disabled or an error occurs;
// the details that explains how the flag value is explained and the error if any.
//
// The method sends insight events back to feature flag center
func (client *FBClient) Variation(featureFlagKey string, user FBUser, defaultValue string) (string, EvalDetail, error) {
ed, err := client.evaluateDetail(featureFlagKey, &user, FlagStringType, defaultValue)
if err != nil {
return defaultValue, ed, err
}
ret, _ := ed.Variation.(string)
return ret, ed, nil
}
// BoolVariation calculates the value of a feature flag for a given user,
// return a bool variation for the given user, or defaultValue if the flag is disabled or an error occurs;
// the details that explains how the flag value is explained and the error if any.
//
// The method sends insight events back to feature flag center
func (client *FBClient) BoolVariation(featureFlagKey string, user FBUser, defaultValue bool) (bool, EvalDetail, error) {
ed, err := client.evaluateDetail(featureFlagKey, &user, FlagBoolType, defaultValue)
if err != nil {
return defaultValue, ed, err
}
ret, _ := ed.Variation.(bool)
return ret, ed, nil
}
// IntVariation calculates the value of a feature flag for a given user,
// return an int variation for the given user, or defaultValue if the flag is disabled or an error occurs;
// the details that explains how the flag value is explained and the error if any.
//
// The method sends insight events back to feature flag center
func (client *FBClient) IntVariation(featureFlagKey string, user FBUser, defaultValue int) (int, EvalDetail, error) {
ed, err := client.evaluateDetail(featureFlagKey, &user, FlagNumericType, defaultValue)
if err != nil {
return defaultValue, ed, err
}
ret, _ := ed.Variation.(int)
return ret, ed, nil
}
// DoubleVariation calculates the value of a feature flag for a given user,
// return a float variation for the given user, or defaultValue if the flag is disabled or an error occurs;
// the details that explains how the flag value is explained and the error if any.
//
// The method sends insight events back to feature flag center
func (client *FBClient) DoubleVariation(featureFlagKey string, user FBUser, defaultValue float64) (float64, EvalDetail, error) {
ed, err := client.evaluateDetail(featureFlagKey, &user, FlagNumericType, defaultValue)
if err != nil {
return defaultValue, ed, err
}
ret, _ := ed.Variation.(float64)
return ret, ed, nil
}
// JsonVariation calculates the value of a feature flag for a given user,
// return a json object variation for the given user, or defaultValue if the flag is disabled or an error occurs;
// the details that explains how the flag value is explained and the error if any.
//
// The method sends insight events back to feature flag center
func (client *FBClient) JsonVariation(featureFlagKey string, user FBUser, defaultValue interface{}) (interface{}, EvalDetail, error) {
ed, err := client.evaluateDetail(featureFlagKey, &user, FlagJsonType, defaultValue)
if err != nil {
return defaultValue, ed, err
}
return ed.Variation, ed, nil
}
// AllLatestFlagsVariations returns a list of all feature flags value with details for a given user, including the reason
// describes the way the value was determined.
//
// The return type AllFlagState could be used as a cache that provides the flag value to a client side sdk or a front-end app.
// See more details in AllFlagState.
//
// This method does not send insight events back to feature flag center. See interfaces.AllFlagState
func (client *FBClient) AllLatestFlagsVariations(user FBUser) (AllFlagState, error) {
if !client.IsInitialized() {
log.LogWarn("FB GO SDK: evaluation is called before GO SDK client is initialized for feature flag, well using the default value")
return &allFlagStateImpl{reason: ReasonClientNotReady}, clientNotInitialized
}
if !user.IsValid() {
log.LogWarn("FB GO SDK: invalid user")
return &allFlagStateImpl{reason: ReasonUserNotSpecified}, userInvalid
}
items, err := client.dataStorage.GetAll(data.Features)
if err != nil {
return &allFlagStateImpl{reason: ReasonError}, err
}
if len(items) == 0 {
return &allFlagStateImpl{reason: ReasonFlagNotFound}, flagNotFound
}
ret := &allFlagStateImpl{}
var once sync.Once
for key, item := range items {
if flag, ok := item.(*data.FeatureFlag); ok {
eventUser := insight.ConvertFBUserToEventUser(&user)
event := insight.NewFlagEvent(eventUser)
er := client.evaluator.evaluate(flag, &user, event)
if er.success {
once.Do(func() {
ret.success = true
ret.reason = "OK"
ret.states = make(map[string]map[evalResult]*insight.FlagEvent, len(items))
ret.sendEvent = client.sendEvent
})
ret.states[key] = map[evalResult]*insight.FlagEvent{*er: event}
}
}
}
if !ret.success {
log.LogError("FB GO SDK: unexpected error in evaluation")
ret.reason = ReasonError
return nil, evalFailed
}
return ret, nil
}
// InitializeFromExternalJson initializes FeatBit client in the offline mode
//
// Return false if the json can't be parsed or client is not in the offline mode
func (client *FBClient) InitializeFromExternalJson(jsonStr string) (bool, error) {
if client.offline && jsonStr != "" {
var all data.All
if err := json.Unmarshal([]byte(jsonStr), &all); err != nil {
return false, err
}
if all.IsProcessData() {
d := all.Data
if ok := client.dataUpdater.Init(d.ToStorageType(), d.GetTimestamp()); ok {
client.dataUpdater.UpdateStatus(OKState())
return true, nil
}
}
}
return false, nil
}