Skip to content

Commit

Permalink
handle lapis timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
theosanderson committed May 26, 2024
1 parent 6ba32c7 commit 167b64f
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions website/src/components/IndexPage/getOrganismStatistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export type OrganismStatistics = {
};
type OrganismStatisticsMap = Map<string, OrganismStatistics>;

const TIMEOUT_MS = 500;

export const getOrganismStatisticsMap = async (
organismNames: string[],
numberDaysAgo: number,
Expand All @@ -26,8 +28,8 @@ export const getOrganismStatisticsMap = async (

const getOrganismStatistics = async (organism: string, numberDaysAgo: number): Promise<OrganismStatistics> => {
const [{ total, lastUpdatedAt }, recent] = await Promise.all([
getTotalAndLastUpdatedAt(organism),
getRecent(organism, numberDaysAgo),
withTimeout(getTotalAndLastUpdatedAt(organism), TIMEOUT_MS, { total: -1, lastUpdatedAt: undefined }),
withTimeout(getRecent(organism, numberDaysAgo), TIMEOUT_MS, 0),
]);
return {
totalSequences: total,
Expand All @@ -36,6 +38,11 @@ const getOrganismStatistics = async (organism: string, numberDaysAgo: number): P
};
};

const withTimeout = <T>(promise: Promise<T>, ms: number, defaultValue: T): Promise<T> => {
const timeout = new Promise<T>((resolve) => setTimeout(() => resolve(defaultValue), ms));
return Promise.race([promise, timeout]);
};

const getTotalAndLastUpdatedAt = async (
organism: string,
): Promise<{ total: number; lastUpdatedAt: DateTime | undefined }> => {
Expand Down

0 comments on commit 167b64f

Please sign in to comment.