Commit db7af2a0 authored by Robert Knight's avatar Robert Knight

Merge pull request #2612 from hypothesis/exponential-backoff-features

Don't endlessly re-request features on connectivity issues
parents 68b3c4fd d0d19355
...@@ -21,28 +21,38 @@ ...@@ -21,28 +21,38 @@
*/ */
'use strict'; 'use strict';
var retry = require('retry');
var CACHE_TTL = 5 * 60 * 1000; // 5 minutes var CACHE_TTL = 5 * 60 * 1000; // 5 minutes
function features ($document, $http, $log) { function features ($document, $http, $log) {
var cache = null; var cache = null;
var pending = false; var operation = null;
var featuresUrl = new URL('/app/features', $document.prop('baseURI')).href; var featuresUrl = new URL('/app/features', $document.prop('baseURI')).href;
function fetch() { function fetch() {
// Short-circuit if a fetch is already in progress... // Short-circuit if a fetch is already in progress...
if (pending) { if (operation) {
return; return;
} }
pending = true; operation = retry.operation({retries: 10, randomize: true});
$http.get(featuresUrl)
.success(function(data) { function success(data) {
cache = [Date.now(), data]; cache = [Date.now(), data];
}) operation = null;
.error(function() { }
$log.warn('features service: failed to load features data');
}) function failure(data, status) {
.finally(function() { if (!operation.retry('failed to load - remote status was ' + status)) {
pending = false; // 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);
}); });
} }
......
...@@ -42,6 +42,7 @@ ...@@ -42,6 +42,7 @@
"node-uuid": "^1.4.3", "node-uuid": "^1.4.3",
"postcss": "^5.0.6", "postcss": "^5.0.6",
"raf": "^3.1.0", "raf": "^3.1.0",
"retry": "^0.8.0",
"scroll-into-view": "^1.3.1", "scroll-into-view": "^1.3.1",
"showdown": "^1.2.1", "showdown": "^1.2.1",
"uglify-js": "^2.4.14", "uglify-js": "^2.4.14",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment