Skip to content

Commit 5050675

Browse files
author
Michael Lehenbauer
committed
Tweak naming / comments based on CR feedback.
1 parent e4819fe commit 5050675

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

packages/firestore/src/remote/online_state_tracker.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,22 @@ import { CancelablePromise } from '../util/promise';
2323
const LOG_TAG = 'OnlineStateTracker';
2424

2525
// To deal with transient failures, we allow multiple stream attempts before
26-
// giving up and transitioning to Offline.
26+
// giving up and transitioning from OnlineState.Unknown to Offline.
2727
const MAX_WATCH_STREAM_FAILURES = 2;
2828

2929
// To deal with stream attempts that don't succeed or fail in a timely manner,
30-
// we have a maximum timeout we'll wait for the stream to either succeed or fail
31-
// MAX_WATCH_STREAM_FAILURES times, else we revert to OnlineState.Offline.
32-
const MAX_WATCH_STREAM_TIMEOUT_MS = 10 * 1000;
30+
// we have a timeout for the stream to either succeed or fail
31+
// (MAX_WATCH_STREAM_FAILURES times). If the timeout is reached, we transition
32+
// from OnlineState.Unknown to Offline, rather than waiting indefinitely.
33+
const WATCH_STREAM_TIMEOUT_MS = 10 * 1000;
3334

3435
/**
3536
* A component used by the RemoteStore to track the OnlineState (that is,
3637
* whether or not the client as a whole should be considered to be online or
3738
* offline), implementing the appropriate heuristics.
3839
*
3940
* In particular, when the client is trying to connect to the backend, we
40-
* allow up to MAX_WATCH_STREAM_FAILURES within MAX_WATCH_STREAM_TIMEOUT_MS for
41+
* allow up to MAX_WATCH_STREAM_FAILURES within WATCH_STREAM_TIMEOUT_MS for
4142
* a connection to succeed. If we have too many failures or the timeout elapses,
4243
* then we set the OnlineState to Offline, and the client will behave as if
4344
* it is offline (get()s will return cached data, etc.).
@@ -54,11 +55,11 @@ export class OnlineStateTracker {
5455
private watchStreamFailures = 0;
5556

5657
/**
57-
* A timer that elapses after MAX_WATCH_STREAM_TIMEOUT_MS, at which point we
58-
* revert to OnlineState.Offline without waiting for the stream to actually
59-
* fail (MAX_WATCH_STREAM_FAILURES times).
58+
* A timer that elapses after WATCH_STREAM_TIMEOUT_MS, at which point we
59+
* transition from OnlineState.Unknown to OnlineState.Offline without waiting
60+
* for the stream to actually fail (MAX_WATCH_STREAM_FAILURES times).
6061
*/
61-
private watchStreamTimerPromise: CancelablePromise<void> | null = null;
62+
private watchStreamTimeout: CancelablePromise<void> | null = null;
6263

6364
/**
6465
* Whether the client should log a warning message if it fails to connect to
@@ -75,26 +76,26 @@ export class OnlineStateTracker {
7576
/**
7677
* Called by RemoteStore when a watch stream is started.
7778
*
78-
* It sets the OnlineState to Unknown and starts a MAX_WATCH_STREAM_TIMEOUT_MS
79+
* It sets the OnlineState to Unknown and starts a WATCH_STREAM_TIMEOUT_MS
7980
* timer if necessary.
8081
*/
8182
handleWatchStreamStart(): void {
8283
this.setAndBroadcast(OnlineState.Unknown);
8384

84-
if (this.watchStreamTimerPromise === null) {
85-
this.watchStreamTimerPromise = this.asyncQueue.enqueueAfterDelay(
85+
if (this.watchStreamTimeout === null) {
86+
this.watchStreamTimeout = this.asyncQueue.enqueueAfterDelay(
8687
TimerId.OnlineStateTimeout,
87-
MAX_WATCH_STREAM_TIMEOUT_MS,
88+
WATCH_STREAM_TIMEOUT_MS,
8889
() => {
89-
this.watchStreamTimerPromise = null;
90+
this.watchStreamTimeout = null;
9091
assert(
9192
this.state === OnlineState.Unknown,
9293
'Timer should be canceled if we transitioned to a different state.'
9394
);
9495
log.debug(
9596
LOG_TAG,
9697
`Watch stream didn't reach online or offline within ` +
97-
`${MAX_WATCH_STREAM_TIMEOUT_MS}ms. Considering client offline.`
98+
`${WATCH_STREAM_TIMEOUT_MS}ms. Considering client offline.`
9899
);
99100
this.logClientOfflineWarningIfNecessary();
100101
this.setAndBroadcast(OnlineState.Offline);
@@ -158,9 +159,9 @@ export class OnlineStateTracker {
158159
}
159160

160161
private clearWatchStreamTimer(): void {
161-
if (this.watchStreamTimerPromise !== null) {
162-
this.watchStreamTimerPromise.cancel();
163-
this.watchStreamTimerPromise = null;
162+
if (this.watchStreamTimeout !== null) {
163+
this.watchStreamTimeout.cancel();
164+
this.watchStreamTimeout = null;
164165
}
165166
}
166167
}

packages/firestore/src/util/async_queue.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,25 @@ type TimerHandle = any;
3131
* The string values are used when encoding these timer IDs in JSON spec tests.
3232
*/
3333
export enum TimerId {
34-
// All can be used with runDelayedOperationsEarly() to run all timers.
34+
/** All can be used with runDelayedOperationsEarly() to run all timers. */
3535
All = 'all',
36+
37+
/**
38+
* The following 4 timers are used in persistent_stream.ts for the listen and
39+
* write streams. The "Idle" timer is used to close the stream due to
40+
* inactivity. The "Connection" timer is used to start a stream once the
41+
* appropriate backoff delay has elapsed.
42+
*/
3643
ListenStreamIdle = 'listen_stream_idle',
3744
ListenStreamConnection = 'listen_stream_connection',
3845
WriteStreamIdle = 'write_stream_idle',
3946
WriteStreamConnection = 'write_stream_connection',
47+
48+
/**
49+
* A timer used in online_state_tracker.ts to transition from
50+
* OnlineState.Unknown to Offline after a set timeout, rather than waiting
51+
* indefinitely for success or failure.
52+
*/
4053
OnlineStateTimeout = 'online_state_timeout'
4154
}
4255

0 commit comments

Comments
 (0)