Skip to content

Commit 6383f6d

Browse files
authored
Change WithinAppServiceConnection's ScheduledThreadPoolExecutor's con… (#6414)
…figuration to avoid polling. * Changed WithinAppServiceConnection's ScheduledThreadPoolExecutor's configuration to allow the thread to stop polling after the timeout task has been canceled.
1 parent d402ad0 commit 6383f6d

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

firebase-messaging/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
* [deprecated] Deprecated additional FCM upstream messaging methods. See the
33
[FAQ](https://firebase.google.com/support/faq#fcm-23-deprecation) for more
44
details.
5+
* [changed] Changed WithinAppServiceConnection's ScheduledThreadPoolExecutor's
6+
configuration to allow the thread to stop polling after the timeout task has
7+
been canceled.
58

69

710
# 24.0.3

firebase-messaging/src/main/java/com/google/firebase/messaging/WithinAppServiceConnection.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package com.google.firebase.messaging;
1515

1616
import static com.google.firebase.messaging.FirebaseMessaging.TAG;
17+
import static java.util.concurrent.TimeUnit.SECONDS;
1718

1819
import android.annotation.SuppressLint;
1920
import android.content.ComponentName;
@@ -26,7 +27,6 @@
2627
import androidx.annotation.Nullable;
2728
import androidx.annotation.VisibleForTesting;
2829
import com.google.android.gms.common.stats.ConnectionTracker;
29-
import com.google.android.gms.common.util.concurrent.NamedThreadFactory;
3030
import com.google.android.gms.tasks.Task;
3131
import com.google.android.gms.tasks.TaskCompletionSource;
3232
import com.google.errorprone.annotations.CanIgnoreReturnValue;
@@ -35,7 +35,6 @@
3535
import java.util.concurrent.ScheduledExecutorService;
3636
import java.util.concurrent.ScheduledFuture;
3737
import java.util.concurrent.ScheduledThreadPoolExecutor;
38-
import java.util.concurrent.TimeUnit;
3938

4039
/**
4140
* Helper object to abstract the ServiceConnection lifecycle for binding to services within the same
@@ -65,7 +64,7 @@ void arrangeTimeout(ScheduledExecutorService executor) {
6564
finish();
6665
},
6766
EnhancedIntentService.MESSAGE_TIMEOUT_S,
68-
TimeUnit.SECONDS);
67+
SECONDS);
6968

7069
getTask()
7170
.addOnCompleteListener(
@@ -100,18 +99,20 @@ void finish() {
10099
@GuardedBy("this")
101100
private boolean connectionInProgress = false;
102101

103-
// TODO(b/258424124): Migrate to go/firebase-android-executors
104-
@SuppressLint("ThreadPoolCreation")
105102
WithinAppServiceConnection(Context context, String action) {
106103
// Class instances are owned by a static variable in FirebaseInstanceIdReceiver
107104
// and GcmReceiver so that they survive getting gc'd and reinstantiated, so use a
108105
// scheduled thread pool executor with core size of 0 so that the no threads will be
109106
// kept idle.
110-
this(
111-
context,
112-
action,
113-
new ScheduledThreadPoolExecutor(
114-
0, new NamedThreadFactory("Firebase-FirebaseInstanceIdServiceConnection")));
107+
this(context, action, createScheduledThreadPoolExecutor());
108+
}
109+
110+
@SuppressLint("ThreadPoolCreation")
111+
private static ScheduledThreadPoolExecutor createScheduledThreadPoolExecutor() {
112+
ScheduledThreadPoolExecutor threadPoolExecutor = new ScheduledThreadPoolExecutor(1);
113+
threadPoolExecutor.setKeepAliveTime(EnhancedIntentService.MESSAGE_TIMEOUT_S * 2, SECONDS);
114+
threadPoolExecutor.allowCoreThreadTimeOut(true);
115+
return threadPoolExecutor;
115116
}
116117

117118
@VisibleForTesting

0 commit comments

Comments
 (0)