-
Notifications
You must be signed in to change notification settings - Fork 23
chore: FDv2 State Debouncing #344
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
aaron-zeisler
wants to merge
4
commits into
main
Choose a base branch
from
aaronz/SDK-2001/fdv2-state-debouncing
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ad4d74c
[SDK-2001] feat: add StateDebounceManager for FDv2 state change debou…
aaron-zeisler aa1b6f3
[SDK-2001] feat: wire StateDebounceManager into ConnectivityManager f…
aaron-zeisler 149076b
[SDK-2001] fix: thread debounceMs through LDConfig to fix instrumente…
aaron-zeisler c134ebf
Merge branch 'main' into aaronz/SDK-2001/fdv2-state-debouncing
aaron-zeisler 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
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
94 changes: 94 additions & 0 deletions
94
...y-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/StateDebounceManager.java
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,94 @@ | ||
| package com.launchdarkly.sdk.android; | ||
|
|
||
| import java.util.concurrent.ScheduledFuture; | ||
|
|
||
| /** | ||
| * Debounces FDv2 state changes (network, lifecycle) into a single | ||
| * reconciliation callback (CONNMODE 3.5). Each state change resets a timer; when | ||
| * the timer fires, the callback runs with the latest accumulated state. | ||
| * <p> | ||
| * {@code identify()} does NOT participate in debounce (CONNMODE 3.5.6). Callers | ||
| * handle this by closing and recreating the manager on identify. | ||
| * <p> | ||
| * FDv1 data sources do not use this class. The existing {@link Debounce} class | ||
| * (used by {@code pollDebouncer}) serves a different purpose in the FDv1 path. | ||
| */ | ||
| final class StateDebounceManager { | ||
|
|
||
| static final long DEFAULT_DEBOUNCE_MS = 1000; | ||
|
|
||
| private final Object lock = new Object(); | ||
| private final TaskExecutor taskExecutor; | ||
| private final long debounceMs; | ||
| private final Runnable onReconcile; | ||
|
|
||
| private volatile boolean networkAvailable; | ||
| private volatile boolean foreground; | ||
|
|
||
| private ScheduledFuture<?> pendingTimer; | ||
| private volatile boolean closed; | ||
|
|
||
| StateDebounceManager( | ||
| boolean initialNetworkAvailable, | ||
| boolean initialForeground, | ||
| TaskExecutor taskExecutor, | ||
| long debounceMs, | ||
| Runnable onReconcile | ||
| ) { | ||
| this.networkAvailable = initialNetworkAvailable; | ||
| this.foreground = initialForeground; | ||
| this.taskExecutor = taskExecutor; | ||
| this.debounceMs = debounceMs; | ||
| this.onReconcile = onReconcile; | ||
| } | ||
|
|
||
| void setNetworkAvailable(boolean available) { | ||
| if (this.networkAvailable == available) { | ||
| return; | ||
| } | ||
| this.networkAvailable = available; | ||
| resetTimer(); | ||
| } | ||
|
|
||
| void setForeground(boolean fg) { | ||
| if (this.foreground == fg) { | ||
| return; | ||
| } | ||
| this.foreground = fg; | ||
| resetTimer(); | ||
| } | ||
|
|
||
| boolean isNetworkAvailable() { | ||
| return networkAvailable; | ||
| } | ||
|
|
||
| boolean isForeground() { | ||
| return foreground; | ||
| } | ||
|
|
||
| void close() { | ||
| closed = true; | ||
| synchronized (lock) { | ||
| if (pendingTimer != null) { | ||
| pendingTimer.cancel(false); | ||
| pendingTimer = null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void resetTimer() { | ||
| if (closed) { | ||
| return; | ||
| } | ||
| synchronized (lock) { | ||
| if (pendingTimer != null) { | ||
| pendingTimer.cancel(false); | ||
| } | ||
| pendingTimer = taskExecutor.scheduleTask(() -> { | ||
| if (!closed) { | ||
| onReconcile.run(); | ||
| } | ||
| }, debounceMs); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debounce manager gets stale state when switching partially disabled
Medium Severity
When
autoModeSwitchingConfig.isNetwork()isfalse, theconnectivityChangeListenerreturns early without callingdm.setNetworkAvailable(), leaving theStateDebounceManagerwith stale network state. Symmetrically, whenisLifecycle()isfalse,dm.setForeground()is never called. When the other (enabled) axis later triggers a debounce,handleDebouncedModeStateChangereads both dimensions from the debounce manager, causingresolveModeto use stale data — e.g., resolving toBACKGROUNDwhen the network is actually offline, orSTREAMINGwhen the app is actually backgrounded.Additional Locations (2)
launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/ConnectivityManager.java#L202-L205launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/ConnectivityManager.java#L660-L665Reviewed by Cursor Bugbot for commit c134ebf. Configure here.