Skip to content

Fix Android Connectivity Monitor (v2) #1045

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 27 commits into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
11ca8a5
did the thing
Oct 23, 2019
448311d
comment fixes
Oct 23, 2019
403f91f
Merge branch 'master' into bc/reconnect
Oct 30, 2019
00568be
resolved comments
Oct 30, 2019
c865827
Merge branch 'bc/reconnect' of github.com:firebase/firebase-android-s…
Oct 30, 2019
d714378
just kidding, had to update more comments and remove unused vars
Oct 30, 2019
55e8f46
fix onlinestatetracker constructor
Oct 30, 2019
d202893
continue, make spec tests pass
Nov 1, 2019
411ed9d
resolve comments: comments, code ordering, rename to connectivity_att…
Nov 5, 2019
1519a40
Merge branch 'master' into bc/reconnect
Nov 5, 2019
5bce0a0
separate online_state_timeout from connectivity_attempt_timeout
Nov 12, 2019
ba515b7
update comments
Nov 12, 2019
110fdc3
Merge branch 'master' into bc/reconnect
Nov 20, 2019
c9cafd5
Merge branch 'master' into bc/reconnect
Dec 3, 2019
b77e02e
update comments
Dec 3, 2019
31d0ad8
working with logging comments for future debugging
Dec 6, 2019
8b2ad17
ready for review
Dec 6, 2019
af9ed48
Merge branch 'master' into bc/reconnect-grpc
Dec 6, 2019
1cca6a4
resolve michael comments with runBidi, has comments
Dec 9, 2019
86b4733
working in grpc exclusively with logs
Dec 10, 2019
873d83d
remove logs
Dec 10, 2019
d321316
remove markChannelIdle()
Dec 10, 2019
a5dba09
change close() from protected to private
Dec 10, 2019
51fac1c
added logging, fixed comments
Dec 10, 2019
595046f
fix backoff maxDelay, add comments, some renaming
Dec 12, 2019
a3fc304
Merge branch 'master' into bc/reconnect-grpc
Jan 8, 2020
36e448f
comment fixes and always clear connectivity timer
Jan 8, 2020
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 @@ -177,7 +177,8 @@ public void run() {
/**
* Maximum backoff time for reconnecting when we know the connection is failed on the client-side.
*/
private static final long DNS_FAILURE_BACKOFF_MAX_DELAY_MS = TimeUnit.SECONDS.toMillis(10);
private static final long BACKOFF_CLIENT_NETWORK_FAILURE_MAX_DELAY_MS =
TimeUnit.SECONDS.toMillis(10);

@Nullable private DelayedTask idleTimer;

Expand Down Expand Up @@ -308,8 +309,9 @@ private void close(State finalState, Status status) {
} else if (code == Code.UNAVAILABLE) {
// This exception is thrown when the gRPC connection fails on the client side, To shorten
// reconnect time, we can use a shorter max delay when reconnecting.
if (status.getCause() instanceof java.net.UnknownHostException) {
backoff.setTemporaryMaxDelay(DNS_FAILURE_BACKOFF_MAX_DELAY_MS);
if (status.getCause() instanceof java.net.UnknownHostException
|| status.getCause() instanceof java.net.ConnectException) {
backoff.setTemporaryMaxDelay(BACKOFF_CLIENT_NETWORK_FAILURE_MAX_DELAY_MS);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class GrpcCallProvider {
// This timeout is used when attempting to establish a connection in gRPC. If a connection attempt
// does not succeed in CONNECTIVITY_ATTEMPT_TIMEOUT_MS, we restart the channel and try
// reconnecting again, rather than waiting up to 2+ minutes for gRPC to timeout.
// More details about usage can be found in GrpcCallProvider.onConnectivityStateChanged().
private static final int CONNECTIVITY_ATTEMPT_TIMEOUT_MS = 15 * 1000;
private DelayedTask connectivityAttemptTimer;

Expand Down Expand Up @@ -201,6 +202,21 @@ void shutdown() {
}
}

/**
* Monitors the connectivity state of the gRPC channel and resets the channel when gRPC fails to
* connect.
*
* <p>We currently cannot configure timeouts in connection attempts for gRPC
* (https://github.com/grpc/grpc-java/issues/1943), and until they support doing so, the gRPC
* connection can stay open for up to 2+ minutes before shutting down.
*
* <p>We start a timer when the channel enters ConnectivityState.CONNECTING. If the timer elapses,
* we reset the channel by shutting it down and reinitializing the channelTask. Changes to the
* connectivity state will clear the timer and start a new one-time listener for the next
* ConnectivityState change.
*
* @param channel The channel to monitor the connectivity state of.
*/
private void onConnectivityStateChange(ManagedChannel channel) {
ConnectivityState newState = channel.getState(true);
Logger.debug(LOG_TAG, "Current gRPC connectivity state: " + newState);
Expand All @@ -216,24 +232,18 @@ private void onConnectivityStateChange(ManagedChannel channel) {
CONNECTIVITY_ATTEMPT_TIMEOUT_MS,
() -> {
Logger.debug(LOG_TAG, "connectivityAttemptTimer elapsed. Resetting the channel.");
clearConnectivityTimer();
clearConnectivityAttemptTimer();
resetChannel(channel);
});
} else {
// Clear the timer otherwise, so we don't end up with multiple connectivityAttemptTimers.
clearConnectivityTimer();
clearConnectivityAttemptTimer();
}
// Re-listen for next state change.
channel.notifyWhenStateChanged(
newState, () -> asyncQueue.enqueueAndForget(() -> onConnectivityStateChange(channel)));
}

/**
* Shuts down and reinitializes the channel.
*
* <p>This is used when the connectivity attempt timer elapses and we need to reset the gRPC
* channel to reestablish connectivity.
*/
private void resetChannel(ManagedChannel channel) {
asyncQueue.enqueueAndForget(
() -> {
Expand Down Expand Up @@ -264,8 +274,7 @@ private void initChannelTask() {
});
}

/** Clears the connectivity timer if it exists. */
private void clearConnectivityTimer() {
private void clearConnectivityAttemptTimer() {
if (connectivityAttemptTimer != null) {
Logger.debug(LOG_TAG, "Clearing the connectivityAttemptTimer");
connectivityAttemptTimer.cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,22 @@ public class ExponentialBackoff {

public static final double DEFAULT_BACKOFF_FACTOR = 1.5;

/** Maximum backoff time in milliseconds */
public static final long DEFAULT_BACKOFF_MAX_DELAY_MS = 60 * 1000;

private final AsyncQueue queue;
private final TimerId timerId;
private final long initialDelayMs;
private final double backoffFactor;

private long maxDelayMs;
/** The maximum backoff time in milliseconds. */
private final long maxDelayMs;

/**
* The maximum backoff time used when calculating the next backoff. This value can be changed for
* a single backoffAndRun call, after which it resets to maxDelayMs.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

*/
private long nextMaxDelayMs;

private long currentBaseMs;
private long lastAttemptTime;
private DelayedTask timerTask;
Expand Down Expand Up @@ -71,6 +78,7 @@ public ExponentialBackoff(
this.initialDelayMs = initialDelayMs;
this.backoffFactor = backoffFactor;
this.maxDelayMs = maxDelayMs;
this.nextMaxDelayMs = maxDelayMs;
this.lastAttemptTime = new Date().getTime();

reset();
Expand Down Expand Up @@ -100,7 +108,7 @@ public void reset() {
* Resets the backoff delay to the maximum delay (e.g. for use after a RESOURCE_EXHAUSTED error).
*/
public void resetToMax() {
currentBaseMs = maxDelayMs;
currentBaseMs = nextMaxDelayMs;
}

/**
Expand All @@ -110,7 +118,7 @@ public void resetToMax() {
* @param newMax The temporary maximum delay to set.
*/
public void setTemporaryMaxDelay(long newMax) {
maxDelayMs = newMax;
nextMaxDelayMs = newMax;
}

/**
Expand Down Expand Up @@ -158,12 +166,12 @@ public void backoffAndRun(Runnable task) {
currentBaseMs = (long) (currentBaseMs * backoffFactor);
if (currentBaseMs < initialDelayMs) {
currentBaseMs = initialDelayMs;
} else if (currentBaseMs > maxDelayMs) {
currentBaseMs = maxDelayMs;
} else if (currentBaseMs > nextMaxDelayMs) {
currentBaseMs = nextMaxDelayMs;
}

// Reset max delay to the default.
maxDelayMs = DEFAULT_BACKOFF_MAX_DELAY_MS;
nextMaxDelayMs = maxDelayMs;
}

public void cancel() {
Expand Down