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(),
|
"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,
|
url,
|
||||||
{ headers },
|
{ headers },
|
||||||
this.timeout,
|
this.timeout,
|
||||||
|
@ -99,6 +100,32 @@ export class BilibiliMusicRecognitionTool
|
||||||
return data;
|
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(
|
async fetchWithTimeout(
|
||||||
resource: RequestInfo | URL,
|
resource: RequestInfo | URL,
|
||||||
options = {},
|
options = {},
|
||||||
|
|
Loading…
Reference in New Issue