Skip to content

Commit

Permalink
Don't endlessly re-request features on connectivity issues
Browse files Browse the repository at this point in the history
If, for whatever reason, the request to fetch features data fails,
simply retry a little later (with a randomised exponential backoff).
This commit uses the [retry module](https://www.npmjs.com/package/retry)
to provide the backoff logic.

N.B. If we fail 10 times (over a period spanning between ~15 and ~30
minutes, using the defaults from the retry module) then we will simply
stop looking for new features data. There's no point burning client CPU
forever in the hope that the server reappears.
  • Loading branch information
nickstenning committed Oct 13, 2015
1 parent 73d09e0 commit e227837
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
32 changes: 21 additions & 11 deletions h/static/scripts/features.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,38 @@
*/
'use strict';

var retry = require('retry');

var CACHE_TTL = 5 * 60 * 1000; // 5 minutes

function features ($document, $http, $log) {
var cache = null;
var pending = false;
var operation = null;
var featuresUrl = new URL('/app/features', $document.prop('baseURI')).href;

function fetch() {
// Short-circuit if a fetch is already in progress...
if (pending) {
if (operation) {
return;
}
pending = true;
$http.get(featuresUrl)
.success(function(data) {
operation = retry.operation({retries: 10, randomize: true});

function success(data) {
cache = [Date.now(), data];
})
.error(function() {
$log.warn('features service: failed to load features data');
})
.finally(function() {
pending = false;
operation = null;
}

function failure(data, status) {
if (!operation.retry('failed to load - remote status was ' + status)) {
// All retries have failed, and we will now stop polling the endpoint.
$log.error('features service:', operation.mainError());
}
}

operation.attempt(function () {
$http.get(featuresUrl)
.success(success)
.error(failure);
});
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"node-uuid": "^1.4.3",
"postcss": "^5.0.6",
"raf": "^3.1.0",
"retry": "^0.8.0",
"scroll-into-view": "^1.3.1",
"showdown": "^1.2.1",
"uglify-js": "^2.4.14",
Expand Down

0 comments on commit e227837

Please sign in to comment.