-
Notifications
You must be signed in to change notification settings - Fork 615
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
Changes from 18 commits
11ca8a5
448311d
403f91f
00568be
c865827
d714378
55e8f46
d202893
411ed9d
1519a40
5bce0a0
ba515b7
110fdc3
c9cafd5
b77e02e
31d0ad8
8b2ad17
af9ed48
1cca6a4
86b4733
873d83d
d321316
a5dba09
51fac1c
595046f
a3fc304
36e448f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,8 @@ | |
import com.google.firebase.firestore.core.DatabaseInfo; | ||
import com.google.firebase.firestore.model.DatabaseId; | ||
import com.google.firebase.firestore.util.AsyncQueue; | ||
import com.google.firebase.firestore.util.AsyncQueue.DelayedTask; | ||
import com.google.firebase.firestore.util.AsyncQueue.TimerId; | ||
import com.google.firebase.firestore.util.Util; | ||
import io.grpc.ClientCall; | ||
import io.grpc.ForwardingClientCall; | ||
|
@@ -53,6 +55,11 @@ class FirestoreChannel { | |
private static final String X_GOOG_API_CLIENT_VALUE = | ||
"gl-java/ fire/" + BuildConfig.VERSION_NAME + " grpc/"; | ||
|
||
// This timeout is used when attempting to establish a connection. If a connection attempt | ||
// does not succeed in time, we close the stream and restart the connection, rather than having | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The timer callback does not seem to "close the stream" in any way that I can tell. Based on comments in markChannelIdle, the first stream continues? Does this actually close the stream, or is this a side effect? From what I can tell, the connectivityAttemptTimer merely starts a new clientCall (through a ~recursive invocation of runBidiStreamingRpc). What prevents the observer from getting callbacks from the old stream once that fails ~2 minutes later? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you're looking at an old version of the PR? 😬 This code is gone in the latest version, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's the outdated version. The new one can be found here. |
||
// it hang indefinitely. | ||
private static final int CONNECTIVITY_ATTEMPT_TIMEOUT_MS = 15 * 1000; | ||
|
||
/** The async worker queue that is used to dispatch events. */ | ||
private final AsyncQueue asyncQueue; | ||
|
||
|
@@ -92,6 +99,15 @@ public void shutdown() { | |
callProvider.shutdown(); | ||
} | ||
|
||
/** | ||
* Marks the underlying gRPC channel as idle. This allows on-going RPCs to continue, but the next | ||
* RPC on the channel will trigger the creation of a new connection. This method is primarily used | ||
* to reset the underlying connection. | ||
*/ | ||
public void markChannelIdle() { | ||
this.callProvider.markChannelIdle(); | ||
} | ||
|
||
/** | ||
* Creates and starts a new bi-directional streaming RPC. The stream cannot accept message before | ||
* the observer's `onOpen()` callback is invoked. | ||
|
@@ -154,6 +170,18 @@ public void onReady() { | |
call[0].request(1); | ||
}); | ||
|
||
DelayedTask connectivityAttemptTimer = | ||
asyncQueue.enqueueAfterDelay( | ||
TimerId.CONNECTIVITY_ATTEMPT_TIMER, | ||
CONNECTIVITY_ATTEMPT_TIMEOUT_MS, | ||
() -> { | ||
// Reset the underlying connection and restart the stream. | ||
callProvider.clearConnectivityTimer(); | ||
markChannelIdle(); | ||
runBidiStreamingRpc(method, observer); | ||
}); | ||
callProvider.setConnectivityAttemptTimer(connectivityAttemptTimer); | ||
|
||
return new ForwardingClientCall<ReqT, RespT>() { | ||
@Override | ||
protected ClientCall<ReqT, RespT> delegate() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,8 +36,8 @@ public class ExponentialBackoff { | |
private final TimerId timerId; | ||
private final long initialDelayMs; | ||
private final double backoffFactor; | ||
private final long maxDelayMs; | ||
|
||
private long maxDelayMs; | ||
private long currentBaseMs; | ||
private long lastAttemptTime; | ||
private DelayedTask timerTask; | ||
|
@@ -103,6 +103,16 @@ public void resetToMax() { | |
currentBaseMs = maxDelayMs; | ||
} | ||
|
||
/** | ||
* Set the backoff's maximum delay for only the next call to backoffAndRun, after which the delay | ||
* will be reset to maxDelayMs. | ||
* | ||
* @param newMax The temporary maximum delay to set. | ||
*/ | ||
public void setTemporaryMaxDelay(long newMax) { | ||
maxDelayMs = newMax; | ||
} | ||
|
||
/** | ||
* Waits for currentDelayMs, increases the delay and runs the specified task. If there was a | ||
* pending backoff task waiting to run already, it will be canceled. | ||
|
@@ -151,6 +161,9 @@ public void backoffAndRun(Runnable task) { | |
} else if (currentBaseMs > maxDelayMs) { | ||
currentBaseMs = maxDelayMs; | ||
} | ||
|
||
// Reset max delay to the default. | ||
maxDelayMs = DEFAULT_BACKOFF_MAX_DELAY_MS; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This ignored the comments I made on the last version of this PR. The max delay was configured in the constructor and may not actually be Instead, save two max delay fields in the constructor. One should be final, as maxDelayMs is today and it should be used here as the value to which to reset. The second value should be the one you actually bounce around based on which kind of error is in effect. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done (attempted). |
||
} | ||
|
||
public void cancel() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"when reconnecting" doesn't really do anything to distinguish this from the existing BACKOFF_MAX_DELAY_MS which is also used when reconnecting.
I think the intention is that this max backoff delay is used when we're 100% sure that the connection failed on the client-side and didn't even reach the server. In practice, I think the only case we can be sure of that is DNS failures (see my other comment about ConnectException). If that's the case, then maybe we can call this DNS_FAILURE_BACKOFF_MAX_DELAY_MS or something...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about
CLIENT_NETWORK_FAILURE_BACKOFF_MAX_DELAY_MS
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just noticed that BACKOFF should maybe be first (to group this with the other backoff-related constants). So:
BACKOFF_CLIENT_NETWORK_FAILURE_MAX_DELAY_MS
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW- I don't feel strongly. Just trying to figure out how to make the name parse more sensibly...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to Michael's suggestion.