Skip to content

Commit 51aad79

Browse files
committed
code review changes
1 parent df2c8f5 commit 51aad79

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

firebase-perf/src/main/java/com/google/firebase/perf/config/RemoteConfigManager.java

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
import com.google.firebase.remoteconfig.FirebaseRemoteConfigValue;
2828
import com.google.firebase.remoteconfig.RemoteConfigComponent;
2929
import java.util.Map;
30-
import java.util.Random;
3130
import java.util.concurrent.ConcurrentHashMap;
3231
import java.util.concurrent.Executor;
3332
import java.util.concurrent.LinkedBlockingQueue;
33+
import java.util.concurrent.ThreadLocalRandom;
3434
import java.util.concurrent.ThreadPoolExecutor;
3535
import java.util.concurrent.TimeUnit;
3636

@@ -50,12 +50,12 @@ public class RemoteConfigManager {
5050
TimeUnit.HOURS.toMillis(12);
5151
private static final long FETCH_NEVER_HAPPENED_TIMESTAMP_MS = 0;
5252
private static final long MIN_APP_START_CONFIG_FETCH_DELAY_MS = 5000;
53-
private static final int RANDOM_APP_START_CONFIG_FETCH_DELAY_MS = 25000;
53+
private static final long RANDOM_APP_START_CONFIG_FETCH_DELAY_MS = 25000;
5454

5555
private final ConcurrentHashMap<String, FirebaseRemoteConfigValue> allRcConfigMap;
5656
private final Executor executor;
57-
private final long appStartTime;
58-
private final long appStartConfigFetchDelay;
57+
private final long appStartTimeInMs;
58+
private final long appStartConfigFetchDelayInMs;
5959

6060
private long firebaseRemoteConfigLastFetchTimestampMs = FETCH_NEVER_HAPPENED_TIMESTAMP_MS;
6161

@@ -79,20 +79,22 @@ private RemoteConfigManager() {
7979
executor,
8080
firebaseRemoteConfig,
8181
MIN_APP_START_CONFIG_FETCH_DELAY_MS
82-
+ new Random().nextInt(RANDOM_APP_START_CONFIG_FETCH_DELAY_MS));
82+
+ ThreadLocalRandom.current().nextLong(RANDOM_APP_START_CONFIG_FETCH_DELAY_MS));
8383
}
8484

8585
@VisibleForTesting
8686
RemoteConfigManager(
87-
Executor executor, FirebaseRemoteConfig firebaseRemoteConfig, long fetchDelay) {
87+
Executor executor,
88+
FirebaseRemoteConfig firebaseRemoteConfig,
89+
long appStartConfigFetchDelayInMs) {
8890
this.executor = executor;
8991
this.firebaseRemoteConfig = firebaseRemoteConfig;
9092
this.allRcConfigMap =
9193
firebaseRemoteConfig == null
9294
? new ConcurrentHashMap<>()
9395
: new ConcurrentHashMap<>(firebaseRemoteConfig.getAll());
94-
this.appStartTime = getCurrentSystemTimeMillis();
95-
this.appStartConfigFetchDelay = fetchDelay;
96+
this.appStartTimeInMs = getCurrentSystemTimeMillis();
97+
this.appStartConfigFetchDelayInMs = appStartConfigFetchDelayInMs;
9698
}
9799

98100
/** Gets the singleton instance. */
@@ -313,7 +315,7 @@ private void triggerRemoteConfigFetchIfNecessary() {
313315
if (allRcConfigMap.isEmpty()) { // Initial fetch.
314316
syncConfigValues(firebaseRemoteConfig.getAll());
315317
}
316-
if (passesRandomAppStartDelay() && shouldFetchAndActivateRemoteConfigValues()) {
318+
if (shouldFetchAndActivateRemoteConfigValues()) {
317319
triggerFirebaseRemoteConfigFetchAndActivateOnSuccessfulFetch();
318320
}
319321
}
@@ -360,22 +362,34 @@ public boolean isFirebaseRemoteConfigAvailable() {
360362
return firebaseRemoteConfig != null;
361363
}
362364

365+
/** Returns true if a RC fetch should be made, false otherwise. */
366+
private boolean shouldFetchAndActivateRemoteConfigValues() {
367+
long currentTimeInMs = getCurrentSystemTimeMillis();
368+
return hasAppStartConfigFetchDelayElapsed(currentTimeInMs)
369+
&& hasLastFetchBecomeStale(currentTimeInMs);
370+
}
371+
372+
/**
373+
* Delay fetch by some random time since app start. This is to prevent b/187985523.
374+
*
375+
* @return true if the random delay has elapsed, false otherwise
376+
*/
377+
private boolean hasAppStartConfigFetchDelayElapsed(long currentTimeInMs) {
378+
return (currentTimeInMs - appStartTimeInMs) >= appStartConfigFetchDelayInMs;
379+
}
380+
363381
// We want to fetch once when the app starts and every 12 hours after that.
364382
// The reason we maintain our own timestamps and do not use FRC's is because FRC only updates
365383
// the last successful fetch timestamp AFTER successfully fetching - which might mean that
366384
// we could potentially fire off multiple fetch requests. This protects against that because
367385
// we update the timestamp before a successful fetch and reset it back if the fetch was
368386
// unsuccessful, making sure that a fetch is triggered again.
369387
// TODO(b/132369190): This shouldn't be needed once the feature is implemented in FRC.
370-
private boolean shouldFetchAndActivateRemoteConfigValues() {
371-
return (getCurrentSystemTimeMillis() - firebaseRemoteConfigLastFetchTimestampMs)
388+
private boolean hasLastFetchBecomeStale(long currentTimeInMs) {
389+
return (currentTimeInMs - firebaseRemoteConfigLastFetchTimestampMs)
372390
> TIME_AFTER_WHICH_A_FETCH_IS_CONSIDERED_STALE_MS;
373391
}
374392

375-
private boolean passesRandomAppStartDelay() {
376-
return (getCurrentSystemTimeMillis() - appStartTime) >= appStartConfigFetchDelay;
377-
}
378-
379393
/** Gets the version code of the Android app. */
380394
@VisibleForTesting
381395
public static int getVersionCode(Context context) {

0 commit comments

Comments
 (0)