-
Notifications
You must be signed in to change notification settings - Fork 19
/
factory.go
273 lines (236 loc) · 9.9 KB
/
factory.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
/****************************************************************************
* Copyright 2019-2020, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/
// Package client has client facing factories
package client
import (
"context"
"errors"
"time"
"github.com/optimizely/go-sdk/pkg/config"
"github.com/optimizely/go-sdk/pkg/decide"
"github.com/optimizely/go-sdk/pkg/decision"
"github.com/optimizely/go-sdk/pkg/event"
"github.com/optimizely/go-sdk/pkg/logging"
"github.com/optimizely/go-sdk/pkg/metrics"
"github.com/optimizely/go-sdk/pkg/registry"
"github.com/optimizely/go-sdk/pkg/utils"
)
// OptimizelyFactory is used to customize and construct an instance of the OptimizelyClient.
type OptimizelyFactory struct {
SDKKey string
Datafile []byte
DatafileAccessToken string
configManager config.ProjectConfigManager
ctx context.Context
decisionService decision.Service
defaultDecideOptions *decide.Options
eventDispatcher event.Dispatcher
eventProcessor event.Processor
userProfileService decision.UserProfileService
overrideStore decision.ExperimentOverrideStore
metricsRegistry metrics.Registry
}
// OptionFunc is used to provide custom client configuration to the OptimizelyFactory.
type OptionFunc func(*OptimizelyFactory)
// Client instantiates a new OptimizelyClient with the given options.
func (f *OptimizelyFactory) Client(clientOptions ...OptionFunc) (*OptimizelyClient, error) {
// extract options
for _, opt := range clientOptions {
opt(f)
}
if f.SDKKey == "" && f.Datafile == nil && f.configManager == nil {
return nil, errors.New("unable to instantiate client: no project config manager, SDK key, or a Datafile provided")
}
var metricsRegistry metrics.Registry
if f.metricsRegistry != nil {
metricsRegistry = f.metricsRegistry
} else {
metricsRegistry = metrics.NewNoopRegistry()
}
var ctx context.Context
if f.ctx != nil {
ctx = f.ctx
} else {
ctx = context.Background()
}
var decideOptions *decide.Options
if f.defaultDecideOptions != nil {
decideOptions = f.defaultDecideOptions
} else {
decideOptions = &decide.Options{}
}
eg := utils.NewExecGroup(ctx, logging.GetLogger(f.SDKKey, "ExecGroup"))
appClient := &OptimizelyClient{
defaultDecideOptions: decideOptions,
execGroup: eg,
notificationCenter: registry.GetNotificationCenter(f.SDKKey),
logger: logging.GetLogger(f.SDKKey, "OptimizelyClient"),
}
if f.configManager != nil {
appClient.ConfigManager = f.configManager
} else {
appClient.ConfigManager = config.NewPollingProjectConfigManager(
f.SDKKey,
config.WithInitialDatafile(f.Datafile),
config.WithDatafileAccessToken(f.DatafileAccessToken),
)
}
if f.eventProcessor != nil {
appClient.EventProcessor = f.eventProcessor
} else {
var eventProcessorOptions = []event.BPOptionConfig{
event.WithSDKKey(f.SDKKey),
}
if f.eventDispatcher != nil {
eventProcessorOptions = append(eventProcessorOptions, event.WithEventDispatcher(f.eventDispatcher))
}
eventProcessorOptions = append(eventProcessorOptions, event.WithEventDispatcherMetrics(metricsRegistry))
appClient.EventProcessor = event.NewBatchEventProcessor(eventProcessorOptions...)
}
if f.decisionService != nil {
appClient.DecisionService = f.decisionService
} else {
var experimentServiceOptions []decision.CESOptionFunc
if f.userProfileService != nil {
experimentServiceOptions = append(experimentServiceOptions, decision.WithUserProfileService(f.userProfileService))
}
if f.overrideStore != nil {
experimentServiceOptions = append(experimentServiceOptions, decision.WithOverrideStore(f.overrideStore))
}
compositeExperimentService := decision.NewCompositeExperimentService(f.SDKKey, experimentServiceOptions...)
compositeService := decision.NewCompositeService(f.SDKKey, decision.WithCompositeExperimentService(compositeExperimentService))
appClient.DecisionService = compositeService
}
// Initialize the default services with the execution context
if pollingConfigManager, ok := appClient.ConfigManager.(*config.PollingProjectConfigManager); ok {
eg.Go(pollingConfigManager.Start)
}
if batchProcessor, ok := appClient.EventProcessor.(*event.BatchEventProcessor); ok {
eg.Go(batchProcessor.Start)
}
return appClient, nil
}
// WithDatafileAccessToken sets authenticated datafile token
func WithDatafileAccessToken(datafileAccessToken string) OptionFunc {
return func(f *OptimizelyFactory) {
f.DatafileAccessToken = datafileAccessToken
}
}
// WithPollingConfigManager sets polling config manager on a client.
func WithPollingConfigManager(pollingInterval time.Duration, initDataFile []byte) OptionFunc {
return func(f *OptimizelyFactory) {
f.configManager = config.NewPollingProjectConfigManager(f.SDKKey, config.WithInitialDatafile(initDataFile),
config.WithPollingInterval(pollingInterval))
}
}
// WithPollingConfigManagerDatafileAccessToken sets polling config manager with auth datafile token on a client
func WithPollingConfigManagerDatafileAccessToken(pollingInterval time.Duration, initDataFile []byte, datafileAccessToken string) OptionFunc {
return func(f *OptimizelyFactory) {
f.configManager = config.NewPollingProjectConfigManager(f.SDKKey, config.WithInitialDatafile(initDataFile),
config.WithPollingInterval(pollingInterval), config.WithDatafileAccessToken(datafileAccessToken))
}
}
// WithConfigManager sets polling config manager on a client.
func WithConfigManager(configManager config.ProjectConfigManager) OptionFunc {
return func(f *OptimizelyFactory) {
f.configManager = configManager
}
}
// WithDecisionService sets decision service on a client.
func WithDecisionService(decisionService decision.Service) OptionFunc {
return func(f *OptimizelyFactory) {
f.decisionService = decisionService
}
}
// WithDefaultDecideOptions sets default decide options on a client.
func WithDefaultDecideOptions(decideOptions []decide.OptimizelyDecideOptions) OptionFunc {
return func(f *OptimizelyFactory) {
f.defaultDecideOptions = convertDecideOptions(decideOptions)
}
}
// WithUserProfileService sets the user profile service on the decision service.
func WithUserProfileService(userProfileService decision.UserProfileService) OptionFunc {
return func(f *OptimizelyFactory) {
f.userProfileService = userProfileService
}
}
// WithExperimentOverrides sets the experiment override store on the decision service.
func WithExperimentOverrides(overrideStore decision.ExperimentOverrideStore) OptionFunc {
return func(f *OptimizelyFactory) {
f.overrideStore = overrideStore
}
}
// WithBatchEventProcessor sets event processor on a client.
func WithBatchEventProcessor(batchSize, queueSize int, flushInterval time.Duration) OptionFunc {
return func(f *OptimizelyFactory) {
f.eventProcessor = event.NewBatchEventProcessor(event.WithBatchSize(batchSize),
event.WithQueueSize(queueSize), event.WithFlushInterval(flushInterval))
}
}
// WithEventProcessor sets event processor on a client
func WithEventProcessor(eventProcessor event.Processor) OptionFunc {
return func(f *OptimizelyFactory) {
f.eventProcessor = eventProcessor
}
}
// WithEventDispatcher sets event dispatcher on the factory.
func WithEventDispatcher(eventDispatcher event.Dispatcher) OptionFunc {
return func(f *OptimizelyFactory) {
f.eventDispatcher = eventDispatcher
}
}
// WithContext allows user to pass in their own context to override the default one in the client.
func WithContext(ctx context.Context) OptionFunc {
return func(f *OptimizelyFactory) {
f.ctx = ctx
}
}
// WithMetricsRegistry allows user to pass in their own implementation of a metrics collector
func WithMetricsRegistry(metricsRegistry metrics.Registry) OptionFunc {
return func(f *OptimizelyFactory) {
f.metricsRegistry = metricsRegistry
}
}
// StaticClient returns a client initialized with a static project config.
func (f *OptimizelyFactory) StaticClient() (optlyClient *OptimizelyClient, err error) {
staticManager := config.NewStaticProjectConfigManagerWithOptions(f.SDKKey, config.WithInitialDatafile(f.Datafile), config.WithDatafileAccessToken(f.DatafileAccessToken))
if staticManager == nil {
return nil, errors.New("unable to initiate config manager")
}
optlyClient, err = f.Client(
WithConfigManager(staticManager),
WithBatchEventProcessor(event.DefaultBatchSize, event.DefaultEventQueueSize, event.DefaultEventFlushInterval),
)
return optlyClient, err
}
func convertDecideOptions(options []decide.OptimizelyDecideOptions) *decide.Options {
finalOptions := decide.Options{}
for _, option := range options {
switch option {
case decide.DisableDecisionEvent:
finalOptions.DisableDecisionEvent = true
case decide.EnabledFlagsOnly:
finalOptions.EnabledFlagsOnly = true
case decide.IgnoreUserProfileService:
finalOptions.IgnoreUserProfileService = true
case decide.IncludeReasons:
finalOptions.IncludeReasons = true
case decide.ExcludeVariables:
finalOptions.ExcludeVariables = true
}
}
return &finalOptions
}