Skip to content

Commit

Permalink
core, changes: Support infinite retry for local connections and recov…
Browse files Browse the repository at this point in the history
…er broken changes feed.
  • Loading branch information
abutcher-gh committed Jun 11, 2019
1 parent b49af6d commit cf527ff
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
16 changes: 12 additions & 4 deletions lib/cradle.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ cradle.options = {
retries: 0,
retryTimeout: 10e3,
forceSave: true,
headers: {}
headers: {},
forceReconnect: false,
};

cradle.setup = function (settings) {
Expand Down Expand Up @@ -155,13 +156,14 @@ cradle.Connection.prototype.rawRequest = function (options, callback) {
options.query[k] = String(options.query[k]);
}
}
options.path += '?' + querystring.stringify(options.query);
options.uri = this._url(options.path + '?' + querystring.stringify(options.query));
}
else {
options.uri = this._url(options.path);
}

options.headers['Connection'] = options.headers['Connection'] || 'keep-alive';
options.agent = this.agent;
options.uri = this._url(options.path);
delete options.path;

return request(options, callback || function () { });
};
Expand Down Expand Up @@ -214,6 +216,12 @@ cradle.Connection.prototype.request = function (options, callback) {
return this.rawRequest(options, function _onResponse(err, res, body) {
attempts++;
if (err) {
if (self.options.forceReconnect && String(err.code).startsWith('ECONN')) {
return setTimeout(
self.rawRequest.bind(self, options, _onResponse),
self.options.retryTimeout
);
}
if (self.options.retries &&
(!options.method || options.method.toLowerCase() === 'get' || options.body) &&
String(err.code).indexOf('ECONN') === 0 && attempts <= self.options.retries
Expand Down
16 changes: 15 additions & 1 deletion lib/cradle/database/changes.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,21 @@ Database.prototype.changes = function (options, callback) {

response.emit.apply(response, ['data'].concat(Array.prototype.slice.call(arguments)));
});


var self = this;
// Keep a consistent object for return to the client, even if
// this feed is restarted due to error.
feed.on('error', function (err) {
if (feed.dead && options.follow !== false) {
console.error(self.name, 'ERROR: Cradle changes feed died, restarting', err.message || err);
setTimeout(function() {
console.error(self.name, 'RECOVERY: Restarting feed that died with', err.message || err);
feed.restart();
feed.emit('recover', err);
}, 1000);
}
});

if (options.follow !== false) {
feed.follow();
}
Expand Down

0 comments on commit cf527ff

Please sign in to comment.