Skip to content

Commit

Permalink
Chore: add timeout to build
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Dec 30, 2023
1 parent a7c1b0f commit 480a1fe
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 34 deletions.
84 changes: 54 additions & 30 deletions Build/build-speedtest-domainset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { task } from './lib/trace-runner';
import { fetchWithRetry } from './lib/fetch-retry';
import { SHARED_DESCRIPTION } from './lib/constants';
import { getGorhillPublicSuffixPromise } from './lib/get-gorhill-publicsuffix';
import retry from 'async-retry';

const s = new Sema(2);

Expand All @@ -21,39 +22,54 @@ const querySpeedtestApi = async (keyword: string): Promise<Array<string | null>>
s.acquire()
]))[0];

let timer = null;
try {
const randomUserAgent = topUserAgents[Math.floor(Math.random() * topUserAgents.length)];
const key = `fetch speedtest endpoints: ${keyword}`;
console.log(key);
console.time(key);

// AbortSignal.timeout() is not supported by bun.
const controller = new AbortController();
timer = setTimeout(() => controller.abort(), 4000);

const res = await fetchWithRetry(`https://www.speedtest.net/api/js/servers?engine=js&search=${keyword}&limit=100`, {
headers: {
dnt: '1',
Referer: 'https://www.speedtest.net/',
accept: 'application/json, text/plain, */*',
'User-Agent': randomUserAgent,
'Accept-Language': 'en-US,en;q=0.9',
'Sec-Ch-Ua': '"Not/A)Brand";v="99", "Google Chrome";v="115", "Chromium";v="115"',
'Sec-Ch-Ua-Mobile': '?0',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'Sec-Gpc': '1'
},
retry: {
retryOnAborted: true
},
signal: controller.signal

const res = await retry<Response>(() => {
const { promise, resolve, reject } = Promise.withResolvers<Response>();

const controller = new AbortController();
const timer = setTimeout(() => {
console.log(key, 'timeout');
controller.abort();
reject(new Error('timeout'));
}, 4000);
fetch(`https://www.speedtest.net/api/js/servers?engine=js&search=${keyword}&limit=100`, {
headers: {
dnt: '1',
Referer: 'https://www.speedtest.net/',
accept: 'application/json, text/plain, */*',
'User-Agent': randomUserAgent,
'Accept-Language': 'en-US,en;q=0.9',
...(randomUserAgent.includes('Chrome')
? {
'Sec-Ch-Ua-Mobile': '?0',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'Sec-Gpc': '1'
}
: {})
},
signal: controller.signal
}).then(res => {
clearTimeout(timer);
if (!res.ok) {
reject(new Error(res.statusText));
} else {
resolve(res as Response);
}
});

return promise;
}, {
retries: 2
});
if (!res.ok) {
throw new Error(`${res.statusText}\n${await res.text()}`);
}

const json = await res.json<Array<{ url: string }>>();

Expand All @@ -64,9 +80,6 @@ const querySpeedtestApi = async (keyword: string): Promise<Array<string | null>>
console.log(e);
return [];
} finally {
if (timer) {
clearTimeout(timer);
}
s.release();
}
};
Expand Down Expand Up @@ -125,7 +138,7 @@ export const buildSpeedtestDomainSet = task(import.meta.path, async () => {
'.speed.googlefiber.net'
]);

const hostnameGroups = await Promise.all([
const pMap = ([
'Hong Kong',
'Taiwan',
'China Telecom',
Expand Down Expand Up @@ -154,7 +167,18 @@ export const buildSpeedtestDomainSet = task(import.meta.path, async () => {
'Sydney',
'Brazil',
'Turkey'
].map(querySpeedtestApi));
// eslint-disable-next-line @typescript-eslint/array-type -- generic type fuck ts lsp
]).reduce<Record<string, Promise<(string | null)[]>>>((pMap, keyword) => {
pMap[keyword] = querySpeedtestApi(keyword);
return pMap;
}, {});

const timer = setInterval(() => {
console.log(Object.fromEntries(Object.entries(pMap).map(([name, promise]) => [name, Bun.peek.status(promise)] as const)));
}, 500);
timer.unref();

const hostnameGroups = await Promise.all(Object.values(pMap));

for (const hostnames of hostnameGroups) {
if (Array.isArray(hostnames)) {
Expand Down
18 changes: 15 additions & 3 deletions Build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { buildPublic } from './build-public';
import { downloadMockAssets } from './download-mock-assets';

import type { TaskResult } from './lib/trace-runner';
import picocolors from 'picocolors';

(async () => {
console.log('Bun version:', Bun.version, Bun.revision);
Expand Down Expand Up @@ -74,7 +75,7 @@ import type { TaskResult } from './lib/trace-runner';

const downloadMockAssetsPromise = downloadMockAssets();

const stats = await Promise.all([
const pMap = {
downloadPreviousBuildPromise,
buildCommonPromise,
buildAntiBogusDomainPromise,
Expand All @@ -93,9 +94,20 @@ import type { TaskResult } from './lib/trace-runner';
buildStreamServicePromise,
buildMicrosoftCdnPromise,
buildSSPanelUIMAppProfilePromise,

downloadMockAssetsPromise
]);
};

const _ = setTimeout(() => {
console.error(picocolors.red('Task timeout!'));
Object.entries(pMap).forEach(([name, p]) => {
console.log(`[${name}]`, Bun.peek.status(p));
});

process.exit(1);
}, 1000 * 60 * 2);

const stats = await Promise.all(Object.values(pMap));
clearTimeout(_);

await Promise.all([
buildPublic(),
Expand Down
6 changes: 5 additions & 1 deletion Build/lib/fetch-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ class CustomAbortError extends Error {
}

const sleepWithAbort = (ms: number, signal: AbortSignal) => new Promise<void>((resolve, reject) => {
signal.throwIfAborted();
if (signal.aborted) {
reject(signal.reason);
return;
}

signal.addEventListener('abort', stop);
Bun.sleep(ms).then(done).catch(doReject);

Expand Down

0 comments on commit 480a1fe

Please sign in to comment.