Added a timeout(40s) to $http stream requests (/events) in order to be notified by an error when there is a network issue. Thus, we can retry with a new request.

This commit is contained in:
Emmanuel ROHEE 2014-08-29 11:31:03 +02:00
parent d1bf659ed7
commit ee079cd250
2 changed files with 27 additions and 10 deletions

View File

@ -25,7 +25,8 @@ the eventHandlerService.
angular.module('eventStreamService', []) angular.module('eventStreamService', [])
.factory('eventStreamService', ['$q', '$timeout', 'matrixService', 'eventHandlerService', function($q, $timeout, matrixService, eventHandlerService) { .factory('eventStreamService', ['$q', '$timeout', 'matrixService', 'eventHandlerService', function($q, $timeout, matrixService, eventHandlerService) {
var END = "END"; var END = "END";
var TIMEOUT_MS = 30000; var SERVER_TIMEOUT_MS = 30000;
var CLIENT_TIMEOUT_MS = 40000;
var ERR_TIMEOUT_MS = 5000; var ERR_TIMEOUT_MS = 5000;
var settings = { var settings = {
@ -55,7 +56,7 @@ angular.module('eventStreamService', [])
deferred = deferred || $q.defer(); deferred = deferred || $q.defer();
// run the stream from the latest token // run the stream from the latest token
matrixService.getEventStream(settings.from, TIMEOUT_MS).then( matrixService.getEventStream(settings.from, SERVER_TIMEOUT_MS, CLIENT_TIMEOUT_MS).then(
function(response) { function(response) {
if (!settings.isActive) { if (!settings.isActive) {
console.log("[EventStream] Got response but now inactive. Dropping data."); console.log("[EventStream] Got response but now inactive. Dropping data.");
@ -80,7 +81,7 @@ angular.module('eventStreamService', [])
} }
}, },
function(error) { function(error) {
if (error.status == 403) { if (error.status === 403) {
settings.shouldPoll = false; settings.shouldPoll = false;
} }

View File

@ -41,7 +41,7 @@ angular.module('matrixService', [])
var prefixPath = "/matrix/client/api/v1"; var prefixPath = "/matrix/client/api/v1";
var MAPPING_PREFIX = "alias_for_"; var MAPPING_PREFIX = "alias_for_";
var doRequest = function(method, path, params, data) { var doRequest = function(method, path, params, data, $httpParams) {
if (!config) { if (!config) {
console.warn("No config exists. Cannot perform request to "+path); console.warn("No config exists. Cannot perform request to "+path);
return; return;
@ -58,7 +58,7 @@ angular.module('matrixService', [])
path = prefixPath + path; path = prefixPath + path;
} }
return doBaseRequest(config.homeserver, method, path, params, data, undefined); return doBaseRequest(config.homeserver, method, path, params, data, undefined, $httpParams);
}; };
var doBaseRequest = function(baseUrl, method, path, params, data, headers, $httpParams) { var doBaseRequest = function(baseUrl, method, path, params, data, headers, $httpParams) {
@ -344,14 +344,30 @@ angular.module('matrixService', [])
return doBaseRequest(config.homeserver, "POST", path, params, file, headers, $httpParams); return doBaseRequest(config.homeserver, "POST", path, params, file, headers, $httpParams);
}, },
// start listening on /events /**
getEventStream: function(from, timeout) { * Start listening on /events
* @param {String} from the token from which to listen events to
* @param {Integer} serverTimeout the time in ms the server will hold open the connection
* @param {Integer} clientTimeout the timeout in ms used at the client HTTP request level
* @returns a promise
*/
getEventStream: function(from, serverTimeout, clientTimeout) {
var path = "/events"; var path = "/events";
var params = { var params = {
from: from, from: from,
timeout: timeout timeout: serverTimeout
}; };
return doRequest("GET", path, params);
var $httpParams;
if (clientTimeout) {
// If the Internet connection is lost, this timeout is used to be able to
// cancel the current request and notify the client so that it can retry with a new request.
$httpParams = {
timeout: clientTimeout
};
}
return doRequest("GET", path, params, undefined, $httpParams);
}, },
// Indicates if user authentications details are stored in cache // Indicates if user authentications details are stored in cache