Skip to content

Commit

Permalink
Merge pull request #269 from prey/overdue-406
Browse files Browse the repository at this point in the history
Cover overdue 406 code case
  • Loading branch information
javo authored Feb 21, 2017
2 parents 81f9483 + 96c6fc3 commit eee4bd2
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions lib/agent/plugins/control-panel/long-polling/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ var request = function(re_schedule) {
api_key = keys.get().api;

// used for benchmarking and request interval setup
var request_start,
response_delay;
var status_code,
request_start,
response_delay,
resp_timeout;

if (!keys.get().device) return propagate_error(errors.get('NO_DEVICE_KEY'));

Expand Down Expand Up @@ -77,25 +79,31 @@ var request = function(re_schedule) {
});

request.request.on('response', function(res) {
if (res.statusCode !== 200) {
status_code = res.statusCode;
if (status_code !== 200) {
propagate_error('Invalid response received with status code ' + res.statusCode);
resp_timeout = 30000;

if (status_code == 406) resp_timeout = 60000;
clear_current_request();
check_for_reschedule();

} else {
response_delay = (new Date() - request_start) / 1000;
resp_timeout = 5000;
}

if (response_delay < 1) {
instant_responses++
} else {
instant_responses = 0;
}
response_delay = (new Date() - request_start) / 1000;

if (response_delay < 1) {
instant_responses++
} else {
instant_responses = 0;
}

if (instant_responses > 5) {
if (instant_responses > 5) {
logger.debug('It seems the server is not using long-polling. Adding delay.');
return setTimeout(check_for_reschedule, 5000);
}
check_for_reschedule();
return setTimeout(check_for_reschedule, resp_timeout);
}
check_for_reschedule();
});

// This is most likely a connection error.
Expand Down

0 comments on commit eee4bd2

Please sign in to comment.