Skip to content

Commit 3cdf21e

Browse files
committed
Fix: add timeout to IDCTA fetch
1 parent 1c1d286 commit 3cdf21e

File tree

1 file changed

+9
-2
lines changed
  • src/app/lib/idcta/fetchIdctaConfig

1 file changed

+9
-2
lines changed

src/app/lib/idcta/fetchIdctaConfig/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,19 @@ const cache = new LRUCache<string, IdctaConfig>({
2222
async function fetchIdctaConfig(): Promise<IdctaConfig | null> {
2323
const idctaConfigUrl = getIdctaConfigUrl();
2424

25+
if (!idctaConfigUrl) return null;
26+
2527
const cachedConfig = cache.get(idctaConfigUrl);
2628
if (cachedConfig) {
2729
return cachedConfig;
2830
}
2931

32+
const controller = new AbortController();
33+
const timeoutMs = 2000;
34+
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
35+
3036
try {
31-
const response = await fetch(idctaConfigUrl);
37+
const response = await fetch(idctaConfigUrl, { signal: controller.signal });
3238

3339
if (!response.ok) {
3440
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
@@ -37,14 +43,15 @@ async function fetchIdctaConfig(): Promise<IdctaConfig | null> {
3743
const config = await response.json();
3844

3945
cache.set(idctaConfigUrl, config);
40-
4146
return config;
4247
} catch (error) {
4348
logger.error(IDCTA_FETCH_ERROR, {
4449
url: idctaConfigUrl,
4550
error,
4651
});
4752
return null;
53+
} finally {
54+
clearTimeout(timeoutId);
4855
}
4956
}
5057

0 commit comments

Comments
 (0)