Skip to content

Commit

Permalink
[ch66864] Added startCompleteWhenFlagsReceivedTimeout (#98)
Browse files Browse the repository at this point in the history
* Added startCompleteWhenFlagsReceivedTimeout which allows a timeout in seconds to be given for the completion to be fired indicating that flags have been received

* Fixed doc typo, removed bad ms to second conversion

* Fix previous doc typo that got copied into objc

* Added serial queue to timeOutCheck to fix possible race condition

* Simplified unit tests by swapping if for assertion

* Simplified start timeout method name, fixed doc about timedOut return

* Moved DispatchQueue off of main

* Moved sync dispatchqueue from variable to func

* Changed labeled queue to global queue

* Added test for start timeout timing out, added doc note about start timeout and starting offline
  • Loading branch information
torchhound authored Jun 2, 2020
1 parent ec6e33e commit 4c65c8d
Show file tree
Hide file tree
Showing 3 changed files with 560 additions and 10 deletions.
39 changes: 38 additions & 1 deletion LaunchDarkly/LaunchDarkly/LDClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,44 @@ public class LDClient {
completion?()
}
}

/**
See [start](x-source-tag://start) for more information on starting the SDK.
This method listens for flag updates so the completion will only return once an update has occurred. If the SDK is configured to start offline the method will ignore the timeout and call the completion with True without awaiting a flag update.
- parameter config: The LDConfig that contains the desired configuration. (Required)
- parameter user: The LDUser set with the desired user. If omitted, LDClient retains the previously set user, or default if one was never set. (Optional)
- parameter startWaitSeconds: An Int representing how long to wait for flags before returning true in the completion to indicate that it timed out.
- parameter completion: Closure called when the embedded `setOnlineIdentify` call completes, subject to throttling delays. Takes a Bool as a parameter that indicates whether the SDK did not come online within startWaitSeconds. (Optional)
*/
public func startCompleteWhenFlagsReceived(config: LDConfig, user: LDUser? = nil, startWaitSeconds: Int, completion: ((_ timedOut: Bool) -> Void)? = nil) {
if !config.startOnline {
startCompleteWhenFlagsReceived(config: config, user: user)
completion?(timeOutCheck)
} else {
let startTime = Date().timeIntervalSince1970
startCompleteWhenFlagsReceived(config: config, user: user) {
if startTime + Double(startWaitSeconds) > Date().timeIntervalSince1970 {
self.internalTimeOutCheckQueue.sync {
self.timeOutCheck = false
completion?(self.timeOutCheck)
}
}
}
DispatchQueue.global().asyncAfter(deadline: .now() + DispatchTimeInterval.seconds(startWaitSeconds)) {
self.internalTimeOutCheckQueue.sync {
if self.timeOutCheck {
completion?(self.timeOutCheck)
}
}
}
}
}

private var timeOutCheck = true
private let internalTimeOutCheckQueue: DispatchQueue = DispatchQueue(label: "TimeOutQueue")

private func convertCachedData(skipDuringStart skip: Bool) {
guard !skip
else {
Expand Down Expand Up @@ -1009,7 +1046,7 @@ public class LDClient {
private(set) var flagCache: FeatureFlagCaching
private(set) var cacheConverter: CacheConverting
private(set) var flagSynchronizer: LDFlagSynchronizing
private(set) var flagChangeNotifier: FlagChangeNotifying
var flagChangeNotifier: FlagChangeNotifying
private(set) var eventReporter: EventReporting
private(set) var environmentReporter: EnvironmentReporting
private(set) var throttler: Throttling
Expand Down
14 changes: 14 additions & 0 deletions LaunchDarkly/LaunchDarkly/ObjectiveC/ObjcLDClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,20 @@ public final class ObjcLDClient: NSObject {
@objc public func startCompleteWhenFlagsReceived(config configWrapper: ObjcLDConfig, user userWrapper: ObjcLDUser? = nil, completion: (() -> Void)? = nil) {
LDClient.shared.startCompleteWhenFlagsReceived(config: configWrapper.config, user: userWrapper?.user, completion: completion)
}

/**
See [start](x-source-tag://start) for more information on starting the SDK.
This method listens for flag updates so the completion will only return once an update has occurred. If the SDK is configured to start offline the method will ignore the timeout and call the completion with True without awaiting a flag update.
- parameter configWrapper: The LDConfig that contains the desired configuration. (Required)
- parameter userWrapper: The LDUser set with the desired user. If omitted, LDClient retains the previously set user, or default if one was never set. (Optional)
- parameter startWaitSeconds: An Int representing how long to wait for flags before returning true in the completion to indicate that it timed out.
- parameter completion: Closure called when the embedded `setOnlineIdentify` call completes, subject to throttling delays. Takes a Bool as a parameter that indicates whether the SDK did not come online within startWaitSeconds. (Optional)
*/
@objc public func startCompleteWhenFlagsReceived(config configWrapper: ObjcLDConfig, user userWrapper: ObjcLDUser? = nil, startWaitSeconds: Int, completion: ((_ timedOut: Bool) -> Void)? = nil) {
LDClient.shared.startCompleteWhenFlagsReceived(config: configWrapper.config, user: userWrapper?.user, startWaitSeconds: startWaitSeconds, completion: completion)
}

/**
Stops the LDClient. Stopping the client means the LDClient goes offline and stops recording events. LDClient will no longer provide feature flag values, only returning fallback values.
Expand Down
Loading

0 comments on commit 4c65c8d

Please sign in to comment.