Skip to content

Cache FID to avoid multiple disk reads. #1571

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

Merged
merged 2 commits into from
May 20, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public class FirebaseInstallations implements FirebaseInstallationsApi {
private final Object lock = new Object();
private final ExecutorService backgroundExecutor;
private final ExecutorService networkExecutor;
/* FID of this Firebase Installations instance. Cached after successfully registering and
persisting the FID locally. NOTE: cachedFid resets if FID is deleted.*/
private String cachedFid = null;

@GuardedBy("lock")
private final List<StateListener> listeners = new ArrayList<>();
Expand Down Expand Up @@ -284,6 +287,9 @@ private void triggerOnException(PersistedInstallationEntry prefs, Exception exce
}

private String doGetId() {
if (cachedFid != null) {
return cachedFid;
}
PersistedInstallationEntry prefs = getPrefsWithGeneratedIdMultiProcessSafe();
// Execute network calls (CreateInstallations) to the FIS Servers on a separate executor
// i.e networkExecutor
Expand Down Expand Up @@ -337,6 +343,11 @@ private void doNetworkCall(boolean forceRefresh) {
// Store the prefs to persist the result of the previous step.
insertOrUpdatePrefs(prefs);

// Update cachedFID, if FID is successfully REGISTERED and persisted.
if (prefs.isRegistered()) {
cachedFid = prefs.getFirebaseInstallationId();
}

// Let the caller know about the result.
if (prefs.isErrored()) {
triggerOnException(prefs, new FirebaseInstallationsException(Status.BAD_CONFIG));
Expand Down Expand Up @@ -493,6 +504,7 @@ private PersistedInstallationEntry fetchAuthTokenFromServer(
case AUTH_ERROR:
// The the server refused to generate a new auth token due to bad credentials, clear the
// FID to force the generation of a new one.
cachedFid = null;
return prefs.withNoGeneratedFid();
default:
throw new IOException();
Expand All @@ -504,6 +516,7 @@ private PersistedInstallationEntry fetchAuthTokenFromServer(
* storage.
*/
private Void deleteFirebaseInstallationId() throws FirebaseInstallationsException, IOException {
cachedFid = null;
PersistedInstallationEntry entry = getMultiProcessSafePrefs();
if (entry.isRegistered()) {
// Call the FIS servers to delete this Firebase Installation Id.
Expand All @@ -519,7 +532,6 @@ private Void deleteFirebaseInstallationId() throws FirebaseInstallationsExceptio
"Failed to delete a Firebase Installation.", Status.BAD_CONFIG);
}
}

insertOrUpdatePrefs(entry.withNoGeneratedFid());
return null;
}
Expand Down