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

feat: honor polling interval across process restarts #282

Merged
merged 7 commits into from
Nov 22, 2024

Conversation

tanderson-ld
Copy link
Contributor

@tanderson-ld tanderson-ld commented Nov 19, 2024

Requirements

  • I have added test coverage for new or changed functionality

  • I have followed the repository's pull request submission guidelines

  • I have validated my changes against all supported platform versions

Related issues

SDK-881

Describe the solution you've provided

Polling data source builder now uses context index timestamp to calculate if an initial polling delay.

@@ -244,30 +244,45 @@ static final class PollingDataSourceBuilderImpl extends PollingDataSourceBuilder
implements DiagnosticDescription, DataSourceRequiresFeatureFetcher {
@Override
public DataSource build(ClientContext clientContext) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reviewers: Updated from using ClientContext to ClientContextImpl type here to have access to an internal only object, namely the PerEnvironmentData.

@@ -138,7 +138,7 @@ private Request getDefaultRequest(LDContext ldContext) throws IOException {
// and methods like Uri.withAppendedPath, simply to minimize the amount of code that relies on
// Android-specific APIs so our components are more easily unit-testable.
URI uri = HttpHelpers.concatenateUriPath(pollUri, StandardEndpoints.POLLING_REQUEST_GET_BASE_PATH);
uri = HttpHelpers.concatenateUriPath(uri, LDUtil.base64Url(ldContext));
uri = HttpHelpers.concatenateUriPath(uri, LDUtil.urlSafeBase64(ldContext));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reviewers: Just a rename

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reviewers: This class was a glorified static method, so I got rid of it and moved it to LDUtil.

public static String hashedContextId(final LDContext context) {
return HASHER.hash(context.getFullyQualifiedKey());
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Reviewers: Moved to LDUtil

@tanderson-ld tanderson-ld marked this pull request as ready for review November 21, 2024 16:54
@tanderson-ld tanderson-ld requested a review from a team as a code owner November 21, 2024 16:54
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reviewers: Associated class was removed.

@@ -65,6 +65,11 @@ public void setUp() {
});
}

@Before
public void before() {
store = new InMemoryPersistentDataStore();
Copy link
Contributor Author

@tanderson-ld tanderson-ld Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reviewers: Tests were affecting each other's stores. Now they don't.

// this initial delay logic. Calculate how much time has passed since the last update, if that is less than
// the polling interval, delay by the difference, otherwise 0 delay.
long elapsedSinceUpdate = System.currentTimeMillis() - lastUpdated;
long initialDelayMillis = Math.max(pollInterval - elapsedSinceUpdate, 0);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reviewers: These code changes are the meat and potatoes of the PR.

pollIntervalMillis;
int initialDelayMillis;
if (clientContext.isInBackground() && Boolean.FALSE.equals(clientContext.getPreviouslyInBackground())) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reviewers: This background/foregrounding distinction is no more! The only impact foreground/background has is on the polling interval. Now they are treated the same after that which results in some improvements in behavioral consistency.

@@ -39,8 +39,6 @@
* deferred listener calls are done via the {@link TaskExecutor} abstraction.
*/
final class ContextDataManager {
static final ContextHasher HASHER = new ContextHasher();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reviewers: ContextHasher was eliminated in favor of a static method.

}

if (logger.isEnabled(LDLogLevel.DEBUG)) {
logger.debug("Stored context index is now: {}", newIndex.toJson());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reviewers: Android was updating the index timestamp when switching context. This is not consistent with JS, Flutter, and the iOS SDK. Updated this logic so that the index is only updated when new flag data needs to be written to persistence.

String fingerprint = LDUtil.urlSafeBase64Hash(context);
environmentStore.setContextData(hashedContextId, fingerprint, updatedFlags);
index = index.updateTimestamp(hashedContextId, System.currentTimeMillis());
environmentStore.setIndex(index);
Copy link
Contributor Author

@tanderson-ld tanderson-ld Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reviewers: This code path is hit when a flag patch comes in.

@tanderson-ld tanderson-ld merged commit 9b3ca70 into main Nov 22, 2024
2 checks passed
@tanderson-ld tanderson-ld deleted the ta/sdk-881/honor-polling-across-restarts branch November 22, 2024 23:30
tanderson-ld pushed a commit that referenced this pull request Nov 22, 2024
🤖 I have created a release *beep* *boop*
---


##
[5.5.0](5.4.0...5.5.0)
(2024-11-22)


### Features

* honor polling interval across process restarts
([#282](#282))
([9b3ca70](9b3ca70))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants