Skip to content

Commit

Permalink
chore: add jitter to retries
Browse files Browse the repository at this point in the history
this will reduce the chance of hoard effect
  • Loading branch information
meysam81 authored and cpanato committed Oct 29, 2024
1 parent 43a7677 commit 0613e86
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ async function fetchWithRetry(url, options = {}, retries = 3, initialDelay = 100
return response;
} catch (error) {
console.warn(`Attempt ${attempt} failed. Error: ${error.message}`);
const delay = Math.min(2 ** attempt * initialDelay, 10000); // Limit max delay to 10 seconds
const jitter = Math.floor(Math.random() * 5000);
const delay = Math.min(2 ** attempt * initialDelay + jitter, 10000); // Limit max delay to 10 seconds
await new Promise(resolve => setTimeout(resolve, delay));
attempt++;
retries--;
Expand Down

0 comments on commit 0613e86

Please sign in to comment.