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

retry confirm errors #68

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
29 changes: 20 additions & 9 deletions lib/feed.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,27 +133,24 @@ Feed.prototype.confirm = function confirm_feed() {
clearTimeout(timeout_id);

if(er)
return self.die(er);
return self.confirm_failed(er);

var db;
try {
db = JSON.parse(body)
} catch(json_er) {
return self.emit('error', json_er)
return self.confirm_failed(json_er);
}

if(!self.is_db_updates && !self.dead && (!db.db_name || !db.instance_start_time))
return self.emit('error', new Error('Bad DB response: ' + body));
return self.confirm_failed(new Error('Bad DB response: ' + body));

if(self.is_db_updates && !self.dead && !db.couchdb)
return self.emit('error', new Error('Bad server response: ' + body));
return self.confirm_failed(new Error('Bad server response: ' + body));

if (!self.is_db_updates)
self.original_db_seq = db.update_seq

self.log.debug('Confirmed ' + endpoint + ': ' + self.db_safe);
self.emit('confirm', db);

if(self.since == 'now') {
self.log.debug('Query since "now" is the same as query since -1')
self.since = -1
Expand All @@ -164,12 +161,17 @@ Feed.prototype.confirm = function confirm_feed() {
self.since = db.update_seq
} else if(self.since < 0) {
if(isNaN(db.update_seq))
return self.emit('error', new Error('DB requires specific id in "since"'));
return self.confirm_failed(new Error('DB requires specific id in "since"'));

self.log.debug('Query since '+self.since+' will start at ' + (db.update_seq + self.since + 1))
self.since = db.update_seq + self.since + 1
}

self.log.debug('Confirmed ' + endpoint + ': ' + self.db_safe);
self.confirmed = true;
self.retry_fn = self.query;
self.emit('confirm', db);

// If the next change would come after the current update_seq, just fake a catchup event now.
if(self.original_db_seq == self.since) {
self.caught_up = true
Expand All @@ -180,6 +182,15 @@ Feed.prototype.confirm = function confirm_feed() {
}
}

Feed.prototype.confirm_failed = function(err) {
var self = this;

self.emit('confirm_failed', err);

self.retry_fn = self.confirm;
self.retry();
}

Feed.prototype.query = function query_feed() {
var self = this;

Expand Down Expand Up @@ -489,7 +500,7 @@ Feed.prototype.retry = function retry() {
self.log.debug('Retry since=' + self.since + ' after ' + self.retry_delay + 'ms ')
self.emit('retry', {since:self.since, after:self.retry_delay, db:self.db_safe});

self.retry_timer = setTimeout(function() { self.query() }, self.retry_delay);
self.retry_timer = setTimeout(function() { self.retry_fn() }, self.retry_delay);

var max_retry_ms = self.max_retry_seconds * 1000;
self.retry_delay *= 2;
Expand Down