mirror of
https://github.com/PrivateCaptcha/PrivateCaptcha.git
synced 2026-02-11 00:08:47 -06:00
Add retry for fetching usage chart data
This commit is contained in:
@@ -209,11 +209,24 @@
|
||||
this.setLegend(legend1, 'Requests', this.requestedColor);
|
||||
}
|
||||
|
||||
async fetchChartData(spinnerElement) {
|
||||
async fetchChartData(spinnerElement, maxRetries = 3, baseDelay = 1000) {
|
||||
if (spinnerElement) { spinnerElement.style.display = 'flex'; }
|
||||
try {
|
||||
const response = await fetch('{{ partsURL $.Const.UserEndpoint $.Const.Stats | safeJS }}');
|
||||
return await response.json();
|
||||
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
||||
const response = await fetch('{{ partsURL $.Const.UserEndpoint $.Const.Stats | safeJS }}');
|
||||
if (response.ok) {
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
if (response.status === 429 || response.status === 503) {
|
||||
const retryDelay = baseDelay * Math.pow(2, attempt - 1);
|
||||
await new Promise(resolve => setTimeout(resolve, retryDelay));
|
||||
continue;
|
||||
}
|
||||
|
||||
const errorText = await response.text();
|
||||
throw new Error(`Request failed (${response.status}): ${errorText || response.statusText}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error fetching chart data:', error);
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user