-
Notifications
You must be signed in to change notification settings - Fork 929
Add timeout to OnlineState tracking. #412
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e837136
Add timeout to OnlineState tracking.
46588e6
Add reminder to dispatch timeout onto AsyncQueue.
a85ac2d
Merge branch 'master' into mikelehen/onlinestate-timeout
18c838a
Addressed review Feedback and added a spec test.
e4819fe
Update changelog.
b5d412f
Tweak naming / comments based on CR feedback.
6722528
Merge branch 'master' into mikelehen/onlinestate-timeout
cd7e71e
Tweak timer names: Connection => ConnectionBackoff.
23cd42a
Merge branch 'master' into mikelehen/onlinestate-timeout
8925ae6
[AUTOMATED]: Prettier Code Styling
5c74573
Merge branch 'master' into mikelehen/onlinestate-timeout
de41073
comment tweak.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
/** | ||
* Copyright 2018 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { OnlineState } from '../core/types'; | ||
import * as log from '../util/log'; | ||
import { assert } from '../util/assert'; | ||
import { AsyncQueue, TimerId } from '../util/async_queue'; | ||
import { CancelablePromise } from '../util/promise'; | ||
|
||
const LOG_TAG = 'OnlineStateTracker'; | ||
|
||
// To deal with transient failures, we allow multiple stream attempts before | ||
// giving up and transitioning from OnlineState.Unknown to Offline. | ||
const MAX_WATCH_STREAM_FAILURES = 2; | ||
|
||
// To deal with stream attempts that don't succeed or fail in a timely manner, | ||
// we have a timeout for OnlineState to reach Online or Offline. | ||
// If the timeout is reached, we transition to Offline rather than waiting | ||
// indefinitely. | ||
const ONLINE_STATE_TIMEOUT_MS = 10 * 1000; | ||
|
||
/** | ||
* A component used by the RemoteStore to track the OnlineState (that is, | ||
* whether or not the client as a whole should be considered to be online or | ||
* offline), implementing the appropriate heuristics. | ||
* | ||
* In particular, when the client is trying to connect to the backend, we | ||
* allow up to MAX_WATCH_STREAM_FAILURES within ONLINE_STATE_TIMEOUT_MS for | ||
* a connection to succeed. If we have too many failures or the timeout elapses, | ||
* then we set the OnlineState to Offline, and the client will behave as if | ||
* it is offline (get()s will return cached data, etc.). | ||
*/ | ||
export class OnlineStateTracker { | ||
/** The current OnlineState. */ | ||
private state = OnlineState.Unknown; | ||
|
||
/** | ||
* A count of consecutive failures to open the stream. If it reaches the | ||
* maximum defined by MAX_WATCH_STREAM_FAILURES, we'll set the OnlineState to | ||
* Offline. | ||
*/ | ||
private watchStreamFailures = 0; | ||
|
||
/** | ||
* A timer that elapses after ONLINE_STATE_TIMEOUT_MS, at which point we | ||
* transition from OnlineState.Unknown to OnlineState.Offline without waiting | ||
* for the stream to actually fail (MAX_WATCH_STREAM_FAILURES times). | ||
*/ | ||
private onlineStateTimer: CancelablePromise<void> | null = null; | ||
|
||
/** | ||
* Whether the client should log a warning message if it fails to connect to | ||
* the backend (initially true, cleared after a successful stream, or if we've | ||
* logged the message already). | ||
*/ | ||
private shouldWarnClientIsOffline = true; | ||
|
||
constructor( | ||
private asyncQueue: AsyncQueue, | ||
private onlineStateHandler: (onlineState: OnlineState) => void | ||
) {} | ||
|
||
/** | ||
* Called by RemoteStore when a watch stream is started. | ||
* | ||
* It sets the OnlineState to Unknown and starts the onlineStateTimer | ||
* if necessary. | ||
*/ | ||
handleWatchStreamStart(): void { | ||
this.setAndBroadcast(OnlineState.Unknown); | ||
|
||
if (this.onlineStateTimer === null) { | ||
this.onlineStateTimer = this.asyncQueue.enqueueAfterDelay( | ||
TimerId.OnlineStateTimeout, | ||
ONLINE_STATE_TIMEOUT_MS, | ||
() => { | ||
this.onlineStateTimer = null; | ||
assert( | ||
this.state === OnlineState.Unknown, | ||
'Timer should be canceled if we transitioned to a different state.' | ||
); | ||
log.debug( | ||
LOG_TAG, | ||
`Watch stream didn't reach online or offline within ` + | ||
`${ONLINE_STATE_TIMEOUT_MS}ms. Considering client offline.` | ||
); | ||
this.logClientOfflineWarningIfNecessary(); | ||
this.setAndBroadcast(OnlineState.Offline); | ||
|
||
// NOTE: handleWatchStreamFailure() will continue to increment | ||
// watchStreamFailures even though we are already marked Offline, | ||
// but this is non-harmful. | ||
|
||
return Promise.resolve(); | ||
} | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Updates our OnlineState as appropriate after the watch stream reports a | ||
* failure. The first failure moves us to the 'Unknown' state. We then may | ||
* allow multiple failures (based on MAX_WATCH_STREAM_FAILURES) before we | ||
* actually transition to the 'Offline' state. | ||
*/ | ||
handleWatchStreamFailure(): void { | ||
if (this.state === OnlineState.Online) { | ||
this.setAndBroadcast(OnlineState.Unknown); | ||
} else { | ||
this.watchStreamFailures++; | ||
if (this.watchStreamFailures >= MAX_WATCH_STREAM_FAILURES) { | ||
this.clearOnlineStateTimer(); | ||
this.logClientOfflineWarningIfNecessary(); | ||
this.setAndBroadcast(OnlineState.Offline); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Explicitly sets the OnlineState to the specified state. | ||
* | ||
* Note that this resets our timers / failure counters, etc. used by our | ||
* Offline heuristics, so must not be used in place of | ||
* handleWatchStreamStart() and handleWatchStreamFailure(). | ||
*/ | ||
set(newState: OnlineState): void { | ||
this.clearOnlineStateTimer(); | ||
this.watchStreamFailures = 0; | ||
|
||
if (newState === OnlineState.Online) { | ||
// We've connected to watch at least once. Don't warn the developer | ||
// about being offline going forward. | ||
this.shouldWarnClientIsOffline = false; | ||
} | ||
|
||
this.setAndBroadcast(newState); | ||
} | ||
|
||
private setAndBroadcast(newState: OnlineState): void { | ||
if (newState !== this.state) { | ||
this.state = newState; | ||
this.onlineStateHandler(newState); | ||
} | ||
} | ||
|
||
private logClientOfflineWarningIfNecessary(): void { | ||
if (this.shouldWarnClientIsOffline) { | ||
log.error('Could not reach Firestore backend.'); | ||
this.shouldWarnClientIsOffline = false; | ||
} | ||
} | ||
|
||
private clearOnlineStateTimer(): void { | ||
if (this.onlineStateTimer !== null) { | ||
this.onlineStateTimer.cancel(); | ||
this.onlineStateTimer = null; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 can't think of any way it would happen but just in case: are there any interactions between this class and the idle timeout timers?
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.
There shouldn't be since this timer goes away once you're connected. And the idle timers start once you're connected... And with the improved testing, if both timers were active at once, runDelayedOperationsEarly() would run both so we'd be exercising them together, at least in theory...