/**
* Throttles requests to text files with fixed retry intervals.
*
* @param {string[]} fileUrls An array of URLs to text files.
* @param {number} maxRequestsPerInterval The maximum number of requests allowed within each interval.
* @param {number} intervalMs The duration of each throttling interval in milliseconds.
* @param {number} maxRetries The maximum number of retries for each request.
* @returns {Promise<string[]>} A promise that resolves with an array of the file contents. Rejects if any request fails after all retries.
*/
async function throttleFileRequests(fileUrls, maxRequestsPerInterval, intervalMs, maxRetries) {
const results = [];
let requestCount = 0;
let lastIntervalReset = Date.now();
for (const fileUrl of fileUrls) {
let fileContent = '';
let retryCount = 0;
let requestSuccessful = false;
while (!requestSuccessful && retryCount <= maxRetries) {
requestCount = 0; // Reset count at the start of each retry attempt
try {
const response = await fetch(fileUrl);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
fileContent = await response.text();
requestSuccessful = true;
} catch (error) {
console.error(`Request to ${fileUrl} failed. Retrying (${retryCount}/${maxRetries})...`, error);
retryCount++;
// Wait for the interval before retrying
const now = Date.now();
if (now - lastIntervalReset >= intervalMs) {
lastIntervalReset = now;
}
await new Promise(resolve => setTimeout(resolve, intervalMs));
}
}
if (!requestSuccessful) {
console.error(`Failed to retrieve content from ${fileUrl} after ${maxRetries} retries.`);
return Promise.reject(`Failed to retrieve content from ${fileUrl} after ${maxRetries} retries.`);
}
results.push(fileContent);
}
return results;
}
Add your comment