Skip to content

Commit

Permalink
Enhance restore_session logic with read_auth retries in case of RPC_C…
Browse files Browse the repository at this point in the history
…ONNECT_TIMEOUT
  • Loading branch information
nb-ohad committed Jun 29, 2017
1 parent 23ae6fb commit fe6e6b2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
1 change: 1 addition & 0 deletions frontend/src/app/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"sslCertificateSuffix": ".zip",
"upgradePackageSuffix": ".gz",
"serverRestartWaitInterval": 20000,
"readAuthRetryCount": 2,
"nodeTest": {
"targetCount": 20,
"testSettings": {
Expand Down
53 changes: 38 additions & 15 deletions frontend/src/app/epics/restore-session.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
));
}

0 comments on commit fe6e6b2

Please sign in to comment.