Skip to content
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

Only emit CATCHUP if recovering from conn error #727

Merged
merged 2 commits into from
Sep 7, 2018
Merged
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
22 changes: 15 additions & 7 deletions src/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -779,8 +779,12 @@ SyncApi.prototype._onSyncError = function(err, syncOptions) {
// erroneous. We set the state to 'reconnecting'
// instead, so that clients can observe this state
// if they wish.
this._startKeepAlives().then(() => {
if (this.getSyncState() === 'ERROR') {
this._startKeepAlives().then((connDidFail) => {
// Only emit CATCHUP if we detected a connectivity error: if we didn't,
// it's quite likely the sync will fail again for the same reason and we
// want to stay in ERROR rather than keep flip-flopping between ERROR
// and CATCHUP.
if (connDidFail && this.getSyncState() === 'ERROR') {
this._updateSyncState("CATCHUP", {
oldSyncToken: null,
nextSyncToken: null,
Expand Down Expand Up @@ -1215,13 +1219,16 @@ SyncApi.prototype._startKeepAlives = function(delay) {
*
* On failure, schedules a call back to itself. On success, resolves
* this._connectionReturnedDefer.
*
* @param {bool} connDidFail True if a connectivity failure has been detected. Optional.
*/
SyncApi.prototype._pokeKeepAlive = function() {
SyncApi.prototype._pokeKeepAlive = function(connDidFail) {
if (connDidFail === undefined) connDidFail = false;
const self = this;
function success() {
clearTimeout(self._keepAliveTimer);
if (self._connectionReturnedDefer) {
self._connectionReturnedDefer.resolve();
self._connectionReturnedDefer.resolve(connDidFail);
self._connectionReturnedDefer = null;
}
}
Expand All @@ -1238,24 +1245,25 @@ SyncApi.prototype._pokeKeepAlive = function() {
).done(function() {
success();
}, function(err) {
if (err.httpStatus == 400) {
if (err.httpStatus == 400 || err.httpStatus == 404) {
// treat this as a success because the server probably just doesn't
// support /versions: point is, we're getting a response.
// We wait a short time though, just in case somehow the server
// is in a mode where it 400s /versions responses and sync etc.
// responses fail, this will mean we don't hammer in a loop.
self._keepAliveTimer = setTimeout(success, 2000);
} else {
connDidFail = true;
bwindels marked this conversation as resolved.
Show resolved Hide resolved
self._keepAliveTimer = setTimeout(
self._pokeKeepAlive.bind(self),
self._pokeKeepAlive.bind(self, connDidFail),
5000 + Math.floor(Math.random() * 5000),
);
// A keepalive has failed, so we emit the
// error state (whether or not this is the
// first failure).
// Note we do this after setting the timer:
// this lets the unit tests advance the mock
// clock when the get the error.
// clock when they get the error.
self._updateSyncState("ERROR", { error: err });
}
});
Expand Down