Commit d0d19355 authored by Nick Stenning's avatar Nick Stenning

Don't endlessly re-request features on connectivity issues

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.

Fixes #2547.
parent 68b3c4fd
......@@ -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);
});
}
......
......@@ -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",
......
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