-
Notifications
You must be signed in to change notification settings - Fork 3
feat: demonstrate per-hook evaluation exposure deduplication #73
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
Open
abelonogov-ld
wants to merge
5
commits into
main
Choose a base branch
from
andrey/flag-exposure-dedupe
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8c66656
feat: demonstrate per-hook evaluation exposure deduplication
abelonogov-ld 6437dd1
chore: load mobile key from Secrets.xcconfig
abelonogov-ld df6a88b
Revert "chore: load mobile key from Secrets.xcconfig"
abelonogov-ld 3c85ab8
chore: drop the tracked-result cap from the dedupe demo
abelonogov-ld 4e10898
chore: wrap the demo hooks in DedupingHook
abelonogov-ld File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,30 @@ | ||
| use_frameworks! | ||
| target 'hello-ios' do | ||
| platform :ios, '13.0' | ||
| pod 'LaunchDarkly', '>= 11.2' | ||
| # Points at the evaluation-exposure-dedupe branch until that work is released. | ||
| # After release, switch back to: pod 'LaunchDarkly', '>= 11.x' | ||
| pod 'LaunchDarkly', :git => 'https://github.com/launchdarkly/ios-client-sdk.git', :branch => 'andrey/flag-exposure-dedupe' | ||
| end | ||
|
|
||
| target 'hello-watchOS Extension' do | ||
| platform :watchos, '6.0' | ||
| pod 'LaunchDarkly', '>= 11.2' | ||
| pod 'LaunchDarkly', :git => 'https://github.com/launchdarkly/ios-client-sdk.git', :branch => 'andrey/flag-exposure-dedupe' | ||
| end | ||
|
|
||
| # CocoaPods resets the target's base xcconfig on integrate; keep our wrappers that | ||
| # include both the Pods settings and the local Secrets.xcconfig. | ||
| post_integrate do |installer| | ||
| project_path = File.join(installer.sandbox.root.parent, 'hello-ios.xcodeproj') | ||
| project = Xcodeproj::Project.open(project_path) | ||
| target = project.targets.find { |t| t.name == 'hello-ios' } | ||
| next unless target | ||
|
|
||
| debug_ref = project.files.find { |f| f.path == 'hello-ios/Debug.xcconfig' } | ||
| release_ref = project.files.find { |f| f.path == 'hello-ios/Release.xcconfig' } | ||
| next unless debug_ref && release_ref | ||
|
|
||
| target.build_configurations.each do |config| | ||
| config.base_configuration_reference = config.name == 'Debug' ? debug_ref : release_ref | ||
| end | ||
| project.save | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| // Copy this file to Secrets.xcconfig and fill in the values. | ||
| // Secrets.xcconfig is gitignored — do not commit real keys. | ||
|
|
||
| mobileKey = YOUR_MOBILE_KEY_HERE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| #include "../Pods/Target Support Files/Pods-hello-ios/Pods-hello-ios.debug.xcconfig" | ||
| #include? "../Secrets.xcconfig" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import Foundation | ||
| import LaunchDarkly | ||
|
|
||
| /// Counts the evaluation series stages it observes, so the example can show what exposure | ||
| /// deduplication does. Deduplication is declared on the hook via `evaluationExposureDeduper`, | ||
| /// the same way a customer would configure any other hook. | ||
| /// | ||
| /// Deduplication skips the whole series, so both counts stay equal and both stop climbing while | ||
| /// repeated evaluations resolve to the same result. | ||
| final class ExposureCountingHook: Hook { | ||
| private let label: String | ||
| private let window: TimeInterval | ||
| private let onStage: () -> Void | ||
| private let befores = Counter() | ||
| private let afters = Counter() | ||
|
|
||
| /// The per-hook policy. Declaring it here is the Swift equivalent of Android's | ||
| /// `new MetricsHook().evaluationExposureDeduper(window, maxSize)`. | ||
| let evaluationExposureDeduper: EvaluationExposureDeduper? | ||
|
|
||
| init(label: String, window: TimeInterval, onStage: @escaping () -> Void) { | ||
| self.label = label | ||
| self.window = window | ||
| self.onStage = onStage | ||
| self.evaluationExposureDeduper = EvaluationExposureDeduper(window: window, maxSize: 2_000) | ||
| } | ||
|
|
||
| func metadata() -> Metadata { | ||
| Metadata(name: label) | ||
| } | ||
|
|
||
| func beforeEvaluation(seriesContext: EvaluationSeriesContext, seriesData: EvaluationSeriesData) -> EvaluationSeriesData { | ||
| befores.increment() | ||
| onStage() | ||
| return seriesData | ||
| } | ||
|
|
||
| func afterEvaluation(seriesContext: EvaluationSeriesContext, seriesData: EvaluationSeriesData, evaluationDetail: LDEvaluationDetail<LDValue>) -> EvaluationSeriesData { | ||
| afters.increment() | ||
| onStage() | ||
| return seriesData | ||
| } | ||
|
|
||
| /// A line describing this hook's window and how many evaluations have reached it. | ||
| func status() -> String { | ||
| String(format: "%@ (%.0f s): %d (before %d / after %d)", | ||
| label, | ||
| window, | ||
| afters.value, | ||
| befores.value, | ||
| afters.value) | ||
| } | ||
| } | ||
|
|
||
| /// Tiny thread-safe counter for hook stages invoked from any queue. | ||
| private final class Counter { | ||
| private let lock = NSLock() | ||
| private var _value = 0 | ||
|
|
||
| var value: Int { | ||
| lock.lock() | ||
| defer { lock.unlock() } | ||
| return _value | ||
| } | ||
|
|
||
| func increment() { | ||
| lock.lock() | ||
| _value += 1 | ||
| lock.unlock() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import Foundation | ||
| import LaunchDarkly | ||
|
|
||
| /// Owns the two example hooks and starts the LaunchDarkly client the way a customer would: | ||
| /// each hook carries its own `evaluationExposureDeduper`. | ||
| @objc public final class ExposureDedupeDemo: NSObject { | ||
| @objc public static let shared = ExposureDedupeDemo() | ||
|
|
||
| // Two hooks with different windows, to show that each one is deduplicated on its own. | ||
| // TimeInterval is seconds on iOS (Android uses milliseconds). | ||
| private static let fastWindow: TimeInterval = 5 | ||
| private static let slowWindow: TimeInterval = 10 | ||
| private static let defaultUserKey = "example-user-key" | ||
|
|
||
| private var fastHook: ExposureCountingHook! | ||
| private var slowHook: ExposureCountingHook! | ||
| private var evaluationsRequested = 0 | ||
| private var statusHandler: ((String) -> Void)? | ||
|
|
||
| private override init() { | ||
| super.init() | ||
| } | ||
|
|
||
| /// Starts the client with two independently-deduplicated hooks. Call once from AppDelegate. | ||
| @objc public func startClient(mobileKey: String) { | ||
| fastHook = ExposureCountingHook(label: "fast", window: Self.fastWindow) { [weak self] in | ||
| self?.publishStatus() | ||
| } | ||
| slowHook = ExposureCountingHook(label: "slow", window: Self.slowWindow) { [weak self] in | ||
| self?.publishStatus() | ||
| } | ||
|
|
||
| var config = LDConfig(mobileKey: mobileKey, autoEnvAttributes: .enabled) | ||
| // Same shape a customer uses: each hook declares its own deduper. | ||
| config.hooks = [fastHook, slowHook] | ||
|
|
||
| var builder = LDContextBuilder(key: Self.defaultUserKey) | ||
| builder.kind("user") | ||
| builder.name("Sandy") | ||
| guard case .success(let context) = builder.build() else { | ||
| return | ||
| } | ||
|
|
||
| LDClient.start(config: config, context: context, startWaitSeconds: 5) | ||
| publishStatus() | ||
| } | ||
|
|
||
| @objc public func setStatusHandler(_ handler: @escaping (String) -> Void) { | ||
| statusHandler = handler | ||
| publishStatus() | ||
| } | ||
|
|
||
| @objc public func evaluate(flagKey: String) -> Bool { | ||
| evaluationsRequested += 1 | ||
| let value = LDClient.get()?.boolVariation(forKey: flagKey, defaultValue: false) ?? false | ||
| publishStatus() | ||
| return value | ||
| } | ||
|
|
||
| /// Identifies to `userKey`, or the default key when empty — empty keys are invalid and would | ||
| /// skip the dedupe-cache reset the demo is meant to show. | ||
| @objc public func identify(userKey: String) -> String { | ||
| let key = userKey.trimmingCharacters(in: .whitespacesAndNewlines) | ||
| let resolved = key.isEmpty ? Self.defaultUserKey : key | ||
| guard case .success(let context) = LDContextBuilder(key: resolved).build() else { | ||
| return resolved | ||
| } | ||
| LDClient.get()?.identify(context: context) { _ in } | ||
| publishStatus() | ||
| return resolved | ||
| } | ||
|
|
||
| private func publishStatus() { | ||
| guard let fastHook, let slowHook else { return } | ||
| let text = String(format: "Evaluations requested: %d\n%@\n%@", | ||
| evaluationsRequested, | ||
| fastHook.status(), | ||
| slowHook.status()) | ||
| DispatchQueue.main.async { [statusHandler] in | ||
| statusHandler?(text) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| #include "../Pods/Target Support Files/Pods-hello-ios/Pods-hello-ios.release.xcconfig" | ||
| #include? "../Secrets.xcconfig" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.