1. /**
  2. * Throttles requests to text files with fixed retry intervals.
  3. *
  4. * @param {string[]} fileUrls An array of URLs to text files.
  5. * @param {number} maxRequestsPerInterval The maximum number of requests allowed within each interval.
  6. * @param {number} intervalMs The duration of each throttling interval in milliseconds.
  7. * @param {number} maxRetries The maximum number of retries for each request.
  8. * @returns {Promise<string[]>} A promise that resolves with an array of the file contents. Rejects if any request fails after all retries.
  9. */
  10. async function throttleFileRequests(fileUrls, maxRequestsPerInterval, intervalMs, maxRetries) {
  11. const results = [];
  12. let requestCount = 0;
  13. let lastIntervalReset = Date.now();
  14. for (const fileUrl of fileUrls) {
  15. let fileContent = '';
  16. let retryCount = 0;
  17. let requestSuccessful = false;
  18. while (!requestSuccessful && retryCount <= maxRetries) {
  19. requestCount = 0; // Reset count at the start of each retry attempt
  20. try {
  21. const response = await fetch(fileUrl);
  22. if (!response.ok) {
  23. throw new Error(`HTTP error! Status: ${response.status}`);
  24. }
  25. fileContent = await response.text();
  26. requestSuccessful = true;
  27. } catch (error) {
  28. console.error(`Request to ${fileUrl} failed. Retrying (${retryCount}/${maxRetries})...`, error);
  29. retryCount++;
  30. // Wait for the interval before retrying
  31. const now = Date.now();
  32. if (now - lastIntervalReset >= intervalMs) {
  33. lastIntervalReset = now;
  34. }
  35. await new Promise(resolve => setTimeout(resolve, intervalMs));
  36. }
  37. }
  38. if (!requestSuccessful) {
  39. console.error(`Failed to retrieve content from ${fileUrl} after ${maxRetries} retries.`);
  40. return Promise.reject(`Failed to retrieve content from ${fileUrl} after ${maxRetries} retries.`);
  41. }
  42. results.push(fileContent);
  43. }
  44. return results;
  45. }

Add your comment