From fe6e6b20db73e21a27503b39e39fa5cacd782d1f Mon Sep 17 00:00:00 2001 From: nb-ohad Date: Thu, 29 Jun 2017 19:47:13 +0300 Subject: [PATCH] Enhance restore_session logic with read_auth retries in case of RPC_CONNECT_TIMEOUT --- frontend/src/app/config.json | 1 + frontend/src/app/epics/restore-session.js | 53 ++++++++++++++++------- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/frontend/src/app/config.json b/frontend/src/app/config.json index 1f64f45659..8b279c289f 100644 --- a/frontend/src/app/config.json +++ b/frontend/src/app/config.json @@ -7,6 +7,7 @@ "sslCertificateSuffix": ".zip", "upgradePackageSuffix": ".gz", "serverRestartWaitInterval": 20000, + "readAuthRetryCount": 2, "nodeTest": { "targetCount": 20, "testSettings": { diff --git a/frontend/src/app/epics/restore-session.js b/frontend/src/app/epics/restore-session.js index b81a28c27b..ac375b7aed 100644 --- a/frontend/src/app/epics/restore-session.js +++ b/frontend/src/app/epics/restore-session.js @@ -3,27 +3,50 @@ import api from 'services/api'; import { RESTORE_SESSION } from 'action-types'; import { completeRestoreSession, failResotreSession } from 'action-creators'; +import { readAuthRetryCount } from 'config'; +import Rx from 'rx'; + +function _createUnauthorizedException(message) { + const error = new Error(message); + error.rpc_code = 'UNAUTHORIZED'; + return error; +} export default function(action$) { return action$ .ofType(RESTORE_SESSION) - .flatMap(async action => { + .flatMap(action => { const { token } = action.payload; + if (!token) { + const error = _createUnauthorizedException('Token not available'); + throw { token, error }; + } - try { - api.options.auth_token = token; - const sessionInfo = await api.auth.read_auth(); - if (!sessionInfo.account) { - throw Object.assign( - new Error('Account not found'), - { rpc_code: 'UNAUTHORIZED'} - ); - } + api.options.auth_token = token; + return Rx.Observable.fromPromise(() => api.auth.read_auth()) + .map(sessionInfo => { + if (!sessionInfo.account) { + throw _createUnauthorizedException('Account not found'); + } - return completeRestoreSession(token, sessionInfo); + return completeRestoreSession(token, sessionInfo); + }) + .retryWhen(errors => { + return errors + .scan((count, err) => { + if (err.rpc_code !== 'RPC_CONNECT_TIMEOUT' || count >= readAuthRetryCount) { + throw err; + } - } catch (error) { - return failResotreSession(token, error); - } - }); + return count + 1; + }, 0) + .delay(1000); + }) + .catch(error => { + throw { token, error }; + }); + }) + .catch(({ token, error }) => Rx.Observable.of( + failResotreSession(token, error) + )); }