mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-05-31 09:50:35 +09:00
lower the chance that music recognition fails
This commit is contained in:
parent
b19b345731
commit
b1e91ca5cd
@ -83,7 +83,8 @@ export class BilibiliMusicRecognitionTool
|
||||
"User-Agent": getRandomUserAgent(),
|
||||
};
|
||||
|
||||
const response = await this.fetchWithTimeout(
|
||||
const response = await this.fetchWithTimeoutWithRetry(
|
||||
// the server has a stupid restart period every one minute. Give it a shot to recover.
|
||||
url,
|
||||
{ headers },
|
||||
this.timeout,
|
||||
@ -99,6 +100,32 @@ export class BilibiliMusicRecognitionTool
|
||||
return data;
|
||||
}
|
||||
|
||||
async fetchWithTimeoutWithRetry(
|
||||
resource: RequestInfo | URL,
|
||||
options = {},
|
||||
timeout: number = 30000,
|
||||
retryCount: number = 3,
|
||||
retryDelay: number = 1200,
|
||||
) {
|
||||
const _delay = async (ms: number) =>
|
||||
new Promise((resolve) => setTimeout(resolve, ms));
|
||||
let response;
|
||||
for (let i = 0; i < retryCount; i++) {
|
||||
try {
|
||||
response = await this.fetchWithTimeout(resource, options, timeout);
|
||||
if (response.ok) {
|
||||
return response;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
await _delay(retryDelay);
|
||||
}
|
||||
if (response) throw new Error(`HTTP error! status: ${response.status}`);
|
||||
else throw new Error("Failed to fetch resource after multiple retries.");
|
||||
}
|
||||
|
||||
async fetchWithTimeout(
|
||||
resource: RequestInfo | URL,
|
||||
options = {},
|
||||
|
Loading…
Reference in New Issue
Block a user