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,
activeLivestreamsLastFetchedDate: number,
activeLivestreamsLastFetchedOptions: {},
activeLivestreamsLastFetchedFailCount: number,
activeLivestreamInitialized: boolean,
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 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) {
dispatch({ type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_SKIPPED });
return;
const failCount = state.livestream.activeLivestreamsLastFetchedFailCount;
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
@ -207,6 +210,12 @@ export const doFetchActiveLivestreams = (
},
});
} 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,
activeLivestreamsLastFetchedDate: 0,
activeLivestreamsLastFetchedOptions: {},
activeLivestreamsLastFetchedFailCount: 0,
activeLivestreamInitialized: false,
socketConnectionById: {},
};
@ -62,8 +63,15 @@ export default handleActions(
[ACTIONS.FETCH_ACTIVE_LIVESTREAMS_STARTED]: (state: LivestreamState) => {
return { ...state, fetchingActiveLivestreams: true };
},
[ACTIONS.FETCH_ACTIVE_LIVESTREAMS_FAILED]: (state: LivestreamState) => {
return { ...state, fetchingActiveLivestreams: false };
[ACTIONS.FETCH_ACTIVE_LIVESTREAMS_FAILED]: (state: LivestreamState, action: any) => {
const { activeLivestreamsLastFetchedDate, activeLivestreamsLastFetchedOptions } = action.data;
return {
...state,
fetchingActiveLivestreams: false,
activeLivestreamsLastFetchedDate,
activeLivestreamsLastFetchedOptions,
activeLivestreamsLastFetchedFailCount: state.activeLivestreamsLastFetchedFailCount + 1,
};
},
[ACTIONS.FETCH_ACTIVE_LIVESTREAMS_COMPLETED]: (state: LivestreamState, action: any) => {
const { activeLivestreams, activeLivestreamsLastFetchedDate, activeLivestreamsLastFetchedOptions } = action.data;
@ -73,6 +81,7 @@ export default handleActions(
activeLivestreams,
activeLivestreamsLastFetchedDate,
activeLivestreamsLastFetchedOptions,
activeLivestreamsLastFetchedFailCount: 0,
viewersById: updateViewersById(activeLivestreams, state.viewersById),
};
},