Skip to content

Change WithinAppServiceConnection's ScheduledThreadPoolExecutor's con… #6414

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
Nov 4, 2024
Merged
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
3 changes: 3 additions & 0 deletions firebase-messaging/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
* [deprecated] Deprecated additional FCM upstream messaging methods. See the
[FAQ](https://firebase.google.com/support/faq#fcm-23-deprecation) for more
details.
* [changed] Changed WithinAppServiceConnection's ScheduledThreadPoolExecutor's
configuration to allow the thread to stop polling after the timeout task has
been canceled.


# 24.0.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.google.firebase.messaging;

import static com.google.firebase.messaging.FirebaseMessaging.TAG;
import static java.util.concurrent.TimeUnit.SECONDS;

import android.annotation.SuppressLint;
import android.content.ComponentName;
Expand All @@ -26,7 +27,6 @@
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import com.google.android.gms.common.stats.ConnectionTracker;
import com.google.android.gms.common.util.concurrent.NamedThreadFactory;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.TaskCompletionSource;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
Expand All @@ -35,7 +35,6 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
* Helper object to abstract the ServiceConnection lifecycle for binding to services within the same
Expand Down Expand Up @@ -65,7 +64,7 @@ void arrangeTimeout(ScheduledExecutorService executor) {
finish();
},
EnhancedIntentService.MESSAGE_TIMEOUT_S,
TimeUnit.SECONDS);
SECONDS);

getTask()
.addOnCompleteListener(
Expand Down Expand Up @@ -100,18 +99,20 @@ void finish() {
@GuardedBy("this")
private boolean connectionInProgress = false;

// TODO(b/258424124): Migrate to go/firebase-android-executors
@SuppressLint("ThreadPoolCreation")
WithinAppServiceConnection(Context context, String action) {
// Class instances are owned by a static variable in FirebaseInstanceIdReceiver
// and GcmReceiver so that they survive getting gc'd and reinstantiated, so use a
// scheduled thread pool executor with core size of 0 so that the no threads will be
// kept idle.
this(
context,
action,
new ScheduledThreadPoolExecutor(
0, new NamedThreadFactory("Firebase-FirebaseInstanceIdServiceConnection")));
this(context, action, createScheduledThreadPoolExecutor());
}

@SuppressLint("ThreadPoolCreation")
private static ScheduledThreadPoolExecutor createScheduledThreadPoolExecutor() {
ScheduledThreadPoolExecutor threadPoolExecutor = new ScheduledThreadPoolExecutor(1);
threadPoolExecutor.setKeepAliveTime(EnhancedIntentService.MESSAGE_TIMEOUT_S * 2, SECONDS);
threadPoolExecutor.allowCoreThreadTimeOut(true);
return threadPoolExecutor;
}

@VisibleForTesting
Expand Down
Loading