mirror of
https://github.com/basicswap/basicswap.git
synced 2025-02-02 03:06:30 +00:00
Merge pull request #232 from gerlofvanek/cleanup-4
JS: Final tweaks 429
This commit is contained in:
commit
2c176a8c86
1 changed files with 148 additions and 117 deletions
|
@ -141,6 +141,7 @@ const api = {
|
|||
try {
|
||||
const existingCache = localStorage.getItem(cacheKey);
|
||||
let fallbackData = null;
|
||||
|
||||
if (existingCache) {
|
||||
try {
|
||||
const parsed = JSON.parse(existingCache);
|
||||
|
@ -197,12 +198,14 @@ const api = {
|
|||
|
||||
} catch (error) {
|
||||
console.error('Error fetching CoinGecko data:', error);
|
||||
|
||||
const cachedData = cache.get(cacheKey);
|
||||
if (cachedData) {
|
||||
console.log('Using expired cache data due to error');
|
||||
return cachedData.value;
|
||||
}
|
||||
return { error: error.message };
|
||||
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
},
|
||||
|
@ -279,10 +282,11 @@ const api = {
|
|||
const rateLimiter = {
|
||||
lastRequestTime: {},
|
||||
minRequestInterval: {
|
||||
coingecko: 15000,
|
||||
cryptocompare: 1000
|
||||
coingecko: 30000,
|
||||
cryptocompare: 2000
|
||||
},
|
||||
requestQueue: {},
|
||||
retryDelays: [2000, 5000, 10000],
|
||||
|
||||
canMakeRequest: function(apiName) {
|
||||
const now = Date.now();
|
||||
|
@ -300,7 +304,7 @@ const rateLimiter = {
|
|||
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]) {
|
||||
this.requestQueue[apiName] = Promise.resolve();
|
||||
}
|
||||
|
@ -314,15 +318,31 @@ const rateLimiter = {
|
|||
await new Promise(resolve => setTimeout(resolve, waitTime));
|
||||
}
|
||||
|
||||
try {
|
||||
this.updateLastRequestTime(apiName);
|
||||
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();
|
||||
return await this.requestQueue[apiName];
|
||||
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
|
@ -1186,17 +1206,21 @@ setupEventListeners: () => {
|
|||
return;
|
||||
}
|
||||
|
||||
|
||||
const lastGeckoRequest = rateLimiter.lastRequestTime['coingecko'] || 0;
|
||||
const timeSinceLastRequest = Date.now() - lastGeckoRequest;
|
||||
if (timeSinceLastRequest < rateLimiter.minRequestInterval.coingecko) {
|
||||
let waitTime = Math.ceil((rateLimiter.minRequestInterval.coingecko - timeSinceLastRequest) / 1000);
|
||||
const waitTime = Math.max(0, rateLimiter.minRequestInterval.coingecko - timeSinceLastRequest);
|
||||
|
||||
ui.displayErrorMessage(`Rate limit: Please wait ${waitTime} seconds before refreshing`);
|
||||
|
||||
const countdownInterval = setInterval(() => {
|
||||
waitTime--;
|
||||
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 {
|
||||
clearInterval(countdownInterval);
|
||||
ui.hideErrorMessage();
|
||||
|
@ -1206,7 +1230,7 @@ setupEventListeners: () => {
|
|||
return;
|
||||
}
|
||||
|
||||
console.log('Refreshing all data...');
|
||||
console.log('Starting refresh of all data...');
|
||||
app.isRefreshing = true;
|
||||
ui.showLoader();
|
||||
chartModule.showChartLoader();
|
||||
|
@ -1257,8 +1281,10 @@ setupEventListeners: () => {
|
|||
await chartModule.updateChart(chartModule.currentCoin, true);
|
||||
} catch (chartError) {
|
||||
console.error('Chart update failed:', chartError);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
app.lastRefreshedTime = new Date();
|
||||
localStorage.setItem('lastRefreshedTime', app.lastRefreshedTime.getTime().toString());
|
||||
ui.updateLastRefreshedTime();
|
||||
|
@ -1281,11 +1307,16 @@ setupEventListeners: () => {
|
|||
}
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
console.log(`Refresh completed. Failed coins: ${failedCoins.length}`);
|
||||
|
||||
} catch (error) {
|
||||
console.error('Critical error during refresh:', error);
|
||||
|
||||
|
||||
let countdown = 10;
|
||||
ui.displayErrorMessage(`Refresh failed: ${error.message}. Please try again later. (${countdown}s)`);
|
||||
|
||||
const countdownInterval = setInterval(() => {
|
||||
countdown--;
|
||||
if (countdown > 0) {
|
||||
|
|
Loading…
Reference in a new issue