Skip to content

Commit

Permalink
Merge pull request #1457 from ebkr/chunky-pt4
Browse files Browse the repository at this point in the history
Harden chunked package list API requests against CDN problems
  • Loading branch information
anttimaki authored Oct 7, 2024
2 parents b7ba72f + 45b8b20 commit 8055464
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/store/modules/TsModsModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { State as RootState } from '../index';
import ManifestV2 from '../../model/ManifestV2';
import ThunderstoreCombo from '../../model/ThunderstoreCombo';
import ThunderstoreMod from '../../model/ThunderstoreMod';
import CdnProvider from '../../providers/generic/connection/CdnProvider';
import ConnectionProvider from '../../providers/generic/connection/ConnectionProvider';
import * as PackageDb from '../../r2mm/manager/PackageDexieStore';
import { isEmptyArray, isStringArray } from '../../utils/ArrayUtils';
Expand Down Expand Up @@ -171,8 +172,8 @@ export const TsModsModule = {

actions: <ActionTree<State, RootState>>{
async fetchPackageListIndex({dispatch, rootState}): Promise<PackageListIndex> {
const indexUrl = rootState.activeGame.thunderstoreUrl;
const index = await retry(() => fetchAndProcessBlobFile(indexUrl));
const indexUrl = CdnProvider.addCdnQueryParameter(rootState.activeGame.thunderstoreUrl);
const index = await retry(() => fetchAndProcessBlobFile(indexUrl), 5, 2000);

if (!isStringArray(index.content)) {
throw new Error('Received invalid chunk index from API');
Expand Down Expand Up @@ -208,7 +209,8 @@ export const TsModsModule = {
// out due to concurrent requests competing for the bandwidth.
const chunks = [];
for (const [i, chunkUrl] of chunkUrls.entries()) {
const {content: chunk} = await retry(() => fetchAndProcessBlobFile(chunkUrl))
const url = CdnProvider.replaceCdnHost(chunkUrl);
const {content: chunk} = await retry(() => fetchAndProcessBlobFile(url));

if (chunkUrls.length > 1 && isEmptyArray(chunk)) {
throw new Error(`Chunk #${i} in multichunk response was empty`);
Expand Down
3 changes: 2 additions & 1 deletion src/utils/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const getPropertyFromPath = (object: Mappable, path: string | string[]):
export async function retry<T>(
fn: () => Promise<T>,
attempts: number = 3,
interval: number = 5000,
canRetry: () => boolean = () => true,
onError: (e: Error | unknown) => void = console.error
): Promise<T> {
Expand All @@ -43,7 +44,7 @@ export async function retry<T>(
onError(e);

if (currentAttempt < attempts) {
await sleep(5000);
await sleep(interval);
}
}
}
Expand Down

0 comments on commit 8055464

Please sign in to comment.