Skip to content

Fix issue that could cause offline get()s to wait up to 10 seconds. #592

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 2 commits into from
Mar 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions packages/firestore/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Unreleased
- [fixed] Fixed a regression in the Firebase JS release 4.11.0 that could
cause get() requests made while offline to be delayed by up to 10
seconds (rather than returning from cache immediately).

# 0.3.6
- [fixed] Fixed a regression in the Firebase JS release 4.11.0 that could
cause a crash if a user signs out while the client is offline, resulting in
an error of "Attempted to schedule multiple operations with timer id
Expand Down
20 changes: 15 additions & 5 deletions packages/firestore/src/remote/online_state_tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,20 @@ export class OnlineStateTracker {
) {}

/**
* Called by RemoteStore when a watch stream is started.
* Called by RemoteStore when a watch stream is started (including on each
* backoff attempt).
*
* It sets the OnlineState to Unknown and starts the onlineStateTimer
* if necessary.
* If this is the first attempt, it sets the OnlineState to Unknown and starts
* the onlineStateTimer.
*/
handleWatchStreamStart(): void {
this.setAndBroadcast(OnlineState.Unknown);
if (this.watchStreamFailures === 0) {
this.setAndBroadcast(OnlineState.Unknown);

if (this.onlineStateTimer === null) {
assert(
this.onlineStateTimer === null,
`onlineStateTimer shouldn't be started yet`
);
this.onlineStateTimer = this.asyncQueue.enqueueAfterDelay(
TimerId.OnlineStateTimeout,
ONLINE_STATE_TIMEOUT_MS,
Expand Down Expand Up @@ -119,6 +124,11 @@ export class OnlineStateTracker {
handleWatchStreamFailure(): void {
if (this.state === OnlineState.Online) {
this.setAndBroadcast(OnlineState.Unknown);

// To get to OnlineState.Online, set() must have been called which would
// have reset our heuristics.
assert(this.watchStreamFailures === 0, 'watchStreamFailures must be 0');
assert(this.onlineStateTimer === null, 'onlineStateTimer must be null');
} else {
this.watchStreamFailures++;
if (this.watchStreamFailures >= MAX_WATCH_STREAM_FAILURES) {
Expand Down
43 changes: 43 additions & 0 deletions packages/firestore/test/unit/specs/offline_spec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,47 @@ describeSpec('Offline:', [], () => {
})
);
});

specTest(
'New queries return immediately with fromCache=true when offline due to ' +
'stream failures.',
[],
() => {
const query1 = Query.atPath(path('collection'));
const query2 = Query.atPath(path('collection2'));
return (
spec()
.userListens(query1)
// 2 Failures should mark the client offline and trigger an empty
// fromCache event.
.watchStreamCloses(Code.UNAVAILABLE)
.watchStreamCloses(Code.UNAVAILABLE)
.expectEvents(query1, { fromCache: true })

// A new query should immediately return from cache.
.userListens(query2)
.expectEvents(query2, { fromCache: true })
);
}
);

specTest(
'New queries return immediately with fromCache=true when offline due to ' +
'OnlineState timeout.',
[],
() => {
const query1 = Query.atPath(path('collection'));
const query2 = Query.atPath(path('collection2'));
return (
spec()
.userListens(query1)
.runTimer(TimerId.OnlineStateTimeout)
.expectEvents(query1, { fromCache: true })

// A new query should immediately return from cache.
.userListens(query2)
.expectEvents(query2, { fromCache: true })
);
}
);
});