Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce exponential backoff for AWS API to avoid Rate Exceede… #12400

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions lib/aws/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ if (timeout) {
PromiseQueue.configure(Promise);
const requestQueue = new PromiseQueue(2, Infinity);

const MAX_BACKOFF = 20000; // Maximum backoff time in milliseconds (20 seconds)
const MAX_RETRIES = (() => {
const userValue = Number(process.env.SLS_AWS_REQUEST_MAX_RETRIES);
return userValue >= 0 ? userValue : 4;
return userValue >= 0 ? userValue : 5;
})();

const accelerationCompatibleS3Methods = new Set(['upload', 'putObject']);
Expand Down Expand Up @@ -139,7 +140,6 @@ async function awsRequest(service, method, ...args) {
ensureString(service, { name: 'service' });
service = { name: service };
}
const BASE_BACKOFF = 5000;
const persistentRequest = async (f, numTry = 0) => {
try {
return await f();
Expand All @@ -155,9 +155,11 @@ async function awsRequest(service, method, ...args) {
providerError.statusCode === 429)
) {
const nextTryNum = numTry + 1;
const jitter = Math.random() * 3000 - 1000;
// backoff is between 4 and 7 seconds
const backOff = BASE_BACKOFF + jitter;

// Generate a random base within the range of 0 <= backOffBase <= 1
const backOffBase = Math.random();
// Exponential backoff calculation with cap at MAX_BACKOFF
const backOff = Math.min(backOffBase * Math.pow(2, nextTryNum) * 1000, MAX_BACKOFF);
log.info(
[
`Recoverable error occurred (${e.message}), sleeping for ~${Math.round(
Expand Down