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 1 commit
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 @@ -68,6 +68,8 @@ public class FirebaseInstallations implements FirebaseInstallationsApi {
private final ExecutorService backgroundExecutor;
private final ExecutorService networkExecutor;

private String cachedFid = null;

@GuardedBy("lock")
private final List<StateListener> listeners = new ArrayList<>();

Expand Down Expand Up @@ -215,7 +217,11 @@ String getName() {
public Task<String> getId() {
preConditionChecks();
TaskCompletionSource<String> taskCompletionSource = new TaskCompletionSource<>();
taskCompletionSource.trySetResult(doGetId());
if (cachedFid != null) {
taskCompletionSource.trySetResult(cachedFid);
} else {
taskCompletionSource.trySetResult(doGetId());
}
return taskCompletionSource.getTask();
}

Expand Down Expand Up @@ -454,6 +460,7 @@ private PersistedInstallationEntry registerFidWithServer(PersistedInstallationEn

switch (response.getResponseCode()) {
case OK:
cachedFid = response.getFid();
return prefs.withRegisteredFid(
response.getFid(),
response.getRefreshToken(),
Expand Down Expand Up @@ -493,6 +500,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 @@ -519,7 +527,7 @@ private Void deleteFirebaseInstallationId() throws FirebaseInstallationsExceptio
"Failed to delete a Firebase Installation.", Status.BAD_CONFIG);
}
}

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