From a7cf89a97724f0d211c8a216188c289022b9f417 Mon Sep 17 00:00:00 2001 From: infinite-persistence Date: Wed, 4 May 2022 20:04:47 +0800 Subject: [PATCH] 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. --- flow-typed/livestream.js | 1 + ui/redux/actions/livestream.js | 17 +++++++++++++---- ui/redux/reducers/livestream.js | 13 +++++++++++-- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/flow-typed/livestream.js b/flow-typed/livestream.js index 41cdf0e03..2387ee424 100644 --- a/flow-typed/livestream.js +++ b/flow-typed/livestream.js @@ -28,6 +28,7 @@ declare type LivestreamState = { activeLivestreams: ?LivestreamInfo, activeLivestreamsLastFetchedDate: number, activeLivestreamsLastFetchedOptions: {}, + activeLivestreamsLastFetchedFailCount: number, activeLivestreamInitialized: boolean, socketConnectionById: { [id: string]: { connected: ?boolean, sub_category: ?string } }, }; diff --git a/ui/redux/actions/livestream.js b/ui/redux/actions/livestream.js index 16c8aabd8..425bf7c40 100644 --- a/ui/redux/actions/livestream.js +++ b/ui/redux/actions/livestream.js @@ -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, + }, + }); } }; diff --git a/ui/redux/reducers/livestream.js b/ui/redux/reducers/livestream.js index 8786c8127..67463bb22 100644 --- a/ui/redux/reducers/livestream.js +++ b/ui/redux/reducers/livestream.js @@ -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), }; },