-
Notifications
You must be signed in to change notification settings - Fork 150
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
Retrying on any error from the Watch stream #70
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0002a39
Retrying on any error for Watch stream.
schmidt-sebastian 265635b
Fixing comments
schmidt-sebastian 5365a9d
Merge branch 'master' into mrschmidt-backoff
schmidt-sebastian 9cb8dee
Adding tests for backoff.js
schmidt-sebastian 3ce42fb
Merge remote-tracking branch 'origin/mrschmidt-backoff' into mrschmid…
schmidt-sebastian a8a605c
Addressing comments
schmidt-sebastian 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 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,235 @@ | ||
/*! | ||
* Copyright 2017 Google Inc. All Rights Reserved. | ||
* | ||
* 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. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/*! | ||
* Injected. | ||
* | ||
* @see Firestore | ||
*/ | ||
let Firestore; | ||
|
||
/* | ||
* @module firestore/backoff | ||
* @private | ||
* | ||
* Contains backoff logic to facilitate RPC error handling. This class derives | ||
* its implementation from the Firestore Mobile Web Client. | ||
* | ||
* @see https://github.com/firebase/firebase-js-sdk/blob/master/packages/firestore/src/remote/backoff.ts | ||
*/ | ||
|
||
/*! | ||
* The default initial backoff time in milliseconds after an error. | ||
* Set to 1s according to https://cloud.google.com/apis/design/errors. | ||
*/ | ||
const DEFAULT_BACKOFF_INITIAL_DELAY_MS = 1000; | ||
|
||
/*! | ||
* The default maximum backoff time in milliseconds. | ||
*/ | ||
const DEFAULT_BACKOFF_MAX_DELAY_MS = 60 * 1000; | ||
|
||
/*! | ||
* The default factor to increase the backup by after each failed attempt. | ||
*/ | ||
const DEFAULT_BACKOFF_FACTOR = 1.5; | ||
|
||
/*! | ||
* The default jitter to distribute the backoff attempts by (0 means no | ||
* randomization, 1.0 means +/-50% randomization). | ||
*/ | ||
const DEFAULT_JITTER_FACTOR = 1.0; | ||
|
||
/*! | ||
* The timeout handler used by `ExponentialBackoff`. | ||
*/ | ||
let delayExecution = setTimeout; | ||
|
||
/** | ||
* Allows overriding of the timeout handler used by the exponential backoff | ||
* implementation. If not invoked, we default to `setTimeout()`. | ||
* | ||
* Used only in testing. | ||
* | ||
* @private | ||
* @param {function} handler A handler than matches the API of `setTimeout()`. | ||
*/ | ||
function setTimeoutHandler(handler) { | ||
delayExecution = handler; | ||
} | ||
|
||
/** | ||
* A helper for running delayed tasks following an exponential backoff curve | ||
* between attempts. | ||
* | ||
* Each delay is made up of a "base" delay which follows the exponential | ||
* backoff curve, and a "jitter" (+/- 50% by default) that is calculated and | ||
* added to the base delay. This prevents clients from accidentally | ||
* synchronizing their delays causing spikes of load to the backend. | ||
* | ||
* @private | ||
*/ | ||
class ExponentialBackoff { | ||
/** | ||
* @param {number=} options.initialDelayMs Optional override for the initial | ||
* retry delay. | ||
* @param {number=} options.backoffFactor Optional override for the | ||
* exponential backoff factor. | ||
* @param {number=} options.maxDelayMs Optional override for the maximum | ||
* retry delay. | ||
* @param {number=} options.jitterFactor Optional override to control the | ||
* jitter factor by which to randomize attempts (0 means no randomization, | ||
* 1.0 means +/-50% randomization). It is suggested not to exceed this range. | ||
*/ | ||
constructor(options) { | ||
options = options || {}; | ||
|
||
/** | ||
* The initial delay (used as the base delay on the first retry attempt). | ||
* Note that jitter will still be applied, so the actual delay could be as | ||
* little as 0.5*initialDelayMs (based on a jitter factor of 1.0). | ||
* | ||
* @type {number} | ||
* @private | ||
*/ | ||
this._initialDelayMs = | ||
options.initialDelayMs !== undefined | ||
? options.initialDelayMs | ||
: DEFAULT_BACKOFF_INITIAL_DELAY_MS; | ||
|
||
/** | ||
* The multiplier to use to determine the extended base delay after each | ||
* attempt. | ||
* @type {number} | ||
* @private | ||
*/ | ||
this._backoffFactor = | ||
options.backoffFactor !== undefined | ||
? options.backoffFactor | ||
: DEFAULT_BACKOFF_FACTOR; | ||
|
||
/** | ||
* The maximum base delay after which no further backoff is performed. | ||
* Note that jitter will still be applied, so the actual delay could be as | ||
* much as 1.5*maxDelayMs (based on a jitter factor of 1.0). | ||
* | ||
* @type {number} | ||
* @private | ||
*/ | ||
this._maxDelayMs = | ||
options.maxDelayMs !== undefined | ||
? options.maxDelayMs | ||
: DEFAULT_BACKOFF_MAX_DELAY_MS; | ||
|
||
/** | ||
* The jitter factor that controls the random distribution of the backoff | ||
* points. | ||
* | ||
* @type {number} | ||
* @private | ||
*/ | ||
this._jitterFactor = | ||
options.jitterFactor !== undefined | ||
? options.jitterFactor | ||
: DEFAULT_JITTER_FACTOR; | ||
|
||
/** | ||
* The backoff delay of the current attempt. | ||
* @type {number} | ||
* @private | ||
*/ | ||
this._currentBaseMs = 0; | ||
} | ||
|
||
/** | ||
* Resets the backoff delay. | ||
* | ||
* The very next backoffAndWait() will have no delay. If it is called again | ||
* (i.e. due to an error), initialDelayMs (plus jitter) will be used, and | ||
* subsequent ones will increase according to the backoffFactor. | ||
* | ||
* @private | ||
*/ | ||
reset() { | ||
this._currentBaseMs = 0; | ||
} | ||
|
||
/** | ||
* Resets the backoff delay to the maximum delay (e.g. for use after a | ||
* RESOURCE_EXHAUSTED error). | ||
* | ||
* @private | ||
*/ | ||
resetToMax() { | ||
this._currentBaseMs = this._maxDelayMs; | ||
} | ||
|
||
/** | ||
* Returns a promise that resolves after currentDelayMs, and increases the | ||
* delay for any subsequent attempts. | ||
* | ||
* @private | ||
* @return {Promise.<void>} A Promise that resolves when the current delay | ||
* elapsed. | ||
*/ | ||
backoffAndWait() { | ||
// First schedule using the current base (which may be 0 and should be | ||
// honored as such). | ||
const delayWithJitterMs = this._currentBaseMs + this._jitterDelayMs(); | ||
if (this._currentBaseMs > 0) { | ||
Firestore.log( | ||
'ExponentialBackoff.backoffAndWait', | ||
`Backing off for ${delayWithJitterMs} ms ` + | ||
`(base delay: ${this._currentBaseMs} ms)` | ||
); | ||
} | ||
|
||
// Apply backoff factor to determine next delay and ensure it is within | ||
// bounds. | ||
this._currentBaseMs *= this._backoffFactor; | ||
if (this._currentBaseMs < this._initialDelayMs) { | ||
this._currentBaseMs = this._initialDelayMs; | ||
} | ||
if (this._currentBaseMs > this._maxDelayMs) { | ||
this._currentBaseMs = this._maxDelayMs; | ||
} | ||
|
||
return new Promise(resolve => { | ||
delayExecution(resolve, delayWithJitterMs); | ||
}); | ||
} | ||
|
||
/** | ||
* Returns a randomized "jitter" delay based on the current base and jitter | ||
* factor. | ||
* | ||
* @private | ||
* @returns {number} The jitter to apply based on the current delay. | ||
*/ | ||
_jitterDelayMs() { | ||
return (Math.random() - 0.5) * this._jitterFactor * this._currentBaseMs; | ||
} | ||
} | ||
|
||
module.exports = FirestoreType => { | ||
Firestore = FirestoreType; | ||
return { | ||
ExponentialBackoff, | ||
setTimeoutHandler, | ||
}; | ||
}; |
This file contains 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.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.