Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ups): add support for multiple clients #405

Merged
merged 24 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions OptimizelySwiftSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@
6E424D5326324C4D0081004A /* OptimizelyUserContext+ObjC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E86CEA124FDC836005DAFED /* OptimizelyUserContext+ObjC.swift */; };
6E424D5426324C4D0081004A /* OptimizelyUserContext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EC6DD4024ABF89B0017D296 /* OptimizelyUserContext.swift */; };
6E424D7626324DBD0081004A /* AtomicArrayTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E424D7526324DBD0081004A /* AtomicArrayTests.swift */; };
6E474C8D263C889E00ABDFF8 /* UserProfileServiceTests_MultiClients.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E474C8C263C889E00ABDFF8 /* UserProfileServiceTests_MultiClients.swift */; };
6E593FB625BB9C5500EC72BC /* OptimizelyClientTests_Decide.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E593FB425BB9C5500EC72BC /* OptimizelyClientTests_Decide.swift */; };
6E5AB69323F6130D007A82B1 /* OptimizelyClientTests_Init_Sync.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E5AB69123F6130C007A82B1 /* OptimizelyClientTests_Init_Sync.swift */; };
6E5AB69423F6130D007A82B1 /* OptimizelyClientTests_Init_Async.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E5AB69223F6130D007A82B1 /* OptimizelyClientTests_Init_Async.swift */; };
Expand Down Expand Up @@ -1329,7 +1330,6 @@
6E9B115822C5486E00C22D81 /* EventDispatcherTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E75199222C5211100B2B157 /* EventDispatcherTests.swift */; };
6E9B115922C5486E00C22D81 /* BatchEventBuilderTests_Attributes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E75199322C5211100B2B157 /* BatchEventBuilderTests_Attributes.swift */; };
6E9B115A22C5486E00C22D81 /* DecisionServiceTests_Others.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E75199422C5211100B2B157 /* DecisionServiceTests_Others.swift */; };
6E9B115B22C5486E00C22D81 /* DecisionServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E75199522C5211100B2B157 /* DecisionServiceTests.swift */; };
6E9B115C22C5486E00C22D81 /* DatafileHandlerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E75199622C5211100B2B157 /* DatafileHandlerTests.swift */; };
6E9B115D22C5486E00C22D81 /* BatchEventBuilderTests_EventTags.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E75199722C5211100B2B157 /* BatchEventBuilderTests_EventTags.swift */; };
6E9B115E22C5486E00C22D81 /* DataStoreTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E75199822C5211100B2B157 /* DataStoreTests.swift */; };
Expand All @@ -1354,7 +1354,6 @@
6E9B117222C5487100C22D81 /* EventDispatcherTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E75199222C5211100B2B157 /* EventDispatcherTests.swift */; };
6E9B117322C5487100C22D81 /* BatchEventBuilderTests_Attributes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E75199322C5211100B2B157 /* BatchEventBuilderTests_Attributes.swift */; };
6E9B117422C5487100C22D81 /* DecisionServiceTests_Others.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E75199422C5211100B2B157 /* DecisionServiceTests_Others.swift */; };
6E9B117522C5487100C22D81 /* DecisionServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E75199522C5211100B2B157 /* DecisionServiceTests.swift */; };
6E9B117622C5487100C22D81 /* DatafileHandlerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E75199622C5211100B2B157 /* DatafileHandlerTests.swift */; };
6E9B117722C5487100C22D81 /* BatchEventBuilderTests_EventTags.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E75199722C5211100B2B157 /* BatchEventBuilderTests_EventTags.swift */; };
6E9B117822C5487100C22D81 /* DataStoreTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E75199822C5211100B2B157 /* DataStoreTests.swift */; };
Expand Down Expand Up @@ -1838,6 +1837,7 @@
6E424BFB263228FD0081004A /* AtomicDictionary.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AtomicDictionary.swift; sourceTree = "<group>"; };
6E424C3C263249620081004A /* OptimizelyTests-MultiClients-iOS.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "OptimizelyTests-MultiClients-iOS.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
6E424D7526324DBD0081004A /* AtomicArrayTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AtomicArrayTests.swift; sourceTree = "<group>"; };
6E474C8C263C889E00ABDFF8 /* UserProfileServiceTests_MultiClients.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserProfileServiceTests_MultiClients.swift; sourceTree = "<group>"; };
6E593FB425BB9C5500EC72BC /* OptimizelyClientTests_Decide.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OptimizelyClientTests_Decide.swift; sourceTree = "<group>"; };
6E5AB69123F6130C007A82B1 /* OptimizelyClientTests_Init_Sync.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OptimizelyClientTests_Init_Sync.swift; sourceTree = "<group>"; };
6E5AB69223F6130D007A82B1 /* OptimizelyClientTests_Init_Async.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OptimizelyClientTests_Init_Async.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1958,7 +1958,6 @@
6E75199222C5211100B2B157 /* EventDispatcherTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventDispatcherTests.swift; sourceTree = "<group>"; };
6E75199322C5211100B2B157 /* BatchEventBuilderTests_Attributes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BatchEventBuilderTests_Attributes.swift; sourceTree = "<group>"; };
6E75199422C5211100B2B157 /* DecisionServiceTests_Others.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DecisionServiceTests_Others.swift; sourceTree = "<group>"; };
6E75199522C5211100B2B157 /* DecisionServiceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DecisionServiceTests.swift; sourceTree = "<group>"; };
6E75199622C5211100B2B157 /* DatafileHandlerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatafileHandlerTests.swift; sourceTree = "<group>"; };
6E75199722C5211100B2B157 /* BatchEventBuilderTests_EventTags.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BatchEventBuilderTests_EventTags.swift; sourceTree = "<group>"; };
6E75199822C5211100B2B157 /* DataStoreTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataStoreTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2230,6 +2229,7 @@
6E424D7526324DBD0081004A /* AtomicArrayTests.swift */,
6E2D5DAD26338CA00002077F /* AtomicDictionaryTests.swift */,
6E5D120C2638DCE1000ABFC3 /* EventDispatcherTests_MultiClients.swift */,
6E474C8C263C889E00ABDFF8 /* UserProfileServiceTests_MultiClients.swift */,
);
path = "OptimizelyTests-MultiClients";
sourceTree = "<group>";
Expand Down Expand Up @@ -2517,7 +2517,6 @@
6E75199222C5211100B2B157 /* EventDispatcherTests.swift */,
6E75199322C5211100B2B157 /* BatchEventBuilderTests_Attributes.swift */,
6E75199422C5211100B2B157 /* DecisionServiceTests_Others.swift */,
6E75199522C5211100B2B157 /* DecisionServiceTests.swift */,
6E75199622C5211100B2B157 /* DatafileHandlerTests.swift */,
6E75199722C5211100B2B157 /* BatchEventBuilderTests_EventTags.swift */,
6E75199822C5211100B2B157 /* DataStoreTests.swift */,
Expand Down Expand Up @@ -3707,6 +3706,7 @@
6E424D0526324B620081004A /* ConditionHolder.swift in Sources */,
6E424D5226324C4D0081004A /* OptimizelyClient+Decide.swift in Sources */,
6E424D0626324B620081004A /* UserAttribute.swift in Sources */,
6E474C8D263C889E00ABDFF8 /* UserProfileServiceTests_MultiClients.swift in Sources */,
6E8A3D4A2637408500DAEA13 /* MockDatafileHandler.swift in Sources */,
6E424D0726324B620081004A /* Event.swift in Sources */,
6E424D0826324B620081004A /* ProjectConfig.swift in Sources */,
Expand Down Expand Up @@ -4125,7 +4125,6 @@
6E9B116E22C5487100C22D81 /* LoggerTests.swift in Sources */,
6E75180D22C520D400B2B157 /* DataStoreFile.swift in Sources */,
6E75178722C520D400B2B157 /* ArrayEventForDispatch+Extension.swift in Sources */,
6E9B117522C5487100C22D81 /* DecisionServiceTests.swift in Sources */,
6E75179F22C520D400B2B157 /* DataStoreQueueStackImpl+Extension.swift in Sources */,
6E7516BB22C520D400B2B157 /* DefaultUserProfileService.swift in Sources */,
6E424C08263228FD0081004A /* AtomicDictionary.swift in Sources */,
Expand Down Expand Up @@ -4348,7 +4347,6 @@
6E9B115422C5486E00C22D81 /* LoggerTests.swift in Sources */,
6E7518DF22C520D400B2B157 /* ConditionLeaf.swift in Sources */,
6E75172D22C520D400B2B157 /* Constants.swift in Sources */,
6E9B115B22C5486E00C22D81 /* DecisionServiceTests.swift in Sources */,
6E75172122C520D400B2B157 /* OptimizelyResult.swift in Sources */,
6E75186722C520D400B2B157 /* Rollout.swift in Sources */,
6E424C01263228FD0081004A /* AtomicDictionary.swift in Sources */,
Expand Down
16 changes: 7 additions & 9 deletions Sources/Extensions/OptimizelyClient+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,17 @@ extension OptimizelyClient {
datafileHandler: OPTDatafileHandler,
decisionService: OPTDecisionService,
notificationCenter: OPTNotificationCenter) {
// bind it as a non-singleton. so, we will create an instance anytime injected.
// we don't associate the logger with a sdkKey at this time because not all components are sdkKey specific.
let binder: Binder = Binder<OPTLogger>(service: OPTLogger.self, factory: type(of: logger).init)
// Register my logger service. Bind it as a non-singleton. So, we will create an instance anytime injected.
// we don't associate the logger with a sdkKey at this time because not all components are sdkKey specific.
HandlerRegistryService.shared.registerBinding(binder: Binder<OPTLogger>(service: OPTLogger.self, factory: type(of: logger).init))

// Register my logger service.
HandlerRegistryService.shared.registerBinding(binder: binder)

// this is bound a reusable singleton. so, if we re-initalize, we will keep this.
// This is bound a reusable singleton. so, if we re-initalize, we will keep this.
HandlerRegistryService.shared.registerBinding(binder: Binder<OPTNotificationCenter>(sdkKey: sdkKey, service: OPTNotificationCenter.self, strategy: .reUse, isSingleton: true, inst: notificationCenter))
// the decision service is also a singleton that will reCreate on re-initalize

// The decision service is also a singleton that will reCreate on re-initalize
HandlerRegistryService.shared.registerBinding(binder: Binder<OPTDecisionService>(sdkKey: sdkKey, service: OPTDecisionService.self, strategy: .reUse, isSingleton: true, inst: decisionService))

// An event dispatcher. We use a singleton and use the same Event dispatcher for all
// An event dispatcher. We use a singleton and use the same Event dispatcher for all
// projects. If you change the event dispatcher, you can potentially lose data if you
// don't use the same backingstore.
HandlerRegistryService.shared.registerBinding(binder: Binder<OPTEventDispatcher>(sdkKey: sdkKey, service: OPTEventDispatcher.self, strategy: .reUse, isSingleton: true, inst: eventDispatcher))
Expand Down
27 changes: 16 additions & 11 deletions Sources/Implementation/DefaultDecisionService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class DefaultDecisionService: OPTDecisionService {
let userProfileService: OPTUserProfileService
lazy var logger = OPTLoggerFactory.getLogger()

// user-profile-service read-modify-write lock for supporting multiple clients
static let upsRMWLock = DispatchQueue(label: "ups-rmw")
jaeopt marked this conversation as resolved.
Show resolved Hide resolved

init(userProfileService: OPTUserProfileService) {
self.bucketer = DefaultBucketer()
self.userProfileService = userProfileService
Expand Down Expand Up @@ -388,17 +391,19 @@ extension DefaultDecisionService {
func saveProfile(userId: String,
experimentId: String,
variationId: String) {
var profile = userProfileService.lookup(userId: userId) ?? OPTUserProfileService.UPProfile()

var bucketMap = profile[UserProfileKeys.kBucketMap] as? OPTUserProfileService.UPBucketMap ?? OPTUserProfileService.UPBucketMap()
bucketMap[experimentId] = [UserProfileKeys.kVariationId: variationId]

profile[UserProfileKeys.kBucketMap] = bucketMap
profile[UserProfileKeys.kUserId] = userId

userProfileService.save(userProfile: profile)

logger.i(.savedVariationInUserProfile(variationId, experimentId, userId))
DefaultDecisionService.upsRMWLock.sync {
var profile = self.userProfileService.lookup(userId: userId) ?? OPTUserProfileService.UPProfile()

var bucketMap = profile[UserProfileKeys.kBucketMap] as? OPTUserProfileService.UPBucketMap ?? OPTUserProfileService.UPBucketMap()
bucketMap[experimentId] = [UserProfileKeys.kVariationId: variationId]

profile[UserProfileKeys.kBucketMap] = bucketMap
profile[UserProfileKeys.kUserId] = userId

self.userProfileService.save(userProfile: profile)

self.logger.i(.savedVariationInUserProfile(variationId, experimentId, userId))
}
}

}
10 changes: 5 additions & 5 deletions Sources/Optimizely/OptimizelyClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ open class OptimizelyClient: NSObject {
return HandlerRegistryService.shared.injectEventDispatcher(sdkKey: self.sdkKey)
}

public var datafileHandler: OPTDatafileHandler? {
return HandlerRegistryService.shared.injectDatafileHandler(sdkKey: self.sdkKey)
}
lazy var currentDatafileHandler = datafileHandler

// MARK: - Default Services

var decisionService: OPTDecisionService {
return HandlerRegistryService.shared.injectDecisionService(sdkKey: self.sdkKey)!
}

public var datafileHandler: OPTDatafileHandler? {
return HandlerRegistryService.shared.injectDatafileHandler(sdkKey: self.sdkKey)
}
lazy var currentDatafileHandler = datafileHandler

public var notificationCenter: OPTNotificationCenter? {
return HandlerRegistryService.shared.injectNotificationCenter(sdkKey: self.sdkKey)
}
Expand Down
11 changes: 3 additions & 8 deletions Sources/Utils/HandlerRegistryService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,12 @@ class HandlerRegistryService {
}

var binders: AtomicProperty<[ServiceKey: BinderProtocol]> = {

var binders = AtomicProperty<[ServiceKey: BinderProtocol]>()
binders.property = [ServiceKey: BinderProtocol]()
return binders
}()

private init() {

}
private init() {}

func registerBinding(binder: BinderProtocol) {
let sk = ServiceKey(service: "\(type(of: binder.service))", sdkKey: binder.sdkKey)
Expand Down Expand Up @@ -105,17 +102,15 @@ protocol BinderProtocol {
var strategy: ReInitializeStrategy { get }
var service: Any { get }
var isSingleton: Bool { get }
var factory:()->Any? { get }
// var configure:(_ inst:Any?)->Any? { get }
var factory: () -> Any? { get }
var instance: Any? { get set }

}
struct Binder<T>: BinderProtocol {
var sdkKey: String?
var service: Any
var strategy: ReInitializeStrategy = .reCreate
var factory: (() -> Any?) = { ()->Any? in { return nil as Any? }}
// var configure: ((Any?) -> Any?) = { (_)->Any? in { return nil as Any? }}
var factory: () -> Any? = { return nil as Any? }
var isSingleton = false
var inst: T?

Expand Down
Loading