Limit livestream fetches when failed

We were already limiting the call to once-per-5-minutes (for the succesful case), so just need to put failure cases into the mix.

Retry 3 times before stopping.
This commit is contained in:
infinite-persistence 2022-05-04 20:04:47 +08:00 committed by Thomas Zarebczan
parent 9b44b7eb91
commit a7cf89a977
3 changed files with 25 additions and 6 deletions

View file

@ -28,6 +28,7 @@ declare type LivestreamState = {
activeLivestreams: ?LivestreamInfo, activeLivestreams: ?LivestreamInfo,
activeLivestreamsLastFetchedDate: number, activeLivestreamsLastFetchedDate: number,
activeLivestreamsLastFetchedOptions: {}, activeLivestreamsLastFetchedOptions: {},
activeLivestreamsLastFetchedFailCount: number,
activeLivestreamInitialized: boolean, activeLivestreamInitialized: boolean,
socketConnectionById: { [id: string]: { connected: ?boolean, sub_category: ?string } }, socketConnectionById: { [id: string]: { connected: ?boolean, sub_category: ?string } },
}; };

View file

@ -168,10 +168,13 @@ export const doFetchActiveLivestreams = (
const nextOptions = { order_by: orderBy, ...(lang ? { any_languages: lang } : {}) }; const nextOptions = { order_by: orderBy, ...(lang ? { any_languages: lang } : {}) };
const sameOptions = JSON.stringify(prevOptions) === JSON.stringify(nextOptions); const sameOptions = JSON.stringify(prevOptions) === JSON.stringify(nextOptions);
// already fetched livestreams within the interval, skip for now
if (sameOptions && timeDelta < FETCH_ACTIVE_LIVESTREAMS_MIN_INTERVAL_MS) { if (sameOptions && timeDelta < FETCH_ACTIVE_LIVESTREAMS_MIN_INTERVAL_MS) {
dispatch({ type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_SKIPPED }); const failCount = state.livestream.activeLivestreamsLastFetchedFailCount;
return; if (failCount === 0 || failCount > 3) {
// Just fetched successfully, or failed 3 times. Skip for FETCH_ACTIVE_LIVESTREAMS_MIN_INTERVAL_MS.
dispatch({ type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_SKIPPED });
return;
}
} }
// start fetching livestreams // start fetching livestreams
@ -207,6 +210,12 @@ export const doFetchActiveLivestreams = (
}, },
}); });
} catch (err) { } catch (err) {
dispatch({ type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_FAILED }); dispatch({
type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_FAILED,
data: {
activeLivestreamsLastFetchedDate: now,
activeLivestreamsLastFetchedOptions: nextOptions,
},
});
} }
}; };

View file

@ -10,6 +10,7 @@ const defaultState: LivestreamState = {
activeLivestreams: null, activeLivestreams: null,
activeLivestreamsLastFetchedDate: 0, activeLivestreamsLastFetchedDate: 0,
activeLivestreamsLastFetchedOptions: {}, activeLivestreamsLastFetchedOptions: {},
activeLivestreamsLastFetchedFailCount: 0,
activeLivestreamInitialized: false, activeLivestreamInitialized: false,
socketConnectionById: {}, socketConnectionById: {},
}; };
@ -62,8 +63,15 @@ export default handleActions(
[ACTIONS.FETCH_ACTIVE_LIVESTREAMS_STARTED]: (state: LivestreamState) => { [ACTIONS.FETCH_ACTIVE_LIVESTREAMS_STARTED]: (state: LivestreamState) => {
return { ...state, fetchingActiveLivestreams: true }; return { ...state, fetchingActiveLivestreams: true };
}, },
[ACTIONS.FETCH_ACTIVE_LIVESTREAMS_FAILED]: (state: LivestreamState) => { [ACTIONS.FETCH_ACTIVE_LIVESTREAMS_FAILED]: (state: LivestreamState, action: any) => {
return { ...state, fetchingActiveLivestreams: false }; const { activeLivestreamsLastFetchedDate, activeLivestreamsLastFetchedOptions } = action.data;
return {
...state,
fetchingActiveLivestreams: false,
activeLivestreamsLastFetchedDate,
activeLivestreamsLastFetchedOptions,
activeLivestreamsLastFetchedFailCount: state.activeLivestreamsLastFetchedFailCount + 1,
};
}, },
[ACTIONS.FETCH_ACTIVE_LIVESTREAMS_COMPLETED]: (state: LivestreamState, action: any) => { [ACTIONS.FETCH_ACTIVE_LIVESTREAMS_COMPLETED]: (state: LivestreamState, action: any) => {
const { activeLivestreams, activeLivestreamsLastFetchedDate, activeLivestreamsLastFetchedOptions } = action.data; const { activeLivestreams, activeLivestreamsLastFetchedDate, activeLivestreamsLastFetchedOptions } = action.data;
@ -73,6 +81,7 @@ export default handleActions(
activeLivestreams, activeLivestreams,
activeLivestreamsLastFetchedDate, activeLivestreamsLastFetchedDate,
activeLivestreamsLastFetchedOptions, activeLivestreamsLastFetchedOptions,
activeLivestreamsLastFetchedFailCount: 0,
viewersById: updateViewersById(activeLivestreams, state.viewersById), viewersById: updateViewersById(activeLivestreams, state.viewersById),
}; };
}, },