From add7792d2442d888916783d59e89c8cf23666d8d Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 22 Aug 2019 11:22:47 +0200 Subject: [PATCH] MOBILE-3127 core: Allow defining a different timeout in wifi/3g --- src/classes/site.ts | 2 +- src/core/constants.ts | 3 ++- src/providers/sites.ts | 8 +++++--- src/providers/utils/utils.ts | 4 ++-- src/providers/ws.ts | 19 ++++++++++++++----- 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/classes/site.ts b/src/classes/site.ts index 18be199e1..4f885fe93 100644 --- a/src/classes/site.ts +++ b/src/classes/site.ts @@ -1334,7 +1334,7 @@ export class CoreSite { return Promise.resolve({ code: 0 }); } - const promise = this.http.post(checkUrl, { service: service }).timeout(CoreConstants.WS_TIMEOUT).toPromise(); + const promise = this.http.post(checkUrl, { service: service }).timeout(this.wsProvider.getRequestTimeout()).toPromise(); return promise.then((data: any) => { if (typeof data != 'undefined' && data.errorcode === 'requirecorrectaccess') { diff --git a/src/core/constants.ts b/src/core/constants.ts index 33e20f0c2..a6f31c3a5 100644 --- a/src/core/constants.ts +++ b/src/core/constants.ts @@ -39,7 +39,8 @@ export class CoreConstants { static SETTINGS_ANALYTICS_ENABLED = 'CoreSettingsAnalyticsEnabled'; // WS constants. - static WS_TIMEOUT = 30000; + static WS_TIMEOUT = 30000; // Timeout when not in WiFi. + static WS_TIMEOUT_WIFI = 30000; // Timeout when in WiFi. static WS_PREFIX = 'local_mobile_'; // Login constants. diff --git a/src/providers/sites.ts b/src/providers/sites.ts index 22df10cb7..699843c09 100644 --- a/src/providers/sites.ts +++ b/src/providers/sites.ts @@ -22,6 +22,7 @@ import { CoreSitesFactoryProvider } from './sites-factory'; import { CoreTextUtilsProvider } from './utils/text'; import { CoreUrlUtilsProvider } from './utils/url'; import { CoreUtilsProvider } from './utils/utils'; +import { CoreWSProvider } from './ws'; import { CoreConstants } from '@core/constants'; import { CoreConfigConstants } from '../configconstants'; import { CoreSite } from '@classes/site'; @@ -326,7 +327,7 @@ export class CoreSitesProvider { constructor(logger: CoreLoggerProvider, private http: HttpClient, private sitesFactory: CoreSitesFactoryProvider, private appProvider: CoreAppProvider, private translate: TranslateService, private urlUtils: CoreUrlUtilsProvider, private eventsProvider: CoreEventsProvider, private textUtils: CoreTextUtilsProvider, - private utils: CoreUtilsProvider, private injector: Injector) { + private utils: CoreUtilsProvider, private injector: Injector, private wsProvider: CoreWSProvider) { this.logger = logger.getInstance('CoreSitesProvider'); this.appDB = appProvider.getDB(); @@ -515,7 +516,8 @@ export class CoreSitesProvider { * @return {Promise} A promise to be resolved if the site exists. */ siteExists(siteUrl: string): Promise { - return this.http.post(siteUrl + '/login/token.php', {}).timeout(CoreConstants.WS_TIMEOUT).toPromise().catch(() => { + return this.http.post(siteUrl + '/login/token.php', {}).timeout(this.wsProvider.getRequestTimeout()).toPromise() + .catch(() => { // Default error messages are kinda bad, return our own message. return Promise.reject({error: this.translate.instant('core.cannotconnect')}); }).then((data: any) => { @@ -555,7 +557,7 @@ export class CoreSitesProvider { service: service }, loginUrl = siteUrl + '/login/token.php', - promise = this.http.post(loginUrl, params).timeout(CoreConstants.WS_TIMEOUT).toPromise(); + promise = this.http.post(loginUrl, params).timeout(this.wsProvider.getRequestTimeout()).toPromise(); return promise.then((data: any): any => { if (typeof data == 'undefined') { diff --git a/src/providers/utils/utils.ts b/src/providers/utils/utils.ts index 306c71525..db08cf476 100644 --- a/src/providers/utils/utils.ts +++ b/src/providers/utils/utils.ts @@ -27,7 +27,6 @@ import { CoreLoggerProvider } from '../logger'; import { TranslateService } from '@ngx-translate/core'; import { CoreLangProvider } from '../lang'; import { CoreWSProvider, CoreWSError } from '../ws'; -import { CoreConstants } from '@core/constants'; /** * Deferred promise. It's similar to the result of $q.defer() in AngularJS. @@ -232,7 +231,8 @@ export class CoreUtilsProvider { initOptions.signal = controller.signal; } - return this.timeoutPromise(window.fetch(url, initOptions), CoreConstants.WS_TIMEOUT).then((response: Response) => { + return this.timeoutPromise(window.fetch(url, initOptions), this.wsProvider.getRequestTimeout()) + .then((response: Response) => { return response.redirected; }).catch((error) => { if (error.timeout && controller) { diff --git a/src/providers/ws.ts b/src/providers/ws.ts index 08d53055c..636b00e0b 100644 --- a/src/providers/ws.ts +++ b/src/providers/ws.ts @@ -253,9 +253,9 @@ export class CoreWSProvider { if (preSets.noLogin && preSets.useGet) { // Send params using GET. siteUrl += '&args=' + encodeURIComponent(ajaxData); - promise = this.http.get(siteUrl).timeout(CoreConstants.WS_TIMEOUT).toPromise(); + promise = this.http.get(siteUrl).timeout(this.getRequestTimeout()).toPromise(); } else { - promise = this.http.post(siteUrl, ajaxData).timeout(CoreConstants.WS_TIMEOUT).toPromise(); + promise = this.http.post(siteUrl, ajaxData).timeout(this.getRequestTimeout()).toPromise(); } return promise.then((data: any) => { @@ -516,6 +516,15 @@ export class CoreWSProvider { }); } + /** + * Get a request timeout based on the network connection. + * + * @return {number} Timeout in ms. + */ + getRequestTimeout(): number { + return this.appProvider.isNetworkAccessLimited() ? CoreConstants.WS_TIMEOUT : CoreConstants.WS_TIMEOUT_WIFI; + } + /** * Get the unique queue item id of the cache for a HTTP request. * @@ -542,7 +551,7 @@ export class CoreWSProvider { let promise = this.getPromiseHttp('head', url); if (!promise) { - promise = this.commonHttp.head(url).timeout(CoreConstants.WS_TIMEOUT).toPromise(); + promise = this.commonHttp.head(url).timeout(this.getRequestTimeout()).toPromise(); promise = this.setPromiseHttp(promise, 'head', url); } @@ -573,7 +582,7 @@ export class CoreWSProvider { const requestUrl = siteUrl + '&wsfunction=' + method; // Perform the post request. - const promise = this.http.post(requestUrl, ajaxData, options).timeout(CoreConstants.WS_TIMEOUT).toPromise(); + const promise = this.http.post(requestUrl, ajaxData, options).timeout(this.getRequestTimeout()).toPromise(); return promise.then((data: any) => { // Some moodle web services return null. @@ -693,7 +702,7 @@ export class CoreWSProvider { // HTTP not finished, but we should delete the promise after timeout. timeout = setTimeout(() => { delete this.ongoingCalls[queueItemId]; - }, CoreConstants.WS_TIMEOUT); + }, this.getRequestTimeout()); // HTTP finished, delete from ongoing. return promise.finally(() => {