Skip to content

Immediately close realtime connections when in background #5819

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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 @@ -35,6 +35,7 @@
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.json.JSONException;
import org.json.JSONObject;

Expand All @@ -54,21 +55,24 @@ public class ConfigAutoFetch {
private final ConfigUpdateListener retryCallback;
private final ScheduledExecutorService scheduledExecutorService;
private final Random random;
private AtomicBoolean isInBackground;

public ConfigAutoFetch(
HttpURLConnection httpURLConnection,
ConfigFetchHandler configFetchHandler,
ConfigCacheClient activatedCache,
Set<ConfigUpdateListener> eventListeners,
ConfigUpdateListener retryCallback,
ScheduledExecutorService scheduledExecutorService) {
ScheduledExecutorService scheduledExecutorService,
AtomicBoolean isInBackground) {
this.httpURLConnection = httpURLConnection;
this.configFetchHandler = configFetchHandler;
this.activatedCache = activatedCache;
this.eventListeners = eventListeners;
this.retryCallback = retryCallback;
this.scheduledExecutorService = scheduledExecutorService;
this.random = new Random();
this.isInBackground = isInBackground;
}

private synchronized void propagateErrors(FirebaseRemoteConfigException exception) {
Expand Down Expand Up @@ -126,7 +130,7 @@ private void handleNotifications(InputStream inputStream) throws IOException {
// Multiple config update messages can be sent through this loop. Each message comes in line by
// line as partialConfigUpdateMessage and are accumulated together into
// currentConfigUpdateMessage.
while ((partialConfigUpdateMessage = reader.readLine()) != null) {
while ((partialConfigUpdateMessage = reader.readLine()) != null && !isInBackground.get()) {
// Accumulate all the partial parts of the message until we have a full message.
currentConfigUpdateMessage += partialConfigUpdateMessage;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONObject;
Expand Down Expand Up @@ -98,7 +99,7 @@ public class ConfigRealtimeHttpClient {
private boolean isRealtimeDisabled;

/** Flag to indicate whether or not the app is in the background or not. */
private boolean isInBackground;
private AtomicBoolean isInBackground;

private final int ORIGINAL_RETRIES = 8;
private final ScheduledExecutorService scheduledExecutorService;
Expand Down Expand Up @@ -144,7 +145,7 @@ public ConfigRealtimeHttpClient(
this.namespace = namespace;
this.metadataClient = metadataClient;
this.isRealtimeDisabled = false;
this.isInBackground = false;
this.isInBackground = new AtomicBoolean(false);
}

private static String extractProjectNumberFromAppId(String gmpAppId) {
Expand Down Expand Up @@ -286,7 +287,7 @@ private synchronized boolean canMakeHttpStreamConnection() {
return !listeners.isEmpty()
&& !isHttpConnectionRunning
&& !isRealtimeDisabled
&& !isInBackground;
&& !isInBackground.get();
}

private String getRealtimeURL(String namespace) {
Expand Down Expand Up @@ -383,7 +384,7 @@ public void run() {
},
retryMilliseconds,
TimeUnit.MILLISECONDS);
} else if (!isInBackground) {
} else if (!isInBackground.get()) {
propagateErrors(
new FirebaseRemoteConfigClientException(
"Unable to connect to the server. Check your connection and try again.",
Expand All @@ -392,7 +393,7 @@ public void run() {
}

void setRealtimeBackgroundState(boolean backgroundState) {
isInBackground = backgroundState;
isInBackground.set(backgroundState);
}

private synchronized void resetRetryCount() {
Expand Down Expand Up @@ -429,7 +430,8 @@ public void onError(@NonNull FirebaseRemoteConfigException error) {
activatedCache,
listeners,
retryCallback,
scheduledExecutorService);
scheduledExecutorService,
isInBackground);
}

// HTTP status code that the Realtime client should retry on.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -348,7 +349,8 @@ public void onError(@NonNull FirebaseRemoteConfigException error) {
mockActivatedCache,
listeners,
mockRetryListener,
scheduledExecutorService);
scheduledExecutorService,
new AtomicBoolean(false));
realtimeMetadataClient =
new ConfigMetadataClient(context.getSharedPreferences("test_file", Context.MODE_PRIVATE));
configRealtimeHttpClient =
Expand Down