-
Notifications
You must be signed in to change notification settings - Fork 23
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: registered LDStatusListeners are now sent updates that result from a call to LDClient.identify(...) #274
Merged
tanderson-ld
merged 2 commits into
main
from
ta/sc-211876/fixing-connection-information-update-bug
Jul 10, 2024
Merged
Changes from all commits
Commits
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 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 |
---|---|---|
|
@@ -76,8 +76,6 @@ class ConnectivityManager { | |
// source we're using, like "if there was an error, update the ConnectionInformation." | ||
private class DataSourceUpdateSinkImpl implements DataSourceUpdateSink { | ||
private final ContextDataManager contextDataManager; | ||
private final AtomicReference<ConnectionMode> connectionMode = new AtomicReference<>(null); | ||
private final AtomicReference<LDFailure> lastFailure = new AtomicReference<>(null); | ||
|
||
DataSourceUpdateSinkImpl(ContextDataManager contextDataManager) { | ||
this.contextDataManager = contextDataManager; | ||
|
@@ -97,40 +95,10 @@ public void upsert(LDContext context, DataModel.Flag item) { | |
|
||
@Override | ||
public void setStatus(ConnectionMode newConnectionMode, Throwable error) { | ||
ConnectionMode oldConnectionMode = newConnectionMode == null ? null : | ||
connectionMode.getAndSet(newConnectionMode); | ||
LDFailure failure = null; | ||
if (error != null) { | ||
if (error instanceof LDFailure) { | ||
failure = (LDFailure)error; | ||
} else { | ||
failure = new LDFailure("Unknown failure", error, LDFailure.FailureType.UNKNOWN_ERROR); | ||
} | ||
lastFailure.set(failure); | ||
} | ||
boolean updated = false; | ||
if (newConnectionMode != null && oldConnectionMode != newConnectionMode) { | ||
if (failure == null && newConnectionMode.isConnectionActive()) { | ||
connectionInformation.setLastSuccessfulConnection(System.currentTimeMillis()); | ||
} | ||
connectionInformation.setConnectionMode(newConnectionMode); | ||
updated = true; | ||
} | ||
if (failure != null) { | ||
connectionInformation.setLastFailedConnection(System.currentTimeMillis()); | ||
connectionInformation.setLastFailure(failure); | ||
updated = true; | ||
} | ||
if (updated) { | ||
try { | ||
saveConnectionInformation(); | ||
} catch (Exception ex) { | ||
LDUtil.logExceptionAtErrorLevel(logger, ex, "Error saving connection information"); | ||
} | ||
updateStatusListeners(connectionInformation); | ||
if (failure != null) { | ||
updateListenersOnFailure(failure); | ||
} | ||
if (error == null) { | ||
updateConnectionInfoForSuccess(newConnectionMode); | ||
} else { | ||
updateConnectionInfoForError(newConnectionMode, error); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For reviewers: Majority of the removed code has been moved into |
||
} | ||
|
||
|
@@ -263,11 +231,17 @@ private synchronized boolean updateDataSource( | |
@Override | ||
public void onSuccess(Boolean result) { | ||
initialized = true; | ||
// passing the current connection mode since we don't want to change the mode, just trigger | ||
// the logic to update the last connection success. | ||
updateConnectionInfoForSuccess(connectionInformation.getConnectionMode()); | ||
onCompletion.onSuccess(null); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable error) { | ||
// passing the current connection mode since we don't want to change the mode, just trigger | ||
// the logic to update the last connection failure. | ||
updateConnectionInfoForError(connectionInformation.getConnectionMode(), error); | ||
onCompletion.onSuccess(null); | ||
} | ||
}); | ||
|
@@ -303,6 +277,52 @@ void unregisterStatusListener(LDStatusListener LDStatusListener) { | |
} | ||
} | ||
|
||
private void updateConnectionInfoForSuccess(ConnectionMode connectionMode) { | ||
boolean updated = false; | ||
if (connectionInformation.getConnectionMode() != connectionMode) { | ||
connectionInformation.setConnectionMode(connectionMode); | ||
updated = true; | ||
} | ||
|
||
// even if connection mode doesn't change, it may be the case that the data source re-established its connection | ||
// and so we should update the last successful connection time (e.g. connection drops and we reconnect, | ||
// an identify occurs) | ||
if (connectionMode.isConnectionActive()) { | ||
connectionInformation.setLastSuccessfulConnection(System.currentTimeMillis()); | ||
updated = true; | ||
} | ||
|
||
if (updated) { | ||
try { | ||
saveConnectionInformation(connectionInformation); | ||
} catch (Exception ex) { | ||
LDUtil.logExceptionAtErrorLevel(logger, ex, "Error saving connection information"); | ||
} | ||
updateStatusListeners(connectionInformation); | ||
} | ||
} | ||
|
||
private void updateConnectionInfoForError(ConnectionMode connectionMode, Throwable error) { | ||
LDFailure failure = null; | ||
if (error != null) { | ||
if (error instanceof LDFailure) { | ||
failure = (LDFailure)error; | ||
} else { | ||
failure = new LDFailure("Unknown failure", error, LDFailure.FailureType.UNKNOWN_ERROR); | ||
} | ||
} | ||
|
||
connectionInformation.setConnectionMode(connectionMode); | ||
connectionInformation.setLastFailedConnection(System.currentTimeMillis()); | ||
connectionInformation.setLastFailure(failure); | ||
try { | ||
saveConnectionInformation(connectionInformation); | ||
} catch (Exception ex) { | ||
LDUtil.logExceptionAtErrorLevel(logger, ex, "Error saving connection information"); | ||
} | ||
updateStatusListeners(connectionInformation); | ||
} | ||
|
||
private void readStoredConnectionState() { | ||
PersistentDataStoreWrapper.SavedConnectionInfo savedConnectionInfo = | ||
environmentStore.getConnectionInfo(); | ||
|
@@ -315,7 +335,7 @@ private void readStoredConnectionState() { | |
connectionInformation.setLastFailure(savedConnectionInfo.lastFailure); | ||
} | ||
|
||
private synchronized void saveConnectionInformation() { | ||
private synchronized void saveConnectionInformation(ConnectionInformation connectionInformation) { | ||
PersistentDataStoreWrapper.SavedConnectionInfo savedConnectionInfo = | ||
new PersistentDataStoreWrapper.SavedConnectionInfo( | ||
connectionInformation.getLastSuccessfulConnection(), | ||
|
This file contains 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
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.
For reviewers: These pieces of data duplicated what was held in the ConnectionInformationState member of the ConnectivityManager, so removed duplication to reduce chance of being out of sync.