Skip to content

Commit cda1d9a

Browse files
author
Michael Ng
authored
feat(registry): Added service registry for retrieving instances of notif center (#120)
1 parent 0771aa1 commit cda1d9a

File tree

11 files changed

+105
-35
lines changed

11 files changed

+105
-35
lines changed

examples/benchmark/main.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/optimizely/go-sdk/optimizely/client"
1212
"github.com/optimizely/go-sdk/optimizely/decision"
1313
"github.com/optimizely/go-sdk/optimizely/entities"
14-
"github.com/optimizely/go-sdk/optimizely/notification"
1514

1615
"github.com/pkg/profile"
1716
)
@@ -41,8 +40,7 @@ func stressTest() {
4140
}
4241

4342
// Creates a default, canceleable context
44-
notificationCenter := notification.NewNotificationCenter()
45-
decisionService := decision.NewCompositeService(notificationCenter)
43+
decisionService := decision.NewCompositeService("sdk_key")
4644

4745
clientApp, err := optlyClient.Client(client.DecisionService(decisionService))
4846
if err != nil {

examples/main.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ import (
1111
"github.com/optimizely/go-sdk/optimizely/entities"
1212
"github.com/optimizely/go-sdk/optimizely/event"
1313
"github.com/optimizely/go-sdk/optimizely/logging"
14-
"github.com/optimizely/go-sdk/optimizely/notification"
1514
)
1615

1716
func main() {
18-
17+
sdkKey := "4SLpaJA1r1pgE6T2CoMs9q"
1918
logging.SetLogLevel(logging.LogLevelDebug)
2019
user := entities.UserContext{
2120
ID: "mike ng",
@@ -25,7 +24,7 @@ func main() {
2524
},
2625
}
2726
optimizelyFactory := &client.OptimizelyFactory{
28-
SDKKey: "4SLpaJA1r1pgE6T2CoMs9q",
27+
SDKKey: sdkKey,
2928
}
3029

3130
/************* StaticClient ********************/
@@ -46,7 +45,7 @@ func main() {
4645
/************* Client ********************/
4746

4847
optimizelyFactory = &client.OptimizelyFactory{
49-
SDKKey: "4SLpaJA1r1pgE6T2CoMs9q",
48+
SDKKey: sdkKey,
5049
}
5150

5251
optimizelyClient, err = optimizelyFactory.Client()
@@ -62,11 +61,9 @@ func main() {
6261

6362
/************* Setting Polling Interval ********************/
6463

65-
notificationCenter := notification.NewNotificationCenter()
66-
6764
optimizelyClient, _ = optimizelyFactory.Client(
68-
client.PollingConfigManager("4SLpaJA1r1pgE6T2CoMs9q", time.Second, nil, notificationCenter),
69-
client.CompositeDecisionService(notificationCenter),
65+
client.PollingConfigManager(sdkKey, time.Second, nil),
66+
client.CompositeDecisionService(sdkKey),
7067
client.BatchEventProcessor(event.DefaultBatchSize, event.DefaultEventQueueSize, event.DefaultEventFlushInterval),
7168
)
7269
optimizelyClient.Close()

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ go 1.12
55
require (
66
github.com/google/uuid v1.1.1
77
github.com/json-iterator/go v1.1.7
8+
github.com/pkg/profile v1.3.0
89
github.com/spf13/cobra v0.0.5
910
github.com/stretchr/testify v1.4.0
1011
github.com/twmb/murmur3 v1.0.0
1112
)
1213

13-
1414
// Work around issue wtih git.apache.org/thrift.git
1515
replace git.apache.org/thrift.git => github.com/apache/thrift v0.12.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJ
2424
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
2525
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
2626
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
27+
github.com/pkg/profile v1.3.0 h1:OQIvuDgm00gWVWGTf4m4mCt6W1/0YqU7Ntg0mySWgaI=
28+
github.com/pkg/profile v1.3.0/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA=
2729
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2830
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2931
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=

optimizely/client/factory.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/optimizely/go-sdk/optimizely/config"
2626
"github.com/optimizely/go-sdk/optimizely/decision"
2727
"github.com/optimizely/go-sdk/optimizely/event"
28-
"github.com/optimizely/go-sdk/optimizely/notification"
2928
"github.com/optimizely/go-sdk/optimizely/utils"
3029
)
3130

@@ -42,11 +41,10 @@ type OptionFunc func(*OptimizelyClient, utils.ExecutionCtx)
4241
func (f OptimizelyFactory) Client(clientOptions ...OptionFunc) (*OptimizelyClient, error) {
4342

4443
executionCtx := utils.NewCancelableExecutionCtx()
45-
notificationCenter := notification.NewNotificationCenter()
4644

4745
appClient := &OptimizelyClient{
4846
executionCtx: executionCtx,
49-
decisionService: decision.NewCompositeService(notificationCenter),
47+
decisionService: decision.NewCompositeService(f.SDKKey),
5048
eventProcessor: event.NewEventProcessor(executionCtx, event.DefaultBatchSize, event.DefaultEventQueueSize, event.DefaultEventFlushInterval),
5149
}
5250

@@ -61,17 +59,17 @@ func (f OptimizelyFactory) Client(clientOptions ...OptionFunc) (*OptimizelyClien
6159
if appClient.configManager == nil { // if it was not passed then assign here
6260

6361
appClient.configManager = config.NewPollingProjectConfigManager(executionCtx, f.SDKKey,
64-
config.InitialDatafile(f.Datafile), config.PollingInterval(config.DefaultPollingInterval), config.NotificationCenter(notificationCenter))
62+
config.InitialDatafile(f.Datafile), config.PollingInterval(config.DefaultPollingInterval))
6563
}
6664

6765
return appClient, nil
6866
}
6967

7068
// PollingConfigManager sets polling config manager on a client
71-
func PollingConfigManager(sdkKey string, pollingInterval time.Duration, initDataFile []byte, notificationCenter notification.Center) OptionFunc {
69+
func PollingConfigManager(sdkKey string, pollingInterval time.Duration, initDataFile []byte) OptionFunc {
7270
return func(f *OptimizelyClient, executionCtx utils.ExecutionCtx) {
7371
f.configManager = config.NewPollingProjectConfigManager(f.executionCtx, sdkKey, config.InitialDatafile(initDataFile),
74-
config.PollingInterval(pollingInterval), config.NotificationCenter(notificationCenter))
72+
config.PollingInterval(pollingInterval))
7573
}
7674
}
7775

@@ -83,9 +81,9 @@ func ConfigManager(configManager optimizely.ProjectConfigManager) OptionFunc {
8381
}
8482

8583
// CompositeDecisionService sets decision service on a client
86-
func CompositeDecisionService(notificationCenter notification.Center) OptionFunc {
84+
func CompositeDecisionService(sdkKey string) OptionFunc {
8785
return func(f *OptimizelyClient, executionCtx utils.ExecutionCtx) {
88-
f.decisionService = decision.NewCompositeService(notificationCenter)
86+
f.decisionService = decision.NewCompositeService(sdkKey)
8987
}
9088
}
9189

@@ -133,11 +131,9 @@ func (f OptimizelyFactory) StaticClient() (*OptimizelyClient, error) {
133131
configManager = staticConfigManager
134132
}
135133

136-
notificationCenter := notification.NewNotificationCenter()
137-
138134
optlyClient, e := f.Client(
139135
ConfigManager(configManager),
140-
CompositeDecisionService(notificationCenter),
136+
CompositeDecisionService(f.SDKKey),
141137
BatchEventProcessor(event.DefaultBatchSize, event.DefaultEventQueueSize, event.DefaultEventFlushInterval),
142138
)
143139
return optlyClient, e

optimizely/config/polling_manager.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/optimizely/go-sdk/optimizely/config/datafileprojectconfig"
2727
"github.com/optimizely/go-sdk/optimizely/logging"
2828
"github.com/optimizely/go-sdk/optimizely/notification"
29+
"github.com/optimizely/go-sdk/optimizely/registry"
2930
"github.com/optimizely/go-sdk/optimizely/utils"
3031
)
3132

@@ -78,13 +79,6 @@ func PollingInterval(interval time.Duration) OptionFunc {
7879
}
7980
}
8081

81-
// NotificationCenter is an optional function, sets a passed notification
82-
func NotificationCenter(notificationCenter notification.Center) OptionFunc {
83-
return func(p *PollingProjectConfigManager) {
84-
p.notificationCenter = notificationCenter
85-
}
86-
}
87-
8882
// InitialDatafile is an optional function, sets a passed datafile
8983
func InitialDatafile(datafile []byte) OptionFunc {
9084
return func(p *PollingProjectConfigManager) {
@@ -156,7 +150,12 @@ func (cm *PollingProjectConfigManager) start() {
156150
func NewPollingProjectConfigManager(exeCtx utils.ExecutionCtx, sdkKey string, pollingMangerOptions ...OptionFunc) *PollingProjectConfigManager {
157151
url := fmt.Sprintf(DatafileURLTemplate, sdkKey)
158152

159-
pollingProjectConfigManager := PollingProjectConfigManager{exeCtx: exeCtx, pollingInterval: DefaultPollingInterval, requester: utils.NewHTTPRequester(url)}
153+
pollingProjectConfigManager := PollingProjectConfigManager{
154+
exeCtx: exeCtx,
155+
notificationCenter: registry.GetNotificationCenter(sdkKey),
156+
pollingInterval: DefaultPollingInterval,
157+
requester: utils.NewHTTPRequester(url),
158+
}
160159

161160
for _, opt := range pollingMangerOptions {
162161
opt(&pollingProjectConfigManager)

optimizely/config/polling_manager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func TestNewPollingProjectConfigManagerOnDecision(t *testing.T) {
132132
sdkKey := "test_sdk_key"
133133

134134
exeCtx := utils.NewCancelableExecutionCtx()
135-
configManager := NewPollingProjectConfigManager(exeCtx, sdkKey, Requester(mockRequester), NotificationCenter(notification.NewNotificationCenter()))
135+
configManager := NewPollingProjectConfigManager(exeCtx, sdkKey, Requester(mockRequester))
136136

137137
var numberOfCalls = 0
138138
callback := func(notification notification.ProjectConfigUpdateNotification) {

optimizely/decision/composite_service.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/optimizely/go-sdk/optimizely/entities"
2424
"github.com/optimizely/go-sdk/optimizely/logging"
2525
"github.com/optimizely/go-sdk/optimizely/notification"
26+
"github.com/optimizely/go-sdk/optimizely/registry"
2627
)
2728

2829
var csLogger = logging.GetLogger("CompositeDecisionService")
@@ -35,14 +36,14 @@ type CompositeService struct {
3536
}
3637

3738
// NewCompositeService returns a new instance of the CompositeService with the defaults
38-
func NewCompositeService(notificationCenter notification.Center) *CompositeService {
39+
func NewCompositeService(sdkKey string) *CompositeService {
3940
// @TODO: add factory method with option funcs to accept custom feature and experiment services
4041
compositeFeatureDecisionService := NewCompositeFeatureService()
4142
compositeExperimentService := NewCompositeExperimentService()
4243
return &CompositeService{
4344
compositeExperimentService: compositeExperimentService,
4445
compositeFeatureService: compositeFeatureDecisionService,
45-
notificationCenter: notificationCenter,
46+
notificationCenter: registry.GetNotificationCenter(sdkKey),
4647
}
4748
}
4849

optimizely/decision/composite_service_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (s *CompositeServiceTestSuite) TestDecisionListeners() {
9292

9393
func (s *CompositeServiceTestSuite) TestNewCompositeService() {
9494
notificationCenter := notification.NewNotificationCenter()
95-
compositeService := NewCompositeService(notificationCenter)
95+
compositeService := NewCompositeService("sdk_key")
9696
s.Equal(notificationCenter, compositeService.notificationCenter)
9797
s.IsType(&CompositeExperimentService{}, compositeService.compositeExperimentService)
9898
s.IsType(&CompositeFeatureService{}, compositeService.compositeFeatureService)

optimizely/registry/service.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/****************************************************************************
2+
* Copyright 2019, Optimizely, Inc. and contributors *
3+
* *
4+
* Licensed under the Apache License, Version 2.0 (the "License"); *
5+
* you may not use this file except in compliance with the License. *
6+
* You may obtain a copy of the License at *
7+
* *
8+
* http://www.apache.org/licenses/LICENSE-2.0 *
9+
* *
10+
* Unless required by applicable law or agreed to in writing, software *
11+
* distributed under the License is distributed on an "AS IS" BASIS, *
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13+
* See the License for the specific language governing permissions and *
14+
* limitations under the License. *
15+
***************************************************************************/
16+
17+
// Package registry is the global access point for retrieving instances of services by SDK Key //
18+
package registry
19+
20+
import (
21+
"github.com/optimizely/go-sdk/optimizely/notification"
22+
)
23+
24+
var notificationCenterCache = make(map[string]notification.Center)
25+
26+
// GetNotificationCenter returns the notification center instance associated with the given SDK Key or creates a new one if not found
27+
func GetNotificationCenter(sdkKey string) notification.Center {
28+
var notificationCenter notification.Center
29+
var ok bool
30+
if notificationCenter, ok = notificationCenterCache[sdkKey]; !ok {
31+
notificationCenter = notification.NewNotificationCenter()
32+
notificationCenterCache[sdkKey] = notificationCenter
33+
}
34+
35+
return notificationCenter
36+
}

0 commit comments

Comments
 (0)