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.

paul/schema_breaking_changes
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', [])
.factory('eventStreamService', ['$q', '$timeout', 'matrixService', 'eventHandlerService', function($q, $timeout, matrixService, eventHandlerService) {
var END = "END";
var TIMEOUT_MS = 30000;
var SERVER_TIMEOUT_MS = 30000;
var CLIENT_TIMEOUT_MS = 40000;
var ERR_TIMEOUT_MS = 5000;
var settings = {
@ -55,7 +56,7 @@ angular.module('eventStreamService', [])
deferred = deferred || $q.defer();
// 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) {
if (!settings.isActive) {
console.log("[EventStream] Got response but now inactive. Dropping data.");
@ -80,7 +81,7 @@ angular.module('eventStreamService', [])
}
},
function(error) {
if (error.status == 403) {
if (error.status === 403) {
settings.shouldPoll = false;
}

View File

@ -41,7 +41,7 @@ angular.module('matrixService', [])
var prefixPath = "/matrix/client/api/v1";
var MAPPING_PREFIX = "alias_for_";
var doRequest = function(method, path, params, data) {
var doRequest = function(method, path, params, data, $httpParams) {
if (!config) {
console.warn("No config exists. Cannot perform request to "+path);
return;
@ -58,7 +58,7 @@ angular.module('matrixService', [])
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) {
@ -343,15 +343,31 @@ angular.module('matrixService', [])
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 params = {
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