mirror of
https://github.com/basicswap/basicswap.git
synced 2025-12-29 16:51:39 +01:00
JS: Final tweaks 429
This commit is contained in:
@@ -141,6 +141,7 @@ const api = {
|
|||||||
try {
|
try {
|
||||||
const existingCache = localStorage.getItem(cacheKey);
|
const existingCache = localStorage.getItem(cacheKey);
|
||||||
let fallbackData = null;
|
let fallbackData = null;
|
||||||
|
|
||||||
if (existingCache) {
|
if (existingCache) {
|
||||||
try {
|
try {
|
||||||
const parsed = JSON.parse(existingCache);
|
const parsed = JSON.parse(existingCache);
|
||||||
@@ -197,12 +198,14 @@ const api = {
|
|||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching CoinGecko data:', error);
|
console.error('Error fetching CoinGecko data:', error);
|
||||||
|
|
||||||
const cachedData = cache.get(cacheKey);
|
const cachedData = cache.get(cacheKey);
|
||||||
if (cachedData) {
|
if (cachedData) {
|
||||||
console.log('Using expired cache data due to error');
|
console.log('Using expired cache data due to error');
|
||||||
return cachedData.value;
|
return cachedData.value;
|
||||||
}
|
}
|
||||||
return { error: error.message };
|
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@@ -279,10 +282,11 @@ const api = {
|
|||||||
const rateLimiter = {
|
const rateLimiter = {
|
||||||
lastRequestTime: {},
|
lastRequestTime: {},
|
||||||
minRequestInterval: {
|
minRequestInterval: {
|
||||||
coingecko: 15000,
|
coingecko: 30000,
|
||||||
cryptocompare: 1000
|
cryptocompare: 2000
|
||||||
},
|
},
|
||||||
requestQueue: {},
|
requestQueue: {},
|
||||||
|
retryDelays: [2000, 5000, 10000],
|
||||||
|
|
||||||
canMakeRequest: function(apiName) {
|
canMakeRequest: function(apiName) {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
@@ -300,7 +304,7 @@ const rateLimiter = {
|
|||||||
return Math.max(0, this.minRequestInterval[apiName] - (now - lastRequest));
|
return Math.max(0, this.minRequestInterval[apiName] - (now - lastRequest));
|
||||||
},
|
},
|
||||||
|
|
||||||
queueRequest: async function(apiName, requestFn) {
|
queueRequest: async function(apiName, requestFn, retryCount = 0) {
|
||||||
if (!this.requestQueue[apiName]) {
|
if (!this.requestQueue[apiName]) {
|
||||||
this.requestQueue[apiName] = Promise.resolve();
|
this.requestQueue[apiName] = Promise.resolve();
|
||||||
}
|
}
|
||||||
@@ -314,15 +318,31 @@ const rateLimiter = {
|
|||||||
await new Promise(resolve => setTimeout(resolve, waitTime));
|
await new Promise(resolve => setTimeout(resolve, waitTime));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
this.updateLastRequestTime(apiName);
|
this.updateLastRequestTime(apiName);
|
||||||
return await requestFn();
|
return await requestFn();
|
||||||
|
} catch (error) {
|
||||||
|
if (error.message.includes('429') && retryCount < this.retryDelays.length) {
|
||||||
|
const delay = this.retryDelays[retryCount];
|
||||||
|
console.log(`Rate limit hit, retrying in ${delay/1000} seconds...`);
|
||||||
|
await new Promise(resolve => setTimeout(resolve, delay));
|
||||||
|
return this.queueRequest(apiName, requestFn, retryCount + 1);
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
this.requestQueue[apiName] = executeRequest();
|
this.requestQueue[apiName] = executeRequest();
|
||||||
return await this.requestQueue[apiName];
|
return await this.requestQueue[apiName];
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Queue error for ${apiName}:`, error);
|
if (error.message.includes('429')) {
|
||||||
|
const cachedData = cache.get(`coinData_${apiName}`);
|
||||||
|
if (cachedData) {
|
||||||
|
console.log('Rate limit reached, using cached data');
|
||||||
|
return cachedData.value;
|
||||||
|
}
|
||||||
|
}
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1186,17 +1206,21 @@ setupEventListeners: () => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const lastGeckoRequest = rateLimiter.lastRequestTime['coingecko'] || 0;
|
const lastGeckoRequest = rateLimiter.lastRequestTime['coingecko'] || 0;
|
||||||
const timeSinceLastRequest = Date.now() - lastGeckoRequest;
|
const timeSinceLastRequest = Date.now() - lastGeckoRequest;
|
||||||
if (timeSinceLastRequest < rateLimiter.minRequestInterval.coingecko) {
|
const waitTime = Math.max(0, rateLimiter.minRequestInterval.coingecko - timeSinceLastRequest);
|
||||||
let waitTime = Math.ceil((rateLimiter.minRequestInterval.coingecko - timeSinceLastRequest) / 1000);
|
|
||||||
|
|
||||||
ui.displayErrorMessage(`Rate limit: Please wait ${waitTime} seconds before refreshing`);
|
|
||||||
|
|
||||||
const countdownInterval = setInterval(() => {
|
|
||||||
waitTime--;
|
|
||||||
if (waitTime > 0) {
|
if (waitTime > 0) {
|
||||||
ui.displayErrorMessage(`Rate limit: Please wait ${waitTime} seconds before refreshing`);
|
const seconds = Math.ceil(waitTime / 1000);
|
||||||
|
ui.displayErrorMessage(`Rate limit: Please wait ${seconds} seconds before refreshing`);
|
||||||
|
|
||||||
|
|
||||||
|
let remainingTime = seconds;
|
||||||
|
const countdownInterval = setInterval(() => {
|
||||||
|
remainingTime--;
|
||||||
|
if (remainingTime > 0) {
|
||||||
|
ui.displayErrorMessage(`Rate limit: Please wait ${remainingTime} seconds before refreshing`);
|
||||||
} else {
|
} else {
|
||||||
clearInterval(countdownInterval);
|
clearInterval(countdownInterval);
|
||||||
ui.hideErrorMessage();
|
ui.hideErrorMessage();
|
||||||
@@ -1206,7 +1230,7 @@ setupEventListeners: () => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('Refreshing all data...');
|
console.log('Starting refresh of all data...');
|
||||||
app.isRefreshing = true;
|
app.isRefreshing = true;
|
||||||
ui.showLoader();
|
ui.showLoader();
|
||||||
chartModule.showChartLoader();
|
chartModule.showChartLoader();
|
||||||
@@ -1257,8 +1281,10 @@ setupEventListeners: () => {
|
|||||||
await chartModule.updateChart(chartModule.currentCoin, true);
|
await chartModule.updateChart(chartModule.currentCoin, true);
|
||||||
} catch (chartError) {
|
} catch (chartError) {
|
||||||
console.error('Chart update failed:', chartError);
|
console.error('Chart update failed:', chartError);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
app.lastRefreshedTime = new Date();
|
app.lastRefreshedTime = new Date();
|
||||||
localStorage.setItem('lastRefreshedTime', app.lastRefreshedTime.getTime().toString());
|
localStorage.setItem('lastRefreshedTime', app.lastRefreshedTime.getTime().toString());
|
||||||
ui.updateLastRefreshedTime();
|
ui.updateLastRefreshedTime();
|
||||||
@@ -1281,11 +1307,16 @@ setupEventListeners: () => {
|
|||||||
}
|
}
|
||||||
}, 1000);
|
}, 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Refresh completed. Failed coins: ${failedCoins.length}`);
|
console.log(`Refresh completed. Failed coins: ${failedCoins.length}`);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Critical error during refresh:', error);
|
console.error('Critical error during refresh:', error);
|
||||||
|
|
||||||
|
|
||||||
let countdown = 10;
|
let countdown = 10;
|
||||||
ui.displayErrorMessage(`Refresh failed: ${error.message}. Please try again later. (${countdown}s)`);
|
ui.displayErrorMessage(`Refresh failed: ${error.message}. Please try again later. (${countdown}s)`);
|
||||||
|
|
||||||
const countdownInterval = setInterval(() => {
|
const countdownInterval = setInterval(() => {
|
||||||
countdown--;
|
countdown--;
|
||||||
if (countdown > 0) {
|
if (countdown > 0) {
|
||||||
@@ -1305,7 +1336,7 @@ setupEventListeners: () => {
|
|||||||
app.scheduleNextRefresh();
|
app.scheduleNextRefresh();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
updateNextRefreshTime: () => {
|
updateNextRefreshTime: () => {
|
||||||
console.log('Updating next refresh time display');
|
console.log('Updating next refresh time display');
|
||||||
|
|||||||
Reference in New Issue
Block a user